From 044445890f12a59b5e67644127a87f89514af90d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 20 Nov 2022 18:06:07 +0100 Subject: [PATCH] Improve avatar command Avatar command now produces an embed with links to all possible resolutions --- .../slashcommands/AvatarCommand.java | 47 ++++++++++++++++--- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java b/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java index 6922c07..a7ca8a5 100644 --- a/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java @@ -1,10 +1,13 @@ package wtf.beatrice.hidekobot.slashcommands; +import net.dv8tion.jda.api.EmbedBuilder; 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; +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 @@ -15,7 +18,7 @@ public class AvatarCommand event.deferReply().queue(); User user; - int size; + int resolution; OptionMapping userArg = event.getOption("user"); if(userArg != null) @@ -28,24 +31,54 @@ public class AvatarCommand OptionMapping sizeArg = event.getOption("size"); if(sizeArg != null) { - size = sizeArg.getAsInt(); + resolution = sizeArg.getAsInt(); // method to find closest value to accepted values - int distance = Math.abs(acceptedSizes[0] - size); + int distance = Math.abs(acceptedSizes[0] - resolution); int idx = 0; for(int c = 1; c < acceptedSizes.length; c++){ - int cdistance = Math.abs(acceptedSizes[c] - size); + int cdistance = Math.abs(acceptedSizes[c] - resolution); if(cdistance < distance){ idx = c; distance = cdistance; } } - size = acceptedSizes[idx]; + resolution = acceptedSizes[idx]; } else { - size = 512; + resolution = 512; } - event.getHook().sendMessage(user.getEffectiveAvatar().getUrl(size)).queue(); + EmbedBuilder embedBuilder = new EmbedBuilder(); + + { + embedBuilder.setColor(Color.PINK); + embedBuilder.setAuthor(event.getUser().getAsTag(), null, event.getUser().getEffectiveAvatarUrl()); + embedBuilder.setTitle(user.getAsTag() + "'s profile picture"); + + embedBuilder.addField("Current resolution", resolution + " × " + resolution, false); + + StringBuilder links = new StringBuilder(); + for(int pos = 0; pos < acceptedSizes.length; pos++) + { + int currSize = acceptedSizes[pos]; + + String currLink = user.getEffectiveAvatar().getUrl(currSize); + + links.append("[").append(currSize).append("px](").append(currLink).append(")"); + if(pos+1 != acceptedSizes.length) + { + links.append(" | "); + } + } + + embedBuilder.addField("Available resolutions", links.toString(), false); + + + + embedBuilder.setImage(user.getEffectiveAvatar().getUrl(resolution)); + } + + event.getHook().editOriginalEmbeds(embedBuilder.build()).queue(); } }