2022-11-20 18:56:57 +01:00
|
|
|
|
package wtf.beatrice.hidekobot.commands.slash;
|
2022-11-20 17:19:40 +01:00
|
|
|
|
|
2022-11-20 18:06:07 +01:00
|
|
|
|
import net.dv8tion.jda.api.EmbedBuilder;
|
2022-11-20 17:19:40 +01:00
|
|
|
|
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;
|
2022-11-20 19:00:27 +01:00
|
|
|
|
import wtf.beatrice.hidekobot.Configuration;
|
2022-11-20 17:19:40 +01:00
|
|
|
|
|
2022-11-20 18:06:07 +01:00
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
2022-11-20 17:19:40 +01:00
|
|
|
|
public class AvatarCommand
|
|
|
|
|
{
|
|
|
|
|
|
2022-11-20 22:09:58 +01:00
|
|
|
|
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
|
2022-11-20 17:19:40 +01:00
|
|
|
|
{
|
|
|
|
|
event.deferReply().queue();
|
|
|
|
|
|
|
|
|
|
User user;
|
2022-11-20 18:06:07 +01:00
|
|
|
|
int resolution;
|
2022-11-20 17:19:40 +01:00
|
|
|
|
|
2022-11-20 19:00:27 +01:00
|
|
|
|
int[] acceptedSizes = Configuration.getSupportedAvatarResolutions();
|
|
|
|
|
|
|
|
|
|
|
2022-11-20 17:19:40 +01:00
|
|
|
|
OptionMapping userArg = event.getOption("user");
|
|
|
|
|
if(userArg != null)
|
|
|
|
|
{
|
|
|
|
|
user = userArg.getAsUser();
|
|
|
|
|
} else {
|
|
|
|
|
user = event.getUser();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OptionMapping sizeArg = event.getOption("size");
|
|
|
|
|
if(sizeArg != null)
|
|
|
|
|
{
|
2022-11-20 18:06:07 +01:00
|
|
|
|
resolution = sizeArg.getAsInt();
|
2022-11-20 17:19:40 +01:00
|
|
|
|
|
|
|
|
|
// method to find closest value to accepted values
|
2022-11-20 18:06:07 +01:00
|
|
|
|
int distance = Math.abs(acceptedSizes[0] - resolution);
|
2022-11-20 17:19:40 +01:00
|
|
|
|
int idx = 0;
|
|
|
|
|
for(int c = 1; c < acceptedSizes.length; c++){
|
2022-11-20 18:06:07 +01:00
|
|
|
|
int cdistance = Math.abs(acceptedSizes[c] - resolution);
|
2022-11-20 17:19:40 +01:00
|
|
|
|
if(cdistance < distance){
|
|
|
|
|
idx = c;
|
|
|
|
|
distance = cdistance;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-20 18:06:07 +01:00
|
|
|
|
resolution = acceptedSizes[idx];
|
2022-11-20 17:19:40 +01:00
|
|
|
|
|
|
|
|
|
} else {
|
2022-11-20 18:06:07 +01:00
|
|
|
|
resolution = 512;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EmbedBuilder embedBuilder = new EmbedBuilder();
|
|
|
|
|
|
2022-11-20 18:11:00 +01:00
|
|
|
|
// embed processing
|
2022-11-20 18:06:07 +01:00
|
|
|
|
{
|
|
|
|
|
embedBuilder.setColor(Color.PINK);
|
2022-11-20 18:22:32 +01:00
|
|
|
|
embedBuilder.setTitle("Profile picture");
|
|
|
|
|
|
|
|
|
|
embedBuilder.addField("User", "<@" + user.getId() + ">", false);
|
2022-11-20 18:06:07 +01:00
|
|
|
|
|
|
|
|
|
embedBuilder.addField("Current resolution", resolution + " × " + resolution, false);
|
|
|
|
|
|
2022-11-20 18:11:00 +01:00
|
|
|
|
// string builder to create a string that links to all available resolutions
|
2022-11-20 18:06:07 +01:00
|
|
|
|
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(")");
|
2022-11-20 18:11:00 +01:00
|
|
|
|
if(pos + 1 != acceptedSizes.length) // don't add a separator on the last iteration
|
2022-11-20 18:06:07 +01:00
|
|
|
|
{
|
|
|
|
|
links.append(" | ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
embedBuilder.addField("Available resolutions", links.toString(), false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
embedBuilder.setImage(user.getEffectiveAvatar().getUrl(resolution));
|
2022-11-20 17:19:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-20 18:06:07 +01:00
|
|
|
|
event.getHook().editOriginalEmbeds(embedBuilder.build()).queue();
|
2022-11-20 17:19:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|