From f2c78251fd4dd9d54140d56f072af9fc25756cdd Mon Sep 17 00:00:00 2001 From: alphaBEE Date: Fri, 17 Nov 2023 09:00:08 +0530 Subject: [PATCH 1/4] refactor onModalSubmit to handle multiple users submission --- .../moderation/TransferQuestionCommand.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java index 9bfe2bcc6e..a1f11ab253 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java @@ -125,6 +125,19 @@ public void onModalSubmitted(ModalInteractionEvent event, List 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(); + + // to avoid race condition, when multiple users do the transfer. + // message is retrieved, already transferred messages are deleted. + event.getChannel() + .retrieveMessageById(messageId) + .queue(notHandled -> transferFlow(event, channelId, authorId, messageId), + handled -> alreadyHandled(sourceChannel, helperForum)); + } + + private void transferFlow(ModalInteractionEvent event, String channelId, String authorId, + String messageId) { event.getJDA() .retrieveUserById(authorId) @@ -135,6 +148,13 @@ public void onModalSubmitted(ModalInteractionEvent event, List args) { .queue(); } + private void alreadyHandled(TextChannel sourceChannel, ForumChannel helperForum) { + sourceChannel.sendMessage( + "The question has been already forwarded to the helper forum for assistance. Kindly review %s for more information." + .formatted(helperForum.getAsMention())) + .queue(); + } + private static String createTitle(String message) { if (message.length() >= TITLE_MAX_LENGTH) { int lastWordEnd = message.lastIndexOf(' ', TITLE_MAX_LENGTH); From e1bd604164be918c796c153c8b561f94ffa1a67c Mon Sep 17 00:00:00 2001 From: alphaBEE Date: Fri, 17 Nov 2023 20:39:29 +0530 Subject: [PATCH 2/4] refactor handling of already handled transfers --- .../moderation/TransferQuestionCommand.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java index a1f11ab253..1793d517a4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java @@ -32,6 +32,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; @@ -128,12 +129,13 @@ public void onModalSubmitted(ModalInteractionEvent event, List args) { ForumChannel helperForum = getHelperForum(event.getJDA()); TextChannel sourceChannel = event.getChannel().asTextChannel(); - // to avoid race condition, when multiple users do the transfer. - // message is retrieved, already transferred messages are deleted. - event.getChannel() - .retrieveMessageById(messageId) - .queue(notHandled -> transferFlow(event, channelId, authorId, messageId), - handled -> alreadyHandled(sourceChannel, helperForum)); + // Has been handled if original message was deleted by now. + // Deleted messages cause retrieveMessageById to fail. + Consumer notHandledAction = + any -> transferFlow(event, channelId, authorId, messageId); + Consumer handledAction = any -> alreadyHandled(sourceChannel, helperForum); + + event.getChannel().retrieveMessageById(messageId).queue(notHandledAction, handledAction); } private void transferFlow(ModalInteractionEvent event, String channelId, String authorId, @@ -150,7 +152,7 @@ private void transferFlow(ModalInteractionEvent event, String channelId, String private void alreadyHandled(TextChannel sourceChannel, ForumChannel helperForum) { sourceChannel.sendMessage( - "The question has been already forwarded to the helper forum for assistance. Kindly review %s for more information." + "It appears that someone else has already transferred this question. Kindly see %s for details." .formatted(helperForum.getAsMention())) .queue(); } From 11cebb3c2a9c6e51137811d4b5e608b79e295cbf Mon Sep 17 00:00:00 2001 From: alphaBEE Date: Fri, 17 Nov 2023 20:52:11 +0530 Subject: [PATCH 3/4] should only handle when message retrieval fails --- .../features/moderation/TransferQuestionCommand.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java index 1793d517a4..b2a14dc1d3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java @@ -13,11 +13,13 @@ 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; @@ -133,7 +135,13 @@ public void onModalSubmitted(ModalInteractionEvent event, List args) { // Deleted messages cause retrieveMessageById to fail. Consumer notHandledAction = any -> transferFlow(event, channelId, authorId, messageId); - Consumer handledAction = any -> alreadyHandled(sourceChannel, helperForum); + + Consumer handledAction = any -> { + if (any instanceof ErrorResponseException errorResponseException + && errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) { + alreadyHandled(sourceChannel, helperForum); + } + }; event.getChannel().retrieveMessageById(messageId).queue(notHandledAction, handledAction); } From cecc06635814e1d3c3e5750ee46c86726955ebb7 Mon Sep 17 00:00:00 2001 From: alphaBEE Date: Sat, 18 Nov 2023 14:59:46 +0530 Subject: [PATCH 4/4] refactor throwable name and logging error --- .../features/moderation/TransferQuestionCommand.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java index b2a14dc1d3..bc41a02729 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java @@ -23,6 +23,8 @@ 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; @@ -47,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"; @@ -136,11 +139,14 @@ public void onModalSubmitted(ModalInteractionEvent event, List args) { Consumer notHandledAction = any -> transferFlow(event, channelId, authorId, messageId); - Consumer handledAction = any -> { - if (any instanceof ErrorResponseException errorResponseException + Consumer 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);