From 407ee6f1e92e1dd3cf2a13b62ca92ce3d870e79c Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 06:55:56 +0100 Subject: [PATCH] log errors to a channel --- .../fascinated/bat/common/ChannelUtils.java | 2 ++ .../cc/fascinated/bat/common/EmbedUtils.java | 19 +++++++++++++++++-- .../cc/fascinated/bat/common/EnumUtils.java | 3 +++ .../fascinated/bat/common/HexColorUtils.java | 3 +++ .../cc/fascinated/bat/common/MemberUtils.java | 2 ++ .../bat/common/NumberFormatter.java | 3 +++ .../cc/fascinated/bat/common/PasteUtils.java | 2 ++ .../cc/fascinated/bat/common/RoleUtils.java | 2 ++ .../fascinated/bat/common/SpotifyUtils.java | 2 ++ .../cc/fascinated/bat/common/StringUtils.java | 3 +++ .../cc/fascinated/bat/common/UserUtils.java | 2 ++ .../java/cc/fascinated/bat/config/Config.java | 11 ++++++++++- src/main/resources/application.yml | 5 ++++- 13 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/common/ChannelUtils.java b/src/main/java/cc/fascinated/bat/common/ChannelUtils.java index af8ad23..e989fde 100644 --- a/src/main/java/cc/fascinated/bat/common/ChannelUtils.java +++ b/src/main/java/cc/fascinated/bat/common/ChannelUtils.java @@ -1,12 +1,14 @@ package cc.fascinated.bat.common; import cc.fascinated.bat.service.DiscordService; +import lombok.experimental.UtilityClass; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; /** * @author Fascinated (fascinated7) */ +@UtilityClass @Log4j2 public class ChannelUtils { /** diff --git a/src/main/java/cc/fascinated/bat/common/EmbedUtils.java b/src/main/java/cc/fascinated/bat/common/EmbedUtils.java index 1eceee5..7d86ba4 100644 --- a/src/main/java/cc/fascinated/bat/common/EmbedUtils.java +++ b/src/main/java/cc/fascinated/bat/common/EmbedUtils.java @@ -1,7 +1,9 @@ package cc.fascinated.bat.common; +import cc.fascinated.bat.config.Config; import lombok.experimental.UtilityClass; import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import java.time.LocalDateTime; @@ -50,8 +52,21 @@ public class EmbedUtils { * @return the embed builder */ public static EmbedBuilder genericInteractionError(Exception ex) { + TextChannel channel = ChannelUtils.getTextChannel(Config.INSTANCE.getLogsChannel()); + if (channel != null) { + channel.sendMessageEmbeds(EmbedUtils.errorEmbed() + .setDescription(""" + An error has occurred while processing an interaction. Please check the logs for more information. + ```java + %s + ```""".formatted(ex.getLocalizedMessage())) + .build()).queue(); + } return EmbedUtils.errorEmbed() - .setDescription("An error occurred while processing your interaction. Please try again later.\n" + - "**Error:** ```java\n%s\n```" + ex.getLocalizedMessage()); + .setDescription(""" + An error has occurred while processing your interaction. Please check the logs for more information. + ```java + %s + ```""".formatted(ex.getLocalizedMessage())); } } diff --git a/src/main/java/cc/fascinated/bat/common/EnumUtils.java b/src/main/java/cc/fascinated/bat/common/EnumUtils.java index be6b346..8d3e1af 100644 --- a/src/main/java/cc/fascinated/bat/common/EnumUtils.java +++ b/src/main/java/cc/fascinated/bat/common/EnumUtils.java @@ -1,8 +1,11 @@ package cc.fascinated.bat.common; +import lombok.experimental.UtilityClass; + /** * @author Fascinated (fascinated7) */ +@UtilityClass public class EnumUtils { /** * Gets the name of the enum diff --git a/src/main/java/cc/fascinated/bat/common/HexColorUtils.java b/src/main/java/cc/fascinated/bat/common/HexColorUtils.java index bb4bea3..edee965 100644 --- a/src/main/java/cc/fascinated/bat/common/HexColorUtils.java +++ b/src/main/java/cc/fascinated/bat/common/HexColorUtils.java @@ -1,10 +1,13 @@ package cc.fascinated.bat.common; +import lombok.experimental.UtilityClass; + import java.awt.*; /** * @author Fascinated (fascinated7) */ +@UtilityClass public class HexColorUtils { /** * Checks if the given string is a hex color diff --git a/src/main/java/cc/fascinated/bat/common/MemberUtils.java b/src/main/java/cc/fascinated/bat/common/MemberUtils.java index 287e288..f1a22b4 100644 --- a/src/main/java/cc/fascinated/bat/common/MemberUtils.java +++ b/src/main/java/cc/fascinated/bat/common/MemberUtils.java @@ -2,6 +2,7 @@ package cc.fascinated.bat.common; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; +import lombok.experimental.UtilityClass; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; @@ -10,6 +11,7 @@ import java.util.Comparator; /** * @author Fascinated (fascinated7) */ +@UtilityClass public class MemberUtils { /** * Checks if a user has permission to edit another user diff --git a/src/main/java/cc/fascinated/bat/common/NumberFormatter.java b/src/main/java/cc/fascinated/bat/common/NumberFormatter.java index fc8ecc3..ddc1a24 100644 --- a/src/main/java/cc/fascinated/bat/common/NumberFormatter.java +++ b/src/main/java/cc/fascinated/bat/common/NumberFormatter.java @@ -1,11 +1,14 @@ package cc.fascinated.bat.common; +import lombok.experimental.UtilityClass; + import java.text.DecimalFormat; import java.util.Locale; /** * @author Fascinated (fascinated7) */ +@UtilityClass public class NumberFormatter { /** * The suffixes for the numbers diff --git a/src/main/java/cc/fascinated/bat/common/PasteUtils.java b/src/main/java/cc/fascinated/bat/common/PasteUtils.java index 46343b0..4344705 100644 --- a/src/main/java/cc/fascinated/bat/common/PasteUtils.java +++ b/src/main/java/cc/fascinated/bat/common/PasteUtils.java @@ -3,6 +3,7 @@ package cc.fascinated.bat.common; import cc.fascinated.bat.BatApplication; import cc.fascinated.bat.model.token.paste.PasteUploadToken; import lombok.SneakyThrows; +import lombok.experimental.UtilityClass; import lombok.extern.log4j.Log4j2; import java.net.URI; @@ -13,6 +14,7 @@ import java.net.http.HttpResponse; /** * @author Fascinated (fascinated7) */ +@UtilityClass @Log4j2 public class PasteUtils { private static final String PASTE_URL = "https://paste.fascinated.cc/"; diff --git a/src/main/java/cc/fascinated/bat/common/RoleUtils.java b/src/main/java/cc/fascinated/bat/common/RoleUtils.java index 95294ad..8d69b62 100644 --- a/src/main/java/cc/fascinated/bat/common/RoleUtils.java +++ b/src/main/java/cc/fascinated/bat/common/RoleUtils.java @@ -1,12 +1,14 @@ package cc.fascinated.bat.common; import cc.fascinated.bat.model.BatGuild; +import lombok.experimental.UtilityClass; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; /** * @author Fascinated (fascinated7) */ +@UtilityClass public class RoleUtils { /** * Checks if a member has permission to give the role to another member diff --git a/src/main/java/cc/fascinated/bat/common/SpotifyUtils.java b/src/main/java/cc/fascinated/bat/common/SpotifyUtils.java index bb34694..860f18d 100644 --- a/src/main/java/cc/fascinated/bat/common/SpotifyUtils.java +++ b/src/main/java/cc/fascinated/bat/common/SpotifyUtils.java @@ -3,6 +3,7 @@ package cc.fascinated.bat.common; import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.service.SpotifyService; import lombok.NonNull; +import lombok.experimental.UtilityClass; import lombok.extern.log4j.Log4j2; import se.michaelthelin.spotify.model_objects.miscellaneous.CurrentlyPlaying; import se.michaelthelin.spotify.model_objects.specification.Track; @@ -10,6 +11,7 @@ import se.michaelthelin.spotify.model_objects.specification.Track; /** * @author Fascinated (fascinated7) */ +@UtilityClass @Log4j2 public class SpotifyUtils { /** diff --git a/src/main/java/cc/fascinated/bat/common/StringUtils.java b/src/main/java/cc/fascinated/bat/common/StringUtils.java index 2171e90..a2b84fc 100644 --- a/src/main/java/cc/fascinated/bat/common/StringUtils.java +++ b/src/main/java/cc/fascinated/bat/common/StringUtils.java @@ -1,8 +1,11 @@ package cc.fascinated.bat.common; +import lombok.experimental.UtilityClass; + /** * @author Fascinated (fascinated7) */ +@UtilityClass public class StringUtils { /** * Generates a random string diff --git a/src/main/java/cc/fascinated/bat/common/UserUtils.java b/src/main/java/cc/fascinated/bat/common/UserUtils.java index dfba7d6..daa0578 100644 --- a/src/main/java/cc/fascinated/bat/common/UserUtils.java +++ b/src/main/java/cc/fascinated/bat/common/UserUtils.java @@ -1,12 +1,14 @@ package cc.fascinated.bat.common; import cc.fascinated.bat.service.DiscordService; +import lombok.experimental.UtilityClass; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.entities.User; /** * @author Fascinated (fascinated7) */ +@UtilityClass @Log4j2 public class UserUtils { /** diff --git a/src/main/java/cc/fascinated/bat/config/Config.java b/src/main/java/cc/fascinated/bat/config/Config.java index 708b4bc..3e42f2d 100644 --- a/src/main/java/cc/fascinated/bat/config/Config.java +++ b/src/main/java/cc/fascinated/bat/config/Config.java @@ -23,6 +23,11 @@ public class Config { */ private final String adminGuild; + /** + * The ID of the logs channel + */ + private final String logsChannel; + static { // Are we running on production? String appEnv = System.getenv("APP_ENV"); @@ -30,8 +35,12 @@ public class Config { } @Autowired - public Config(@Value("${bat.admin-guild}") String adminGuild) { + public Config( + @Value("${bat.admin-guild}") String adminGuild, + @Value("${bat.logs-channel}") String logsChannel + ) { INSTANCE = this; this.adminGuild = adminGuild; + this.logsChannel = logsChannel; } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e3a87c4..20ea83a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -6,7 +6,10 @@ discord: bat: # This is where commands will be registered (whilst in development mode) # also where bot owner only commands will be registered - admin-guild: 1203163422498361404 + admin-guild: set me + + # This is the guild where the bot will log errors and other information + logs-channel: set me # Sentry Configuration sentry: