Add logic for registering commands globally or owner guild

java-rewrite
Hammy 3 years ago
parent e1452b18c7
commit 3c6fa3e5e3

@ -1,5 +1,6 @@
package me.goudham.bot; package me.goudham.bot;
import io.micronaut.context.annotation.Value;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
import me.goudham.command.CommandManager; import me.goudham.command.CommandManager;
@ -11,12 +12,17 @@ import net.dv8tion.jda.api.entities.Guild;
public class EnsoBot implements Bot { public class EnsoBot implements Bot {
private final JDA jda; private final JDA jda;
private final Guild guild; private final Guild guild;
private final boolean registerCommands;
private final CommandManager commandManager; private final CommandManager commandManager;
@Inject @Inject
public EnsoBot(JDA jda, Guild guild, CommandManager commandManager) { public EnsoBot(JDA jda,
Guild guild,
@Value("${bot.config.registerCommands}") boolean registerCommands,
CommandManager commandManager) {
this.jda = jda; this.jda = jda;
this.guild = guild; this.guild = guild;
this.registerCommands = registerCommands;
this.commandManager = commandManager; this.commandManager = commandManager;
} }
@ -24,7 +30,12 @@ public class EnsoBot implements Bot {
public void startup() throws InterruptedException { public void startup() throws InterruptedException {
jda.awaitReady(); jda.awaitReady();
addEventListeners(); addEventListeners();
commandManager.registerSlashCommands(guild);
if (registerCommands) {
commandManager.registerSlashCommands(guild);
} else {
commandManager.populateCommandMap();
}
} }
private void addEventListeners() { private void addEventListeners() {

@ -1,11 +1,13 @@
package me.goudham.command; package me.goudham.command;
import io.micronaut.context.annotation.Value;
import io.micronaut.core.annotation.Introspected; import io.micronaut.core.annotation.Introspected;
import io.micronaut.inject.ExecutableMethod; import io.micronaut.inject.ExecutableMethod;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.CommandData;
@ -16,19 +18,39 @@ import net.dv8tion.jda.internal.utils.tuple.Pair;
@Introspected @Introspected
public class SlashCommandManager implements CommandManager { public class SlashCommandManager implements CommandManager {
private final Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap; private final Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap;
private final boolean registerCommandsGlobally;
private final boolean registerCommandsForGuild;
private final CommandLoader commandLoader; private final CommandLoader commandLoader;
private final JDA jda;
@Inject @Inject
public SlashCommandManager(Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap, CommandLoader commandLoader) { public SlashCommandManager(Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap,
@Value("${bot.config.registerCommandsGlobally}") boolean registerCommandsGlobally,
@Value("${bot.config.registerCommandsForGuild}") boolean registerCommandsForGuild,
CommandLoader commandLoader,
JDA jda) {
this.commandMap = commandMap; this.commandMap = commandMap;
this.registerCommandsGlobally = registerCommandsGlobally;
this.registerCommandsForGuild = registerCommandsForGuild;
this.commandLoader = commandLoader; this.commandLoader = commandLoader;
this.jda = jda;
}
@Override
public void populateCommandMap() {
commandLoader.populateCommandMap();
} }
@Override @Override
public void registerSlashCommands(Guild guild) { public void registerSlashCommands(Guild guild) {
CommandListUpdateAction commands = guild.updateCommands(); CommandListUpdateAction commands = null;
List<CommandData> commandDataList = commandLoader.loadCommands(); if (registerCommandsGlobally) commands = jda.updateCommands();
commands.addCommands(commandDataList).queue(); if (registerCommandsForGuild) commands = guild.updateCommands();
if (commands != null) {
List<CommandData> registeredSlashCommands = commandLoader.registerSlashCommands();
commands.addCommands(registeredSlashCommands).queue();
}
} }
@Override @Override
@ -36,9 +58,9 @@ public class SlashCommandManager implements CommandManager {
String commandPath = slashCommandEvent.getCommandPath(); String commandPath = slashCommandEvent.getCommandPath();
Pair<Object, ExecutableMethod<Object, Object>> slashCommandPair = commandMap.get(commandPath); Pair<Object, ExecutableMethod<Object, Object>> slashCommandPair = commandMap.get(commandPath);
Object bean = slashCommandPair.getLeft(); Object slashCommandBean = slashCommandPair.getLeft();
ExecutableMethod<Object, Object> method = slashCommandPair.getRight(); ExecutableMethod<Object, Object> slashCommandMethod = slashCommandPair.getRight();
method.invoke(bean, slashCommandEvent); slashCommandMethod.invoke(slashCommandBean, slashCommandEvent);
} }
} }

Loading…
Cancel
Save