From 4ec65c8d6ecb46c0be500f5cf150343c174388b0 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 9 Jul 2024 20:24:50 +0100 Subject: [PATCH] validate id when getting user --- .../features/moderation/punish/PunishmentProfile.java | 4 ++-- .../java/cc/fascinated/bat/service/UserService.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/features/moderation/punish/PunishmentProfile.java b/src/main/java/cc/fascinated/bat/features/moderation/punish/PunishmentProfile.java index f67392c..1c32d22 100644 --- a/src/main/java/cc/fascinated/bat/features/moderation/punish/PunishmentProfile.java +++ b/src/main/java/cc/fascinated/bat/features/moderation/punish/PunishmentProfile.java @@ -85,9 +85,9 @@ public class PunishmentProfile extends Serializable { discordGuild.getName() ), false); if (length != -1 && (type == PunishmentType.MUTE || type == PunishmentType.BAN)) { - descriptionBuilder.appendLine("Length: %s".formatted(lengthFormatted), true); + descriptionBuilder.appendLine("Length: `%s`".formatted(lengthFormatted), true); } - descriptionBuilder.appendLine("Reason: %s".formatted(reason), true); + descriptionBuilder.appendLine("Reason: `%s`".formatted(reason), true); try { channel.sendMessage(descriptionBuilder.build()).complete(); } catch (Exception ignored) { diff --git a/src/main/java/cc/fascinated/bat/service/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index 5c1ccef..4fd539c 100644 --- a/src/main/java/cc/fascinated/bat/service/UserService.java +++ b/src/main/java/cc/fascinated/bat/service/UserService.java @@ -11,6 +11,7 @@ import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent; +import net.dv8tion.jda.api.utils.MiscUtil; import net.jodah.expiringmap.ExpirationPolicy; import net.jodah.expiringmap.ExpiringMap; import org.bson.Document; @@ -88,6 +89,16 @@ public class UserService implements EventListener { * @return The user */ public BatUser getUser(@NonNull String id) { + if (users.containsKey(id)) { + return users.get(id); + } + // Check if the ID is valid + try { + MiscUtil.parseSnowflake(id); + } catch (Exception ex) { + return null; + } + User user = DiscordService.JDA.retrieveUserById(id).complete(); if (user == null || user.isBot()) { return null;