From 3578de17c89a907282468a4b48729f15332d3122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 20 Nov 2022 18:53:28 +0100 Subject: [PATCH] Implement avatar resolution autocomplete --- .../listeners/SlashCommandCompleter.java | 37 ++++++++ .../slashcommands/AvatarCommand.java | 2 +- .../hidekobot/utils/SlashCommandsUtil.java | 85 ++++++++++--------- 3 files changed, 84 insertions(+), 40 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java new file mode 100644 index 0000000..daa8fdf --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java @@ -0,0 +1,37 @@ +package wtf.beatrice.hidekobot.listeners; + +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.dv8tion.jda.api.interactions.commands.Command; +import wtf.beatrice.hidekobot.slashcommands.AvatarCommand; + +import java.util.ArrayList; +import java.util.List; + +public class SlashCommandCompleter extends ListenerAdapter +{ + + @Override + public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) + { + if(event.getName().equals("avatar")) + { + if(event.getFocusedOption().getName().equals("size")) + { + + List options = new ArrayList<>(); + + for(int res : AvatarCommand.acceptedSizes) + { + String resString = String.valueOf(res); + String userInput = event.getFocusedOption().getValue(); + + if(resString.startsWith(userInput)) + options.add(new Command.Choice(resString, res)); + } + + event.replyChoices(options).queue(); + } + } + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java b/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java index f3a4eed..49916b8 100644 --- a/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java @@ -11,7 +11,7 @@ import java.awt.*; public class AvatarCommand { // discord api returns a broken image if you don't use specific sizes (powers of 2), so we limit it to these - private final int[] acceptedSizes = { 16, 32, 64, 128, 256, 512, 1024 }; + public static final int[] acceptedSizes = { 16, 32, 64, 128, 256, 512, 1024 }; public AvatarCommand(@NotNull SlashCommandInteractionEvent event) { diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java index 25072e7..1bc5871 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java @@ -22,7 +22,7 @@ public class SlashCommandsUtil {{ add(Commands.slash("avatar", "Get someone's profile picture.") .addOption(OptionType.USER, "user", "User you want to grab the avatar of.") - .addOption(OptionType.INTEGER, "size", "The size of the returned image.")); + .addOption(OptionType.INTEGER, "size", "The size of the returned image.", false, true)); add(Commands.slash("die", "Stop the bot's process") .setDefaultPermissions(DefaultMemberPermissions.DISABLED)); add(Commands.slash("clear", "Clear the current channel's chat.") @@ -33,7 +33,7 @@ public class SlashCommandsUtil add(Commands.slash("ping", "Test if the bot is responsive.")); }}; - public static void updateSlashCommands() + public static void updateSlashCommands(boolean force) { JDA jdaInstance = HidekoBot.getAPI(); @@ -42,56 +42,31 @@ public class SlashCommandsUtil boolean update = false; - // for each command that we have already registered... - for(Command currRegCmd : registeredCommands) + if(force) { - boolean found = false; - - // iterate through all "recognized" commands - for(CommandData cmdData : allCommands) - { - // if we find the same command... - if(cmdData.getName().equals(currRegCmd.getName())) - { - // quit the loop since we found it. - found = true; - break; - } - } - - // if no match was found, we need to send an updated command list because - // an old command was probably removed. - if(!found) - { - update = true; - - // quit the loop since we only need to trigger this once. - break; - } - } - - // if an update is not already queued... - if(!update) + update = true; + } else { - // for each "recognized" valid command - for(CommandData currCmdData : allCommands) + + // for each command that we have already registered... + for(Command currRegCmd : registeredCommands) { boolean found = false; - // iterate through all already registered commands. - for(Command cmd : registeredCommands) + // iterate through all "recognized" commands + for(CommandData cmdData : allCommands) { - // if this command was already registered... - if(cmd.getName().equals(currCmdData.getName())) + // if we find the same command... + if(cmdData.getName().equals(currRegCmd.getName())) { - // quit the loop since we found a match. + // quit the loop since we found it. found = true; break; } } // if no match was found, we need to send an updated command list because - // a new command was probably added. + // an old command was probably removed. if(!found) { update = true; @@ -100,6 +75,38 @@ public class SlashCommandsUtil break; } } + + // if an update is not already queued... + if(!update) + { + // for each "recognized" valid command + for(CommandData currCmdData : allCommands) + { + boolean found = false; + + // iterate through all already registered commands. + for(Command cmd : registeredCommands) + { + // if this command was already registered... + if(cmd.getName().equals(currCmdData.getName())) + { + // quit the loop since we found a match. + found = true; + break; + } + } + + // if no match was found, we need to send an updated command list because + // a new command was probably added. + if(!found) + { + update = true; + + // quit the loop since we only need to trigger this once. + break; + } + } + } } logger.log("Found " + registeredCommands.size() + " commands.");