From 3845b50f369f6cbbed9dcda81fbe1fe028d5889a Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 19 Sep 2021 01:20:43 +0100 Subject: [PATCH] Refactor CommandManager.java to interface & Add SlashCommandManager.java --- .../me/goudham/command/CommandManager.java | 29 ++------------ .../goudham/command/SlashCommandManager.java | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 src/main/java/me/goudham/command/SlashCommandManager.java diff --git a/src/main/java/me/goudham/command/CommandManager.java b/src/main/java/me/goudham/command/CommandManager.java index 90fe2461..719d4b2c 100644 --- a/src/main/java/me/goudham/command/CommandManager.java +++ b/src/main/java/me/goudham/command/CommandManager.java @@ -1,30 +1,9 @@ package me.goudham.command; -import io.micronaut.core.annotation.Introspected; -import io.micronaut.inject.BeanDefinition; -import jakarta.inject.Inject; -import jakarta.inject.Singleton; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; -import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -@Singleton -@Introspected -public class CommandManager { - private final Map> commandMap = new HashMap<>(); - private final CommandLoader commandLoader; - - @Inject - public CommandManager(CommandLoader commandLoader) { - this.commandLoader = commandLoader; - } - - public void registerSlashCommands(Guild guild) { - CommandListUpdateAction commands = guild.updateCommands(); - List commandDataList = commandLoader.loadIntoMapAndReturnCommands(commandMap); - commands.addCommands(commandDataList).queue(); - } +public interface CommandManager { + void registerSlashCommands(Guild guild); + void handleSlashCommandEvent(SlashCommandEvent slashCommandEvent); } diff --git a/src/main/java/me/goudham/command/SlashCommandManager.java b/src/main/java/me/goudham/command/SlashCommandManager.java new file mode 100644 index 00000000..c3f556c6 --- /dev/null +++ b/src/main/java/me/goudham/command/SlashCommandManager.java @@ -0,0 +1,39 @@ +package me.goudham.command; + +import io.micronaut.core.annotation.Introspected; +import jakarta.inject.Inject; +import jakarta.inject.Singleton; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import me.goudham.bot.command.ISlashCommand; +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; + +@Singleton +@Introspected +public class SlashCommandManager implements CommandManager { + private final Map commandMap = new HashMap<>(); + private final CommandLoader commandLoader; + + @Inject + public SlashCommandManager(CommandLoader commandLoader) { + this.commandLoader = commandLoader; + } + + @Override + public void registerSlashCommands(Guild guild) { + CommandListUpdateAction commands = guild.updateCommands(); + List commandDataList = commandLoader.loadIntoMapAndReturnCommands(commandMap); + commands.addCommands(commandDataList).queue(); + } + + @Override + public void handleSlashCommandEvent(SlashCommandEvent slashCommandEvent) { + String name = slashCommandEvent.getName(); + ISlashCommand slashCommand = commandMap.get(name); + slashCommand.handle(slashCommandEvent); + } +}