Refactor CommandManager.java to interface & Add SlashCommandManager.java

java-rewrite
Hammy 3 years ago
parent 158fcf2176
commit 3845b50f36

@ -1,30 +1,9 @@
package me.goudham.command; 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.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
@Singleton public interface CommandManager {
@Introspected void registerSlashCommands(Guild guild);
public class CommandManager { void handleSlashCommandEvent(SlashCommandEvent slashCommandEvent);
private final Map<String, BeanDefinition<?>> 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<CommandData> commandDataList = commandLoader.loadIntoMapAndReturnCommands(commandMap);
commands.addCommands(commandDataList).queue();
}
} }

@ -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<String, ISlashCommand> 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<CommandData> 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);
}
}
Loading…
Cancel
Save