add user validation to moderation and to user lookup

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,20 +38,10 @@ public class UserSubCommand extends BatCommand {
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 -> {
User target = userService.getUser(idOption.getAsString()).getDiscordUser();
if (target == null) {
event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("User `%s` not found".formatted(id))
.setDescription("User `%s` not found".formatted(idOption.getAsString()))
.build())
.setEphemeral(true)
.queue();
@ -76,6 +70,5 @@ public class UserSubCommand extends BatCommand {
.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();