From 49beb864d29528331f3aab0ff888b5d9097b8cf1 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 03:15:53 +0100 Subject: [PATCH] use generic embed for interaction errors --- .../cc/fascinated/bat/common/EmbedUtils.java | 12 ++++++++++ .../bat/service/CommandService.java | 6 +---- .../fascinated/bat/service/EventService.java | 22 +++++-------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/common/EmbedUtils.java b/src/main/java/cc/fascinated/bat/common/EmbedUtils.java index ba49620..1eceee5 100644 --- a/src/main/java/cc/fascinated/bat/common/EmbedUtils.java +++ b/src/main/java/cc/fascinated/bat/common/EmbedUtils.java @@ -42,4 +42,16 @@ public class EmbedUtils { .setTimestamp(LocalDateTime.now()) .setColor(Colors.SUCCESS); } + + /** + * Builds a generic interaction error embed + * + * @param ex the exception + * @return the embed builder + */ + public static EmbedBuilder genericInteractionError(Exception ex) { + return EmbedUtils.errorEmbed() + .setDescription("An error occurred while processing your interaction. Please try again later.\n" + + "**Error:** ```java\n%s\n```" + ex.getLocalizedMessage()); + } } diff --git a/src/main/java/cc/fascinated/bat/service/CommandService.java b/src/main/java/cc/fascinated/bat/service/CommandService.java index 5a0db14..111f43f 100644 --- a/src/main/java/cc/fascinated/bat/service/CommandService.java +++ b/src/main/java/cc/fascinated/bat/service/CommandService.java @@ -237,11 +237,7 @@ public class CommandService extends ListenerAdapter { System.currentTimeMillis() - before); } catch (Exception ex) { log.error("An error occurred while executing command \"{}\"", commandName, ex); - event.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription(ex.getLocalizedMessage()) - .build()) - .setEphemeral(true) - .queue(); + event.replyEmbeds(EmbedUtils.genericInteractionError(ex).build()).setEphemeral(true).queue(); } } } \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/service/EventService.java b/src/main/java/cc/fascinated/bat/service/EventService.java index 773d52b..de20c46 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -147,14 +147,9 @@ public class EventService extends ListenerAdapter { for (EventListener listener : LISTENERS) { listener.onButtonInteraction(guild, user, event); } - } catch (Exception e) { - log.error("An error occurred while handling a modal interaction.", e); - if (event.isAcknowledged()) { - return; - } - event.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("An error occurred while handling your interaction.\nError: ```java\n%s\n```".formatted(e.getMessage())) - .build()).queue(); + } catch (Exception ex) { + log.error("An error occurred while handling a modal interaction.", ex); + event.replyEmbeds(EmbedUtils.genericInteractionError(ex).build()).setEphemeral(true).queue(); } } @@ -170,14 +165,9 @@ public class EventService extends ListenerAdapter { for (EventListener listener : LISTENERS) { listener.onModalInteraction(guild, user, event); } - } catch (Exception e) { - log.error("An error occurred while handling a modal interaction.", e); - if (event.isAcknowledged()) { - return; - } - event.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("An error occurred while handling your interaction.\nError: ```java\n%s\n```".formatted(e.getMessage())) - .build()).queue(); + } catch (Exception ex) { + log.error("An error occurred while handling a modal interaction.", ex); + event.replyEmbeds(EmbedUtils.genericInteractionError(ex).build()).setEphemeral(true).queue(); } }