Skip to content

Commit

Permalink
Support deferred interactions in CommandErrorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Mar 17, 2024
1 parent ce81962 commit 7455258
Showing 1 changed file with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.jagrosh.jdautilities.command.UserContextMenu;
import com.jagrosh.jdautilities.command.UserContextMenuEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import org.geysermc.discordbot.util.BotColors;
import pw.chew.chewbotcca.listeners.BotCommandListener;

Expand All @@ -47,15 +48,19 @@ public class CommandErrorHandler extends BotCommandListener implements CommandLi
public void onSlashCommandException(SlashCommandEvent event, SlashCommand command, Throwable throwable) {
String errorMessage = getErrorMessage(throwable);

event.replyEmbeds(new EmbedBuilder()
MessageEmbed errorEmbed = new EmbedBuilder()
.setTitle("Error handling command")
.setDescription("An error occurred while handling the command")
.addField("Command usage", "/" + command.getName() + (command.getArguments() != null ? " " + command.getArguments() : ""), false)
.addField("Command help", command.getHelp(), false)
.addField("Error", errorMessage, false)
.setTimestamp(Instant.now())
.setColor(BotColors.FAILURE.getColor())
.build()).setEphemeral(true).queue();
.build();

event.replyEmbeds(errorEmbed).setEphemeral(true).queue(message -> {}, throwable1 -> {
event.getInteraction().getHook().editOriginalEmbeds(errorEmbed).queue();
});

super.onSlashCommandException(event, command, throwable);
}
Expand All @@ -81,14 +86,18 @@ public void onCommandException(CommandEvent event, Command command, Throwable th
public void onMessageContextMenuException(MessageContextMenuEvent event, MessageContextMenu menu, Throwable throwable) {
String errorMessage = getErrorMessage(throwable);

event.replyEmbeds(new EmbedBuilder()
MessageEmbed errorEmbed = new EmbedBuilder()
.setTitle("Error handling context menu option")
.setDescription("An error occurred while handling the message context menu option")
.addField("Menu option", menu.getName(), false)
.addField("Error", errorMessage, false)
.setTimestamp(Instant.now())
.setColor(BotColors.FAILURE.getColor())
.build()).setEphemeral(true).queue();
.build();

event.replyEmbeds(errorEmbed).setEphemeral(true).queue(message -> {}, throwable1 -> {
event.getInteraction().getHook().editOriginalEmbeds(errorEmbed).queue();
});

super.onMessageContextMenuException(event, menu, throwable);
}
Expand All @@ -97,14 +106,18 @@ public void onMessageContextMenuException(MessageContextMenuEvent event, Message
public void onUserContextMenuException(UserContextMenuEvent event, UserContextMenu menu, Throwable throwable) {
String errorMessage = getErrorMessage(throwable);

event.replyEmbeds(new EmbedBuilder()
MessageEmbed errorEmbed = new EmbedBuilder()
.setTitle("Error handling context menu option")
.setDescription("An error occurred while handling the user context menu option")
.addField("Menu option", menu.getName(), false)
.addField("Error", errorMessage, false)
.setTimestamp(Instant.now())
.setColor(BotColors.FAILURE.getColor())
.build()).setEphemeral(true).queue();
.build();

event.replyEmbeds(errorEmbed).setEphemeral(true).queue(message -> {}, throwable1 -> {
event.getInteraction().getHook().editOriginalEmbeds(errorEmbed).queue();
});

super.onUserContextMenuException(event, menu, throwable);
}
Expand Down

0 comments on commit 7455258

Please sign in to comment.