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.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl; import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import org.codehaus.plexus.util.cli.Arg;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;

@ -4,10 +4,9 @@ import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.DescriptionBuilder; import cc.fascinated.bat.common.DescriptionBuilder;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.LongUtils;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.UserService;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User; 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.OptionType;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; 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.OptionData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -24,7 +24,11 @@ import org.springframework.stereotype.Component;
@Component("lookup.user:sub") @Component("lookup.user:sub")
@CommandInfo(name = "user", description = "Lookup a user") @CommandInfo(name = "user", description = "Lookup a user")
public class UserSubCommand extends BatCommand { 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)); 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) { if (idOption == null) {
return; return;
} }
String id = idOption.getAsString(); User target = userService.getUser(idOption.getAsString()).getDiscordUser();
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) { if (target == null) {
event.replyEmbeds(EmbedUtils.errorEmbed() event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("User `%s` not found".formatted(id)) .setDescription("User `%s` not found".formatted(idOption.getAsString()))
.build()) .build())
.setEphemeral(true) .setEphemeral(true)
.queue(); .queue();
@ -76,6 +70,5 @@ public class UserSubCommand extends BatCommand {
.build()) .build())
.setEphemeral(true) .setEphemeral(true)
.queue()); .queue());
});
} }
} }

@ -49,6 +49,13 @@ public class BanCommand extends BatCommand {
OptionMapping lengthOption = event.getOption("length"); OptionMapping lengthOption = event.getOption("length");
assert memberOption != null; assert memberOption != null;
BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); 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(); String reason = reasonOption == null ? null : reasonOption.getAsString();
long length = lengthOption == null ? -1 : TimeUtils.fromString(lengthOption.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.BatCommand;
import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.EmbedUtils; 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.PunishmentProfile;
import cc.fascinated.bat.features.moderation.punish.PunishmentType; import cc.fascinated.bat.features.moderation.punish.PunishmentType;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
@ -48,6 +47,13 @@ public class KickCommand extends BatCommand {
OptionMapping reasonOption = event.getOption("reason"); OptionMapping reasonOption = event.getOption("reason");
assert memberOption != null; assert memberOption != null;
BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); 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(); String reason = reasonOption == null ? null : reasonOption.getAsString();
PunishmentProfile profile = guild.getPunishmentProfile(); PunishmentProfile profile = guild.getPunishmentProfile();

@ -49,6 +49,13 @@ public class MuteCommand extends BatCommand {
OptionMapping lengthOption = event.getOption("length"); OptionMapping lengthOption = event.getOption("length");
assert memberOption != null; assert memberOption != null;
BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); 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(); String reason = reasonOption == null ? null : reasonOption.getAsString();
long length = lengthOption == null ? -1 : TimeUtils.fromString(lengthOption.getAsString()); long length = lengthOption == null ? -1 : TimeUtils.fromString(lengthOption.getAsString());

@ -53,6 +53,13 @@ public class PunishHistoryCommand extends BatCommand {
OptionMapping memberOption = event.getOption("member"); OptionMapping memberOption = event.getOption("member");
assert memberOption != null; assert memberOption != null;
BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); 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(); PunishmentProfile profile = guild.getPunishmentProfile();
List<Punishment> punishments = profile.getPunishments(targetUser); 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.OptionType;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; 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.OptionData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;

@ -46,7 +46,14 @@ public class UnbanCommand extends BatCommand {
OptionMapping memberOption = event.getOption("member"); OptionMapping memberOption = event.getOption("member");
OptionMapping reasonOption = event.getOption("reason"); OptionMapping reasonOption = event.getOption("reason");
assert memberOption != null; 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(); String reason = reasonOption == null ? null : reasonOption.getAsString();
PunishmentProfile profile = guild.getPunishmentProfile(); PunishmentProfile profile = guild.getPunishmentProfile();

@ -47,6 +47,13 @@ public class UnmuteCommand extends BatCommand {
OptionMapping reasonOption = event.getOption("reason"); OptionMapping reasonOption = event.getOption("reason");
assert memberOption != null; assert memberOption != null;
BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); 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(); String reason = reasonOption == null ? null : reasonOption.getAsString();
PunishmentProfile profile = guild.getPunishmentProfile(); PunishmentProfile profile = guild.getPunishmentProfile();

@ -46,6 +46,13 @@ public class WarnCommand extends BatCommand {
OptionMapping reasonOption = event.getOption("reason"); OptionMapping reasonOption = event.getOption("reason");
assert memberOption != null; assert memberOption != null;
BatUser targetUser = userService.getUser(memberOption.getAsUser().getId()); 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(); String reason = reasonOption == null ? null : reasonOption.getAsString();
PunishmentProfile profile = guild.getPunishmentProfile(); PunishmentProfile profile = guild.getPunishmentProfile();