From f1bc2b2aaa89eb104dde273873dc13e9c5564aa8 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 9 Jul 2024 20:34:56 +0100 Subject: [PATCH] add user validation to moderation and to user lookup --- .../cc/fascinated/bat/command/BatCommand.java | 1 - .../utility/lookup/UserSubCommand.java | 69 +++++++++---------- .../moderation/command/BanCommand.java | 7 ++ .../moderation/command/KickCommand.java | 8 ++- .../moderation/command/MuteCommand.java | 7 ++ .../command/PunishHistoryCommand.java | 7 ++ .../moderation/command/PurgeCommand.java | 2 - .../moderation/command/UnbanCommand.java | 9 ++- .../moderation/command/UnmuteCommand.java | 7 ++ .../moderation/command/WarnCommand.java | 7 ++ 10 files changed, 81 insertions(+), 43 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/command/BatCommand.java b/src/main/java/cc/fascinated/bat/command/BatCommand.java index 58f9834..b5bb0b7 100644 --- a/src/main/java/cc/fascinated/bat/command/BatCommand.java +++ b/src/main/java/cc/fascinated/bat/command/BatCommand.java @@ -13,7 +13,6 @@ import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.dv8tion.jda.internal.interactions.CommandDataImpl; -import org.codehaus.plexus.util.cli.Arg; import java.util.Collections; import java.util.EnumSet; diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/UserSubCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/UserSubCommand.java index a3b1763..64224a6 100644 --- a/src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/UserSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/UserSubCommand.java @@ -4,10 +4,9 @@ import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.common.DescriptionBuilder; 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 cc.fascinated.bat.service.UserService; import lombok.NonNull; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.User; @@ -16,6 +15,7 @@ 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 net.dv8tion.jda.api.interactions.commands.build.OptionData; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** @@ -24,7 +24,11 @@ import org.springframework.stereotype.Component; @Component("lookup.user:sub") @CommandInfo(name = "user", description = "Lookup a user") public class UserSubCommand extends BatCommand { - public UserSubCommand() { + private final UserService userService; + + @Autowired + public UserSubCommand(@NonNull UserService userService) { + this.userService = userService; super.addOptions(new OptionData(OptionType.STRING, "id", "The id of the user", true)); } @@ -34,48 +38,37 @@ public class UserSubCommand extends BatCommand { if (idOption == null) { return; } - String id = idOption.getAsString(); - if (!LongUtils.isLong(id)) { + User target = userService.getUser(idOption.getAsString()).getDiscordUser(); + if (target == null) { event.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("You need to provide a valid user id") + .setDescription("User `%s` not found".formatted(idOption.getAsString())) .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)) + // The flags of the user (eg. Discord Partner, Hypesquad Events, etc.) + StringBuilder flags = new StringBuilder(); + for (User.UserFlag flag : target.getFlags()) { + flags.append("`").append(flag.getName()).append("`, "); + } + String name = target.getGlobalName() == null ? target.getName() : target.getGlobalName().replaceAll("`", ""); + target.retrieveProfile().queue(profile -> event.replyEmbeds(EmbedUtils.genericEmbed() + .setDescription(new DescriptionBuilder("User Lookup") + .appendLine("Name: `%s`".formatted(name), 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()) - .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("`").append(flag.getName()).append("`, "); - } - String name = target.getGlobalName() == null ? target.getName() : target.getGlobalName().replaceAll("`", ""); - target.retrieveProfile().queue(profile -> event.replyEmbeds(EmbedUtils.genericEmbed() - .setDescription(new DescriptionBuilder("User Lookup") - .appendLine("Name: `%s`".formatted(name), 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()); - }); + .setThumbnail(target.getAvatar() == null ? null : target.getAvatar().getUrl(4096)) + .build()) + .setEphemeral(true) + .queue()); } } diff --git a/src/main/java/cc/fascinated/bat/features/moderation/command/BanCommand.java b/src/main/java/cc/fascinated/bat/features/moderation/command/BanCommand.java index 1d1aa6e..c7b2349 100644 --- a/src/main/java/cc/fascinated/bat/features/moderation/command/BanCommand.java +++ b/src/main/java/cc/fascinated/bat/features/moderation/command/BanCommand.java @@ -49,6 +49,13 @@ public class BanCommand extends BatCommand { OptionMapping lengthOption = event.getOption("length"); assert memberOption != null; BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); + if (targetUser == null) { + event.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("User not found") + .build() + ).queue(); + return; + } String reason = reasonOption == null ? null : reasonOption.getAsString(); long length = lengthOption == null ? -1 : TimeUtils.fromString(lengthOption.getAsString()); diff --git a/src/main/java/cc/fascinated/bat/features/moderation/command/KickCommand.java b/src/main/java/cc/fascinated/bat/features/moderation/command/KickCommand.java index 3686ef1..aa2de93 100644 --- a/src/main/java/cc/fascinated/bat/features/moderation/command/KickCommand.java +++ b/src/main/java/cc/fascinated/bat/features/moderation/command/KickCommand.java @@ -3,7 +3,6 @@ package cc.fascinated.bat.features.moderation.command; import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.common.EmbedUtils; -import cc.fascinated.bat.common.RoleUtils; import cc.fascinated.bat.features.moderation.punish.PunishmentProfile; import cc.fascinated.bat.features.moderation.punish.PunishmentType; import cc.fascinated.bat.model.BatGuild; @@ -48,6 +47,13 @@ public class KickCommand extends BatCommand { OptionMapping reasonOption = event.getOption("reason"); assert memberOption != null; BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); + if (targetUser == null) { + event.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("User not found") + .build() + ).queue(); + return; + } String reason = reasonOption == null ? null : reasonOption.getAsString(); PunishmentProfile profile = guild.getPunishmentProfile(); diff --git a/src/main/java/cc/fascinated/bat/features/moderation/command/MuteCommand.java b/src/main/java/cc/fascinated/bat/features/moderation/command/MuteCommand.java index 5fab7a6..77667f1 100644 --- a/src/main/java/cc/fascinated/bat/features/moderation/command/MuteCommand.java +++ b/src/main/java/cc/fascinated/bat/features/moderation/command/MuteCommand.java @@ -49,6 +49,13 @@ public class MuteCommand extends BatCommand { OptionMapping lengthOption = event.getOption("length"); assert memberOption != null; BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); + if (targetUser == null) { + event.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("User not found") + .build() + ).queue(); + return; + } String reason = reasonOption == null ? null : reasonOption.getAsString(); long length = lengthOption == null ? -1 : TimeUtils.fromString(lengthOption.getAsString()); diff --git a/src/main/java/cc/fascinated/bat/features/moderation/command/PunishHistoryCommand.java b/src/main/java/cc/fascinated/bat/features/moderation/command/PunishHistoryCommand.java index 7392d52..9efe754 100644 --- a/src/main/java/cc/fascinated/bat/features/moderation/command/PunishHistoryCommand.java +++ b/src/main/java/cc/fascinated/bat/features/moderation/command/PunishHistoryCommand.java @@ -53,6 +53,13 @@ public class PunishHistoryCommand extends BatCommand { OptionMapping memberOption = event.getOption("member"); assert memberOption != null; BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); + if (targetUser == null) { + event.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("User not found") + .build() + ).queue(); + return; + } PunishmentProfile profile = guild.getPunishmentProfile(); List punishments = profile.getPunishments(targetUser); diff --git a/src/main/java/cc/fascinated/bat/features/moderation/command/PurgeCommand.java b/src/main/java/cc/fascinated/bat/features/moderation/command/PurgeCommand.java index 5159f3a..abee964 100644 --- a/src/main/java/cc/fascinated/bat/features/moderation/command/PurgeCommand.java +++ b/src/main/java/cc/fascinated/bat/features/moderation/command/PurgeCommand.java @@ -20,8 +20,6 @@ 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 net.dv8tion.jda.api.interactions.commands.build.OptionData; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; diff --git a/src/main/java/cc/fascinated/bat/features/moderation/command/UnbanCommand.java b/src/main/java/cc/fascinated/bat/features/moderation/command/UnbanCommand.java index 00f5d9a..cdfe274 100644 --- a/src/main/java/cc/fascinated/bat/features/moderation/command/UnbanCommand.java +++ b/src/main/java/cc/fascinated/bat/features/moderation/command/UnbanCommand.java @@ -46,7 +46,14 @@ public class UnbanCommand extends BatCommand { OptionMapping memberOption = event.getOption("member"); OptionMapping reasonOption = event.getOption("reason"); assert memberOption != null; - BatUser targetUser = userService.getUser(memberOption.getAsString()); + BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); + if (targetUser == null) { + event.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("User not found") + .build() + ).queue(); + return; + } String reason = reasonOption == null ? null : reasonOption.getAsString(); PunishmentProfile profile = guild.getPunishmentProfile(); diff --git a/src/main/java/cc/fascinated/bat/features/moderation/command/UnmuteCommand.java b/src/main/java/cc/fascinated/bat/features/moderation/command/UnmuteCommand.java index e059a82..a228f8a 100644 --- a/src/main/java/cc/fascinated/bat/features/moderation/command/UnmuteCommand.java +++ b/src/main/java/cc/fascinated/bat/features/moderation/command/UnmuteCommand.java @@ -47,6 +47,13 @@ public class UnmuteCommand extends BatCommand { OptionMapping reasonOption = event.getOption("reason"); assert memberOption != null; BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); + if (targetUser == null) { + event.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("User not found") + .build() + ).queue(); + return; + } String reason = reasonOption == null ? null : reasonOption.getAsString(); PunishmentProfile profile = guild.getPunishmentProfile(); diff --git a/src/main/java/cc/fascinated/bat/features/moderation/command/WarnCommand.java b/src/main/java/cc/fascinated/bat/features/moderation/command/WarnCommand.java index df4abd1..f97521c 100644 --- a/src/main/java/cc/fascinated/bat/features/moderation/command/WarnCommand.java +++ b/src/main/java/cc/fascinated/bat/features/moderation/command/WarnCommand.java @@ -46,6 +46,13 @@ public class WarnCommand extends BatCommand { OptionMapping reasonOption = event.getOption("reason"); assert memberOption != null; BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); + if (targetUser == null) { + event.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("User not found") + .build() + ).queue(); + return; + } String reason = reasonOption == null ? null : reasonOption.getAsString(); PunishmentProfile profile = guild.getPunishmentProfile();