From bc469299374379bf1dd50fe8ab4040ca8eba91fc Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Sat, 9 Jul 2022 15:13:42 +0200 Subject: [PATCH] Defered thread creation to avoid crash when hitting rate limits --- .../tjbot/commands/help/AskCommand.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java index 3d9f72270b..63f0b23951 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java @@ -3,12 +3,12 @@ import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.exceptions.ErrorResponseException; +import net.dv8tion.jda.api.interactions.InteractionHook; import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -92,11 +92,16 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { } TextChannel overviewChannel = maybeOverviewChannel.orElseThrow(); + InteractionHook eventHook = event.getHook(); + Member author = event.getMember(); + Guild guild = event.getGuild(); + event.deferReply(true).queue(); + overviewChannel.createThreadChannel("[%s] %s".formatted(category, title)) - .flatMap(threadChannel -> handleEvent(event, threadChannel, event.getMember(), title, - category)) + .flatMap(threadChannel -> handleEvent(eventHook, threadChannel, author, title, category, + guild)) .queue(any -> { - }, e -> handleFailure(e, event)); + }, e -> handleFailure(e, eventHook)); } private boolean handleIsStagingChannel(@NotNull IReplyCallback event) { @@ -125,11 +130,11 @@ private boolean handleIsValidTitle(@NotNull CharSequence title, @NotNull IReplyC return false; } - private @NotNull RestAction handleEvent(@NotNull IReplyCallback event, + private @NotNull RestAction handleEvent(@NotNull InteractionHook eventHook, @NotNull ThreadChannel threadChannel, @NotNull Member author, @NotNull String title, - @NotNull String category) { - return sendInitialMessage(event.getGuild(), threadChannel, author, title, category) - .flatMap(any -> notifyUser(event, threadChannel)) + @NotNull String category, @NotNull Guild guild) { + return sendInitialMessage(guild, threadChannel, author, title, category) + .flatMap(any -> notifyUser(eventHook, threadChannel)) .flatMap(any -> helper.sendExplanationMessage(threadChannel)); } @@ -153,22 +158,21 @@ private RestAction sendInitialMessage(@NotNull Guild guild, .flatMap(message -> message.editMessage(contentWithRole)); } - private static @NotNull ReplyCallbackAction notifyUser(@NotNull IReplyCallback event, + private static @NotNull RestAction notifyUser(@NotNull InteractionHook eventHook, @NotNull IMentionable threadChannel) { - return event.reply(""" + return eventHook.editOriginal(""" Created a thread for you: %s - Please ask your question there, thanks.""".formatted(threadChannel.getAsMention())) - .setEphemeral(true); + Please ask your question there, thanks.""".formatted(threadChannel.getAsMention())); } - private static void handleFailure(@NotNull Throwable exception, @NotNull IReplyCallback event) { + private static void handleFailure(@NotNull Throwable exception, + @NotNull InteractionHook eventHook) { if (exception instanceof ErrorResponseException responseException) { ErrorResponse response = responseException.getErrorResponse(); if (response == ErrorResponse.MAX_CHANNELS || response == ErrorResponse.MAX_ACTIVE_THREADS) { - event.reply( + eventHook.editOriginal( "It seems that there are currently too many active questions, please try again in a few minutes.") - .setEphemeral(true) .queue(); return; }