Implement avatar grabber command
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-11-20 17:19:40 +01:00
parent 4817bacf5c
commit 3e1ba12314
3 changed files with 55 additions and 0 deletions

View File

@ -12,6 +12,7 @@ public class SlashCommandListener extends ListenerAdapter
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event)
{
switch (event.getName().toLowerCase()) {
case "avatar" -> new AvatarCommand(event);
case "clear" -> new ClearChatCommand(event);
case "coinflip" -> new CoinFlipCommand(event);
case "die" -> new DieCommand(event);

View File

@ -0,0 +1,51 @@
package wtf.beatrice.hidekobot.slashcommands;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import org.jetbrains.annotations.NotNull;
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 AvatarCommand(@NotNull SlashCommandInteractionEvent event)
{
event.deferReply().queue();
User user;
int size;
OptionMapping userArg = event.getOption("user");
if(userArg != null)
{
user = userArg.getAsUser();
} else {
user = event.getUser();
}
OptionMapping sizeArg = event.getOption("size");
if(sizeArg != null)
{
size = sizeArg.getAsInt();
// method to find closest value to accepted values
int distance = Math.abs(acceptedSizes[0] - size);
int idx = 0;
for(int c = 1; c < acceptedSizes.length; c++){
int cdistance = Math.abs(acceptedSizes[c] - size);
if(cdistance < distance){
idx = c;
distance = cdistance;
}
}
size = acceptedSizes[idx];
} else {
size = 512;
}
event.getHook().sendMessage(user.getEffectiveAvatar().getUrl(size)).queue();
}
}

View File

@ -20,6 +20,9 @@ public class SlashCommandsUtil
static List<CommandData> allCommands = new ArrayList<>()
{{
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."));
add(Commands.slash("die", "Stop the bot's process")
.setDefaultPermissions(DefaultMemberPermissions.DISABLED));
add(Commands.slash("clear", "Clear the current channel's chat.")