add user validation to moderation and to user lookup
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m18s

This commit is contained in:
Lee 2024-07-09 20:34:56 +01:00
parent 4ec65c8d6e
commit f1bc2b2aaa
10 changed files with 81 additions and 43 deletions

@ -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;

@ -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: <t:%s:R>".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: <t:%s:R>".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());
}
}

@ -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());

@ -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();

@ -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());

@ -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<Punishment> punishments = profile.getPunishments(targetUser);

@ -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;

@ -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();

@ -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();

@ -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();