Skip to content

Commit ea59cf1

Browse files
authored
Defered thread creation to avoid crash when hitting rate limits (#465)
1 parent 4d10b42 commit ea59cf1

File tree

1 file changed

+19
-15
lines changed
  • application/src/main/java/org/togetherjava/tjbot/commands/help

1 file changed

+19
-15
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import net.dv8tion.jda.api.entities.*;
44
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
55
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
6+
import net.dv8tion.jda.api.interactions.InteractionHook;
67
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
78
import net.dv8tion.jda.api.interactions.commands.OptionType;
89
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
910
import net.dv8tion.jda.api.requests.ErrorResponse;
1011
import net.dv8tion.jda.api.requests.RestAction;
11-
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
1212
import org.jetbrains.annotations.NotNull;
1313
import org.slf4j.Logger;
1414
import org.slf4j.LoggerFactory;
@@ -92,11 +92,16 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
9292
}
9393
TextChannel overviewChannel = maybeOverviewChannel.orElseThrow();
9494

95+
InteractionHook eventHook = event.getHook();
96+
Member author = event.getMember();
97+
Guild guild = event.getGuild();
98+
event.deferReply(true).queue();
99+
95100
overviewChannel.createThreadChannel("[%s] %s".formatted(category, title))
96-
.flatMap(threadChannel -> handleEvent(event, threadChannel, event.getMember(), title,
97-
category))
101+
.flatMap(threadChannel -> handleEvent(eventHook, threadChannel, author, title, category,
102+
guild))
98103
.queue(any -> {
99-
}, e -> handleFailure(e, event));
104+
}, e -> handleFailure(e, eventHook));
100105
}
101106

102107
private boolean handleIsStagingChannel(@NotNull IReplyCallback event) {
@@ -125,11 +130,11 @@ private boolean handleIsValidTitle(@NotNull CharSequence title, @NotNull IReplyC
125130
return false;
126131
}
127132

128-
private @NotNull RestAction<Message> handleEvent(@NotNull IReplyCallback event,
133+
private @NotNull RestAction<Message> handleEvent(@NotNull InteractionHook eventHook,
129134
@NotNull ThreadChannel threadChannel, @NotNull Member author, @NotNull String title,
130-
@NotNull String category) {
131-
return sendInitialMessage(event.getGuild(), threadChannel, author, title, category)
132-
.flatMap(any -> notifyUser(event, threadChannel))
135+
@NotNull String category, @NotNull Guild guild) {
136+
return sendInitialMessage(guild, threadChannel, author, title, category)
137+
.flatMap(any -> notifyUser(eventHook, threadChannel))
133138
.flatMap(any -> helper.sendExplanationMessage(threadChannel));
134139
}
135140

@@ -153,22 +158,21 @@ private RestAction<Message> sendInitialMessage(@NotNull Guild guild,
153158
.flatMap(message -> message.editMessage(contentWithRole));
154159
}
155160

156-
private static @NotNull ReplyCallbackAction notifyUser(@NotNull IReplyCallback event,
161+
private static @NotNull RestAction<Message> notifyUser(@NotNull InteractionHook eventHook,
157162
@NotNull IMentionable threadChannel) {
158-
return event.reply("""
163+
return eventHook.editOriginal("""
159164
Created a thread for you: %s
160-
Please ask your question there, thanks.""".formatted(threadChannel.getAsMention()))
161-
.setEphemeral(true);
165+
Please ask your question there, thanks.""".formatted(threadChannel.getAsMention()));
162166
}
163167

164-
private static void handleFailure(@NotNull Throwable exception, @NotNull IReplyCallback event) {
168+
private static void handleFailure(@NotNull Throwable exception,
169+
@NotNull InteractionHook eventHook) {
165170
if (exception instanceof ErrorResponseException responseException) {
166171
ErrorResponse response = responseException.getErrorResponse();
167172
if (response == ErrorResponse.MAX_CHANNELS
168173
|| response == ErrorResponse.MAX_ACTIVE_THREADS) {
169-
event.reply(
174+
eventHook.editOriginal(
170175
"It seems that there are currently too many active questions, please try again in a few minutes.")
171-
.setEphemeral(true)
172176
.queue();
173177
return;
174178
}

0 commit comments

Comments
 (0)