Skip to content

Commit 1d1f09a

Browse files
authored
refactor onModalSubmit in TransferQuestionCommand to handle invocation of feature by multiple users (#955)
* refactor onModalSubmit to handle multiple users submission * refactor handling of already handled transfers * should only handle when message retrieval fails * refactor throwable name and logging error on failed retrieval
1 parent 99201a8 commit 1d1f09a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@
1313
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
1414
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
1515
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
16+
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
1617
import net.dv8tion.jda.api.interactions.commands.build.Commands;
1718
import net.dv8tion.jda.api.interactions.components.Modal;
1819
import net.dv8tion.jda.api.interactions.components.text.TextInput;
1920
import net.dv8tion.jda.api.interactions.components.text.TextInput.Builder;
2021
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
22+
import net.dv8tion.jda.api.requests.ErrorResponse;
2123
import net.dv8tion.jda.api.requests.RestAction;
2224
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
2325
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2428

2529
import org.togetherjava.tjbot.config.Config;
2630
import org.togetherjava.tjbot.features.BotCommandAdapter;
@@ -32,6 +36,7 @@
3236
import java.util.List;
3337
import java.util.Objects;
3438
import java.util.Optional;
39+
import java.util.function.Consumer;
3540
import java.util.function.Predicate;
3641
import java.util.function.Supplier;
3742
import java.util.regex.Pattern;
@@ -44,6 +49,7 @@
4449
*/
4550
public final class TransferQuestionCommand extends BotCommandAdapter
4651
implements MessageContextCommand {
52+
private static final Logger logger = LoggerFactory.getLogger(TransferQuestionCommand.class);
4753
private static final String COMMAND_NAME = "transfer-question";
4854
private static final String MODAL_TITLE_ID = "transferID";
4955
private static final String MODAL_INPUT_ID = "transferQuestion";
@@ -125,6 +131,29 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
125131
String authorId = args.get(0);
126132
String messageId = args.get(1);
127133
String channelId = args.get(2);
134+
ForumChannel helperForum = getHelperForum(event.getJDA());
135+
TextChannel sourceChannel = event.getChannel().asTextChannel();
136+
137+
// Has been handled if original message was deleted by now.
138+
// Deleted messages cause retrieveMessageById to fail.
139+
Consumer<Message> notHandledAction =
140+
any -> transferFlow(event, channelId, authorId, messageId);
141+
142+
Consumer<Throwable> handledAction = failure -> {
143+
if (failure instanceof ErrorResponseException errorResponseException
144+
&& errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
145+
alreadyHandled(sourceChannel, helperForum);
146+
return;
147+
}
148+
logger.warn("Unknown error occurred on modal submission during question transfer.",
149+
failure);
150+
};
151+
152+
event.getChannel().retrieveMessageById(messageId).queue(notHandledAction, handledAction);
153+
}
154+
155+
private void transferFlow(ModalInteractionEvent event, String channelId, String authorId,
156+
String messageId) {
128157

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

167+
private void alreadyHandled(TextChannel sourceChannel, ForumChannel helperForum) {
168+
sourceChannel.sendMessage(
169+
"It appears that someone else has already transferred this question. Kindly see %s for details."
170+
.formatted(helperForum.getAsMention()))
171+
.queue();
172+
}
173+
138174
private static String createTitle(String message) {
139175
if (message.length() >= TITLE_MAX_LENGTH) {
140176
int lastWordEnd = message.lastIndexOf(' ', TITLE_MAX_LENGTH);

0 commit comments

Comments
 (0)