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,6 +42,12 @@ public class SlashCommandsUtil
boolean update = false; boolean update = false;
if(force)
{
update = true;
} else
{
// for each command that we have already registered... // for each command that we have already registered...
for(Command currRegCmd : registeredCommands) for(Command currRegCmd : registeredCommands)
{ {
@ -101,6 +107,7 @@ public class SlashCommandsUtil
} }
} }
} }
}
logger.log("Found " + registeredCommands.size() + " commands."); logger.log("Found " + registeredCommands.size() + " commands.");