mirror of https://github.com/sgoudham/Enso-Bot.git
Create SlashCommandHandler to decouple loading/registering commands and handling them
parent
a3a4473d0c
commit
f855e60343
@ -0,0 +1,7 @@
|
|||||||
|
package me.goudham.command;
|
||||||
|
|
||||||
|
import net.dv8tion.jda.api.events.Event;
|
||||||
|
|
||||||
|
public interface CommandHandler {
|
||||||
|
void handle(Event event);
|
||||||
|
}
|
@ -1,10 +1,8 @@
|
|||||||
package me.goudham.command;
|
package me.goudham.command;
|
||||||
|
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
|
|
||||||
|
|
||||||
public interface CommandManager {
|
public interface CommandManager {
|
||||||
void populateCommandMap();
|
void populateCommandMap();
|
||||||
void registerSlashCommands(Guild guild);
|
void registerSlashCommands(Guild guild);
|
||||||
void handleSlashCommandEvent(SlashCommandEvent slashCommandEvent);
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package me.goudham.command;
|
||||||
|
|
||||||
|
import io.micronaut.inject.ExecutableMethod;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import jakarta.inject.Singleton;
|
||||||
|
import java.util.Map;
|
||||||
|
import net.dv8tion.jda.api.events.Event;
|
||||||
|
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
|
||||||
|
import net.dv8tion.jda.internal.utils.tuple.Pair;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
public class SlashCommandHandler implements CommandHandler {
|
||||||
|
private final Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public SlashCommandHandler(Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap) {
|
||||||
|
this.commandMap = commandMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(Event event) {
|
||||||
|
SlashCommandEvent slashCommandEvent = (SlashCommandEvent) event;
|
||||||
|
String commandPath = slashCommandEvent.getCommandPath();
|
||||||
|
|
||||||
|
Pair<Object, ExecutableMethod<Object, Object>> slashCommandPair = commandMap.get(commandPath);
|
||||||
|
Object slashCommandBean = slashCommandPair.getLeft();
|
||||||
|
ExecutableMethod<Object, Object> slashCommandMethod = slashCommandPair.getRight();
|
||||||
|
|
||||||
|
slashCommandMethod.invoke(slashCommandBean, slashCommandEvent);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue