use generic embed for interaction errors

This commit is contained in:
Lee 2024-07-04 03:15:53 +01:00
parent 750b8cbfea
commit 49beb864d2
3 changed files with 19 additions and 21 deletions

@ -42,4 +42,16 @@ public class EmbedUtils {
.setTimestamp(LocalDateTime.now()) .setTimestamp(LocalDateTime.now())
.setColor(Colors.SUCCESS); .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());
}
} }

@ -237,11 +237,7 @@ public class CommandService extends ListenerAdapter {
System.currentTimeMillis() - before); System.currentTimeMillis() - before);
} catch (Exception ex) { } catch (Exception ex) {
log.error("An error occurred while executing command \"{}\"", commandName, ex); log.error("An error occurred while executing command \"{}\"", commandName, ex);
event.replyEmbeds(EmbedUtils.errorEmbed() event.replyEmbeds(EmbedUtils.genericInteractionError(ex).build()).setEphemeral(true).queue();
.setDescription(ex.getLocalizedMessage())
.build())
.setEphemeral(true)
.queue();
} }
} }
} }

@ -147,14 +147,9 @@ public class EventService extends ListenerAdapter {
for (EventListener listener : LISTENERS) { for (EventListener listener : LISTENERS) {
listener.onButtonInteraction(guild, user, event); listener.onButtonInteraction(guild, user, event);
} }
} catch (Exception e) { } catch (Exception ex) {
log.error("An error occurred while handling a modal interaction.", e); log.error("An error occurred while handling a modal interaction.", ex);
if (event.isAcknowledged()) { event.replyEmbeds(EmbedUtils.genericInteractionError(ex).build()).setEphemeral(true).queue();
return;
}
event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("An error occurred while handling your interaction.\nError: ```java\n%s\n```".formatted(e.getMessage()))
.build()).queue();
} }
} }
@ -170,14 +165,9 @@ public class EventService extends ListenerAdapter {
for (EventListener listener : LISTENERS) { for (EventListener listener : LISTENERS) {
listener.onModalInteraction(guild, user, event); listener.onModalInteraction(guild, user, event);
} }
} catch (Exception e) { } catch (Exception ex) {
log.error("An error occurred while handling a modal interaction.", e); log.error("An error occurred while handling a modal interaction.", ex);
if (event.isAcknowledged()) { event.replyEmbeds(EmbedUtils.genericInteractionError(ex).build()).setEphemeral(true).queue();
return;
}
event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("An error occurred while handling your interaction.\nError: ```java\n%s\n```".formatted(e.getMessage()))
.build()).queue();
} }
} }