From 3e1ba12314ec83b80290279188d5594a37b5773c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 20 Nov 2022 17:19:40 +0100 Subject: [PATCH] Implement avatar grabber command --- .../listeners/SlashCommandListener.java | 1 + .../slashcommands/AvatarCommand.java | 51 +++++++++++++++++++ .../hidekobot/utils/SlashCommandsUtil.java | 3 ++ 3 files changed, 55 insertions(+) create mode 100644 src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index 8da3a8f..605ff1d 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -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); diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java b/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java new file mode 100644 index 0000000..6922c07 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java @@ -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(); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java index 897398a..25072e7 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java @@ -20,6 +20,9 @@ public class SlashCommandsUtil static List 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.")