Implement avatar resolution autocomplete
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Bea 2022-11-20 18:53:28 +01:00
parent 679d16e1fa
commit 3578de17c8
3 changed files with 84 additions and 40 deletions

View File

@ -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<Command.Choice> 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();
}
}
}
}

View File

@ -11,7 +11,7 @@ import java.awt.*;
public class AvatarCommand 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 // 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) public AvatarCommand(@NotNull SlashCommandInteractionEvent event)
{ {

View File

@ -22,7 +22,7 @@ public class SlashCommandsUtil
{{ {{
add(Commands.slash("avatar", "Get someone's profile picture.") add(Commands.slash("avatar", "Get someone's profile picture.")
.addOption(OptionType.USER, "user", "User you want to grab the avatar of.") .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") add(Commands.slash("die", "Stop the bot's process")
.setDefaultPermissions(DefaultMemberPermissions.DISABLED)); .setDefaultPermissions(DefaultMemberPermissions.DISABLED));
add(Commands.slash("clear", "Clear the current channel's chat.") 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.")); add(Commands.slash("ping", "Test if the bot is responsive."));
}}; }};
public static void updateSlashCommands() public static void updateSlashCommands(boolean force)
{ {
JDA jdaInstance = HidekoBot.getAPI(); JDA jdaInstance = HidekoBot.getAPI();
@ -42,56 +42,31 @@ public class SlashCommandsUtil
boolean update = false; boolean update = false;
// for each command that we have already registered... if(force)
for(Command currRegCmd : registeredCommands)
{ {
boolean found = false; update = true;
} else
// 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)
{ {
// for each "recognized" valid command
for(CommandData currCmdData : allCommands) // for each command that we have already registered...
for(Command currRegCmd : registeredCommands)
{ {
boolean found = false; boolean found = false;
// iterate through all already registered commands. // iterate through all "recognized" commands
for(Command cmd : registeredCommands) for(CommandData cmdData : allCommands)
{ {
// if this command was already registered... // if we find the same command...
if(cmd.getName().equals(currCmdData.getName())) if(cmdData.getName().equals(currRegCmd.getName()))
{ {
// quit the loop since we found a match. // quit the loop since we found it.
found = true; found = true;
break; break;
} }
} }
// if no match was found, we need to send an updated command list because // 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) if(!found)
{ {
update = true; update = true;
@ -100,6 +75,38 @@ public class SlashCommandsUtil
break; 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."); logger.log("Found " + registeredCommands.size() + " commands.");