changed context handling for command models for logging

added method to convert the user initiated context to a concrete command model
added method to find if a template exists
added method to render a context aware model
added banid command
This commit is contained in:
Sheldan
2020-03-20 09:34:37 +01:00
parent ec21305725
commit c978c1c677
68 changed files with 412 additions and 254 deletions

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.command.execution;
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
import lombok.Builder;
import lombok.Getter;
import net.dv8tion.jda.api.JDA;
@@ -12,7 +13,7 @@ public class CommandContext {
private Guild guild;
private Member author;
private Message message;
private CommandTemplateContext commandTemplateContext;
private UserInitiatedServerContext userInitiatedContext;
private Parameters parameters;
private JDA jda;
}

View File

@@ -1,18 +0,0 @@
package dev.sheldan.abstracto.command.execution;
import dev.sheldan.abstracto.core.models.AChannel;
import dev.sheldan.abstracto.core.models.AServer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
@Getter @Builder @AllArgsConstructor
public class CommandTemplateContext {
private AChannel channel;
private AServer server;
public CommandTemplateContext(CommandTemplateContext commandTemplateContext) {
this.channel = commandTemplateContext.channel;
this.server = commandTemplateContext.server;
}
}

View File

@@ -0,0 +1,31 @@
package dev.sheldan.abstracto.command.execution;
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Slf4j
public class ContextConverter {
public static <T extends UserInitiatedServerContext> UserInitiatedServerContext fromCommandContext(CommandContext commandContext, Class<T> clazz) {
Method m = null;
try {
m = clazz.getMethod("builder");
UserInitiatedServerContext.UserInitiatedServerContextBuilder<?, ?> builder = (UserInitiatedServerContext.UserInitiatedServerContextBuilder) m.invoke(null, null);
return builder
.member(commandContext.getAuthor())
.guild(commandContext.getGuild())
.textChannel(commandContext.getChannel())
.channel(commandContext.getUserInitiatedContext().getChannel())
.server(commandContext.getUserInitiatedContext().getServer())
.aUserInAServer(commandContext.getUserInitiatedContext().getAUserInAServer())
.user(commandContext.getUserInitiatedContext().getUser())
.build();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
log.error("Failed to execute builder method", e);
}
throw new RuntimeException("Failed to create model from context");
}
}