Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor onModalSubmit in TransferQuestionCommand to handle multiple users submission #955

Merged
merged 4 commits into from
Nov 20, 2023
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 @@ -13,14 +13,18 @@
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.components.Modal;
import net.dv8tion.jda.api.interactions.components.text.TextInput;
import net.dv8tion.jda.api.interactions.components.text.TextInput.Builder;
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
import net.dv8tion.jda.api.requests.ErrorResponse;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.config.Config;
import org.togetherjava.tjbot.features.BotCommandAdapter;
Expand All @@ -32,6 +36,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Pattern;
Expand All @@ -44,6 +49,7 @@
*/
public final class TransferQuestionCommand extends BotCommandAdapter
implements MessageContextCommand {
private static final Logger logger = LoggerFactory.getLogger(TransferQuestionCommand.class);
private static final String COMMAND_NAME = "transfer-question";
private static final String MODAL_TITLE_ID = "transferID";
private static final String MODAL_INPUT_ID = "transferQuestion";
Expand Down Expand Up @@ -125,6 +131,29 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
String authorId = args.get(0);
String messageId = args.get(1);
String channelId = args.get(2);
ForumChannel helperForum = getHelperForum(event.getJDA());
TextChannel sourceChannel = event.getChannel().asTextChannel();

// Has been handled if original message was deleted by now.
// Deleted messages cause retrieveMessageById to fail.
Consumer<Message> notHandledAction =
any -> transferFlow(event, channelId, authorId, messageId);

Consumer<Throwable> handledAction = failure -> {
if (failure instanceof ErrorResponseException errorResponseException
&& errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
alreadyHandled(sourceChannel, helperForum);
return;
}
logger.warn("Unknown error occurred on modal submission during question transfer.",
failure);
};

event.getChannel().retrieveMessageById(messageId).queue(notHandledAction, handledAction);
}

private void transferFlow(ModalInteractionEvent event, String channelId, String authorId,
String messageId) {

event.getJDA()
.retrieveUserById(authorId)
Expand All @@ -135,6 +164,13 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
.queue();
}

private void alreadyHandled(TextChannel sourceChannel, ForumChannel helperForum) {
sourceChannel.sendMessage(
"It appears that someone else has already transferred this question. Kindly see %s for details."
.formatted(helperForum.getAsMention()))
.queue();
}

private static String createTitle(String message) {
if (message.length() >= TITLE_MAX_LENGTH) {
int lastWordEnd = message.lastIndexOf(' ', TITLE_MAX_LENGTH);
Expand Down
Loading