diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index e4b54fd..751a396 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -7,7 +7,9 @@ import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.requests.GatewayIntent; import wtf.beatrice.hidekobot.listeners.MessageListener; import wtf.beatrice.hidekobot.listeners.MessageLogger; +import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.utils.Logger; +import wtf.beatrice.hidekobot.utils.SlashCommandsUtil; import javax.security.auth.login.LoginException; import java.util.ArrayList; @@ -18,7 +20,7 @@ public class HidekoBot { private static String botToken; private static String standardInviteLink = - "https://discord.com/oauth2/authorize?client_id=%userid%&scope=bot&permissions=8"; + "https://discord.com/oauth2/authorize?client_id=%userid%&scope=bot+applications.commands&permissions=8"; private static String botUserId; private static final String version = "0.0.1"; // we should probably find a way to make this consistent with Maven @@ -66,6 +68,10 @@ public class HidekoBot // register listeners jda.addEventListener(new MessageListener()); + jda.addEventListener(new SlashCommandListener()); + + // update slash commands + SlashCommandsUtil.updateSlashCommands(jda); // set the bot's status jda.getPresence().setStatus(OnlineStatus.ONLINE); diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java new file mode 100644 index 0000000..bbab6c7 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -0,0 +1,19 @@ +package wtf.beatrice.hidekobot.listeners; + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; + +public class SlashCommandListener extends ListenerAdapter +{ + + @Override + public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) + { + if(event.getName().equals("ping")) + { + event.reply("Pong!").queue(); + } + + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java new file mode 100644 index 0000000..51c7b50 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java @@ -0,0 +1,91 @@ +package wtf.beatrice.hidekobot.utils; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.interactions.commands.Command; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.interactions.commands.build.Commands; +import wtf.beatrice.hidekobot.listeners.MessageListener; + +import java.util.ArrayList; +import java.util.List; + +public class SlashCommandsUtil +{ + + private static final Logger logger = new Logger(MessageListener.class); + + static List allCommands = new ArrayList<>() + {{ + add(Commands.slash("ping", "Test if the bot is responsive.")); + }}; + + public static void updateSlashCommands(JDA jdaInstance) + { + List toAdd = new ArrayList<>(); + List toDelete = new ArrayList<>(); + + List registeredCommands = jdaInstance.retrieveCommands().complete(); + + // for each command that we have already registered... + for(Command currRegCmd : registeredCommands) + { + // queue it for removal. + boolean toRemove = true; + + // iterate through all "recognized" commands + for(CommandData cmdData : allCommands) + { + // if we find the same command... + if(cmdData.getName().equals(currRegCmd.getName())) + { + // then don't remove it + toRemove = false; + // and quit the loop since we found it. + break; + } + } + + // if no match was found, queue this command for removal. + if(toRemove) toDelete.add(currRegCmd); + + } + + // for each "recognized" valid command + for(CommandData currCmdData : allCommands) + { + // queue it for registration. + boolean toRegister = true; + + // iterate through all already registered commands. + for(Command cmd : registeredCommands) + { + // if this command was already registered... + if(cmd.getName().equals(currCmdData.getName())) + { + // flag that we don't need to register it + toRegister = false; + // and quit the loop since we found a match. + break; + } + } + + // if no match was found, queue this command for registration. + if(toRegister) toAdd.add(currCmdData); + } + + logger.log("Found " + registeredCommands.size() + " commands."); + + // remove all commands queued for removal. + for(Command cmd : toDelete) + { + jdaInstance.deleteCommandById(cmd.getId()).queue(); + } + + logger.log("Deleted " + toDelete.size() + " commands."); + + // register all new commands. + jdaInstance.updateCommands().addCommands(toAdd).queue(); + logger.log("Registered " + toAdd.size() + " new commands."); + + } +}