Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -125,11 +130,11 @@ private boolean handleIsValidTitle(@NotNull CharSequence title, @NotNull IReplyC
return false;
}

private @NotNull RestAction<Message> handleEvent(@NotNull IReplyCallback event,
private @NotNull RestAction<Message> 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));
}

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

private static @NotNull ReplyCallbackAction notifyUser(@NotNull IReplyCallback event,
private static @NotNull RestAction<Message> 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;
}
Expand Down