diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 893637b..a50d7f5 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -129,7 +129,7 @@ public class HidekoBot // register message commands MessageCommandListener messageCommandListener = new MessageCommandListener(); messageCommandListener.registerCommand(new HelloCommand()); - messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.InviteCommand()); + messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.AvatarCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BotInfoCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.CoinFlipCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.ClearCommand()); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/Avatar.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/Avatar.java new file mode 100644 index 0000000..2515269 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/Avatar.java @@ -0,0 +1,62 @@ +package wtf.beatrice.hidekobot.commands.base; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.User; +import wtf.beatrice.hidekobot.Cache; + +public class Avatar +{ + public static int parseResolution(int resolution) + { + int[] acceptedSizes = Cache.getSupportedAvatarResolutions(); + + // method to find closest value to accepted values + int distance = Math.abs(acceptedSizes[0] - resolution); + int idx = 0; + for(int c = 1; c < acceptedSizes.length; c++){ + int cdistance = Math.abs(acceptedSizes[c] - resolution); + if(cdistance < distance){ + idx = c; + distance = cdistance; + } + } + + return acceptedSizes[idx]; + } + + public static MessageEmbed buildEmbed(int resolution, User user) + { + int[] acceptedSizes = Cache.getSupportedAvatarResolutions(); + EmbedBuilder embedBuilder = new EmbedBuilder(); + + embedBuilder.setColor(Cache.getBotColor()); + embedBuilder.setTitle("Profile picture"); + + embedBuilder.addField("User", "<@" + user.getId() + ">", false); + + embedBuilder.addField("Current resolution", resolution + " × " + resolution, false); + + // string builder to create a string that links to all available resolutions + 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) // don't add a separator on the last iteration + { + links.append(" | "); + } + } + + embedBuilder.addField("Available resolutions", links.toString(), false); + + + + embedBuilder.setImage(user.getEffectiveAvatar().getUrl(resolution)); + return embedBuilder.build(); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/AvatarCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/AvatarCommand.java new file mode 100644 index 0000000..bbf0208 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/AvatarCommand.java @@ -0,0 +1,81 @@ +package wtf.beatrice.hidekobot.commands.message; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.IMentionable; +import net.dv8tion.jda.api.entities.Mentions; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import org.jetbrains.annotations.Nullable; +import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.commands.base.Avatar; +import wtf.beatrice.hidekobot.objects.commands.MessageCommand; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class AvatarCommand implements MessageCommand +{ + + @Override + public LinkedList getCommandLabels() { + return new LinkedList<>(Collections.singletonList("avatar")); + } + + @Nullable + @Override + public List getPermissions() { + return null; // anyone can use it + } + + @Override + public boolean passRawArgs() { + return false; + } + + @Override + public void runCommand(MessageReceivedEvent event, String label, String[] args) + { + int[] acceptedSizes = Cache.getSupportedAvatarResolutions(); + + User user = null; + int resolution = -1; + + // we have no specific order for user and resolution, so let's try parsing any arg as resolution + // (mentions are handled differently by a specific method) + boolean resFound = false; + + for (String arg : args) { + try { + int givenRes = Integer.parseInt(arg); + resolution = Avatar.parseResolution(givenRes); + resFound = true; + break; + } catch (NumberFormatException ignored) { + } + } + + // fallback in case we didn't find any specified resolution + if(!resFound) resolution = Avatar.parseResolution(512); + + // check if someone is mentioned + Mentions mentions = event.getMessage().getMentions(); + if(mentions.getMentions().isEmpty()) + { + user = event.getAuthor(); + } else + { + String mentionedId = mentions.getMentions().get(0).getId(); + user = HidekoBot.getAPI().retrieveUserById(mentionedId).complete(); + } + + // in case of issues, fallback to the sender + if(user == null) user = event.getAuthor(); + + // send a response + MessageEmbed embed = Avatar.buildEmbed(resolution, user); + event.getMessage().replyEmbeds(embed).queue(); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java index 5077120..7281212 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java @@ -1,6 +1,7 @@ package wtf.beatrice.hidekobot.commands.slash; import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.commands.OptionMapping; @@ -9,6 +10,7 @@ import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.Commands; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.commands.base.Avatar; import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl; public class AvatarCommand extends SlashCommandImpl @@ -31,9 +33,6 @@ public class AvatarCommand extends SlashCommandImpl User user; int resolution; - int[] acceptedSizes = Cache.getSupportedAvatarResolutions(); - - OptionMapping userArg = event.getOption("user"); if(userArg != null) { @@ -45,57 +44,12 @@ public class AvatarCommand extends SlashCommandImpl OptionMapping sizeArg = event.getOption("size"); if(sizeArg != null) { - resolution = sizeArg.getAsInt(); - - // method to find closest value to accepted values - int distance = Math.abs(acceptedSizes[0] - resolution); - int idx = 0; - for(int c = 1; c < acceptedSizes.length; c++){ - int cdistance = Math.abs(acceptedSizes[c] - resolution); - if(cdistance < distance){ - idx = c; - distance = cdistance; - } - } - resolution = acceptedSizes[idx]; - + resolution = Avatar.parseResolution(sizeArg.getAsInt()); } else { - resolution = 512; + resolution = Avatar.parseResolution(512); } - EmbedBuilder embedBuilder = new EmbedBuilder(); - - // embed processing - { - embedBuilder.setColor(Cache.getBotColor()); - embedBuilder.setTitle("Profile picture"); - - embedBuilder.addField("User", "<@" + user.getId() + ">", false); - - embedBuilder.addField("Current resolution", resolution + " × " + resolution, false); - - // string builder to create a string that links to all available resolutions - 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) // don't add a separator on the last iteration - { - links.append(" | "); - } - } - - embedBuilder.addField("Available resolutions", links.toString(), false); - - - - embedBuilder.setImage(user.getEffectiveAvatar().getUrl(resolution)); - } - - event.getHook().editOriginalEmbeds(embedBuilder.build()).queue(); + MessageEmbed embed = Avatar.buildEmbed(resolution, user); + event.getHook().editOriginalEmbeds(embed).queue(); } }