From a0e0b82f4092459a77733bedd2982c4d86878132 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 09:15:26 +0100 Subject: [PATCH] add user lookup command --- .../cc/fascinated/bat/common/LongUtils.java | 21 +++++ .../bat/features/base/BaseFeature.java | 2 + .../commands/discord/LookupUserCommand.java | 80 +++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/main/java/cc/fascinated/bat/common/LongUtils.java create mode 100644 src/main/java/cc/fascinated/bat/features/base/commands/discord/LookupUserCommand.java diff --git a/src/main/java/cc/fascinated/bat/common/LongUtils.java b/src/main/java/cc/fascinated/bat/common/LongUtils.java new file mode 100644 index 0000000..d2032c4 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/LongUtils.java @@ -0,0 +1,21 @@ +package cc.fascinated.bat.common; + +/** + * @author Fascinated (fascinated7) + */ +public class LongUtils { + /** + * Checks if a string is a long + * + * @param string the string to check + * @return if the string is a long + */ + public static boolean isLong(String string) { + try { + Long.parseLong(string); + return true; + } catch (NumberFormatException exception) { + return false; + } + } +} diff --git a/src/main/java/cc/fascinated/bat/features/base/BaseFeature.java b/src/main/java/cc/fascinated/bat/features/base/BaseFeature.java index 8cda4dc..4d7ebac 100644 --- a/src/main/java/cc/fascinated/bat/features/base/BaseFeature.java +++ b/src/main/java/cc/fascinated/bat/features/base/BaseFeature.java @@ -3,6 +3,7 @@ package cc.fascinated.bat.features.base; import cc.fascinated.bat.command.Category; import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.base.commands.botadmin.premium.PremiumAdminCommand; +import cc.fascinated.bat.features.base.commands.discord.LookupUserCommand; import cc.fascinated.bat.features.base.commands.fun.EightBallCommand; import cc.fascinated.bat.features.base.commands.fun.image.ImageCommand; import cc.fascinated.bat.features.base.commands.general.*; @@ -41,5 +42,6 @@ public class BaseFeature extends Feature { super.registerCommand(commandService, context.getBean(ImageCommand.class)); super.registerCommand(commandService, context.getBean(FeatureCommand.class)); super.registerCommand(commandService, context.getBean(EightBallCommand.class)); + super.registerCommand(commandService, context.getBean(LookupUserCommand.class)); } } diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/discord/LookupUserCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/discord/LookupUserCommand.java new file mode 100644 index 0000000..1472c28 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/features/base/commands/discord/LookupUserCommand.java @@ -0,0 +1,80 @@ +package cc.fascinated.bat.features.base.commands.discord; + +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedDescriptionBuilder; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.common.LongUtils; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import cc.fascinated.bat.service.DiscordService; +import lombok.NonNull; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import org.springframework.stereotype.Component; + +/** + * @author Fascinated (fascinated7) + */ +@Component +@CommandInfo(name = "lookupuser", description = "Lookup a user") +public class LookupUserCommand extends BatCommand { + public LookupUserCommand() { + super.addOption(OptionType.STRING, "id", "The id of the user", true); + } + + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction event) { + OptionMapping idOption = event.getOption("id"); + if (idOption == null) { + return; + } + String id = idOption.getAsString(); + if (!LongUtils.isLong(id)) { + event.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("You need to provide a valid user id") + .build()) + .setEphemeral(true) + .queue(); + return; + } + + DiscordService.JDA.retrieveUserById(id).queue(target -> { + if (target == null) { + event.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("User `%s` not found".formatted(id)) + .build()) + .setEphemeral(true) + .queue(); + return; + } + + // The flags of the user (eg. Discord Partner, Hypesquad Events, etc.) + StringBuilder flags = new StringBuilder(); + for (User.UserFlag flag : target.getFlags()) { + flags.append(flag.getName()).append(", "); + } + + target.retrieveProfile().queue(profile -> event.replyEmbeds(EmbedUtils.genericEmbed() + .setDescription(new EmbedDescriptionBuilder("User Lookup") + .appendLine("Name: `%s`".formatted(target.getGlobalName()), true) + .appendLine("Username: `%s`".formatted(target.getName()), true) + .appendLine("ID: `%s`".formatted(target.getId()), true) + .appendLine("Flags: `%s`".formatted(flags.toString().isEmpty() ? "None" : flags.substring(0, flags.length() - 2)), true) + .appendLine("Joined Discord: ".formatted(target.getTimeCreated().toEpochSecond()), true) + .appendLine("Avatar: %s".formatted(target.getAvatar() == null ? "None" + : "[click here](%s)".formatted(target.getAvatar().getUrl(4096))), true) + .appendLine("Banner: %s".formatted(profile.getBanner() == null ? "None" + : "[click here](%s)".formatted(profile.getBanner().getUrl(4096))), true) + .build()) + .setThumbnail(target.getAvatar() == null ? null : target.getAvatar().getUrl(4096)) + .build()) + .setEphemeral(true) + .queue()); + }); + } +}