|
|
@ -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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|