Create SlashCommandHandler to decouple loading/registering commands and handling them

java-rewrite
Hammy 3 years ago
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;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
public interface CommandManager {
void populateCommandMap();
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);
}
}

@ -1,35 +1,26 @@
package me.goudham.command;
import io.micronaut.context.annotation.Value;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.inject.ExecutableMethod;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.List;
import java.util.Map;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
import net.dv8tion.jda.internal.utils.tuple.Pair;
@Singleton
@Introspected
public class SlashCommandManager implements CommandManager {
private final Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap;
private final boolean registerCommandsGlobally;
private final boolean registerCommandsForGuild;
private final CommandLoader commandLoader;
private final JDA jda;
@Inject
public SlashCommandManager(Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap,
@Value("${bot.config.registerCommandsGlobally}") boolean registerCommandsGlobally,
public SlashCommandManager(@Value("${bot.config.registerCommandsGlobally}") boolean registerCommandsGlobally,
@Value("${bot.config.registerCommandsForGuild}") boolean registerCommandsForGuild,
CommandLoader commandLoader,
JDA jda) {
this.commandMap = commandMap;
this.registerCommandsGlobally = registerCommandsGlobally;
this.registerCommandsForGuild = registerCommandsForGuild;
this.commandLoader = commandLoader;
@ -52,15 +43,4 @@ public class SlashCommandManager implements CommandManager {
commands.addCommands(registeredSlashCommands).queue();
}
}
@Override
public void handleSlashCommandEvent(SlashCommandEvent slashCommandEvent) {
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);
}
}

@ -2,23 +2,23 @@ package me.goudham.listener;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import me.goudham.command.CommandManager;
import me.goudham.command.CommandHandler;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
@Singleton
public class SlashCommandListener extends ListenerAdapter {
private final CommandManager commandManager;
private final CommandHandler commandHandler;
@Inject
public SlashCommandListener(CommandManager commandManager) {
this.commandManager = commandManager;
public SlashCommandListener(CommandHandler commandHandler) {
this.commandHandler = commandHandler;
}
@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
if (event.getGuild() == null) return;
commandManager.handleSlashCommandEvent(event);
commandHandler.handle(event);
}
}

Loading…
Cancel
Save