Skip to content

Commit 1a5a344

Browse files
Taz03ankitsmt211
andauthored
used ephemeral response as message to user during question transfer (#960)
* used ephemeral response * spotless * fixed response * removed unused import * removed extra params from record * defer reply because of delay in interaction * refactor url to for post to be sent to OP for clarity * sonar fix * refactor record ForumPost -> ForumPostData due to conflicting names * send message via hook * fixed name --------- Co-authored-by: alphaBEE <61616007+ankitsmt211@users.noreply.github.com>
1 parent f4b5ebf commit 1a5a344

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.dv8tion.jda.api.entities.User;
99
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
1010
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
11+
import net.dv8tion.jda.api.entities.channel.forums.ForumPost;
1112
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
1213
import net.dv8tion.jda.api.entities.channel.forums.ForumTagSnowflake;
1314
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
@@ -21,6 +22,7 @@
2122
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
2223
import net.dv8tion.jda.api.requests.ErrorResponse;
2324
import net.dv8tion.jda.api.requests.RestAction;
25+
import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction;
2426
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
2527
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
2628
import org.slf4j.Logger;
@@ -37,6 +39,7 @@
3739
import java.util.Objects;
3840
import java.util.Optional;
3941
import java.util.function.Consumer;
42+
import java.util.function.Function;
4043
import java.util.function.Predicate;
4144
import java.util.function.Supplier;
4245
import java.util.regex.Pattern;
@@ -126,13 +129,12 @@ public void onMessageContext(MessageContextInteractionEvent event) {
126129

127130
@Override
128131
public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
129-
event.deferEdit().queue();
132+
event.deferReply(true).queue();
130133

131134
String authorId = args.get(0);
132135
String messageId = args.get(1);
133136
String channelId = args.get(2);
134137
ForumChannel helperForum = getHelperForum(event.getJDA());
135-
TextChannel sourceChannel = event.getChannel().asTextChannel();
136138

137139
// Has been handled if original message was deleted by now.
138140
// Deleted messages cause retrieveMessageById to fail.
@@ -142,7 +144,7 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
142144
Consumer<Throwable> handledAction = failure -> {
143145
if (failure instanceof ErrorResponseException errorResponseException
144146
&& errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
145-
alreadyHandled(sourceChannel, helperForum);
147+
alreadyHandled(event, helperForum);
146148
return;
147149
}
148150
logger.warn("Unknown error occurred on modal submission during question transfer.",
@@ -154,20 +156,25 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
154156

155157
private void transferFlow(ModalInteractionEvent event, String channelId, String authorId,
156158
String messageId) {
159+
Function<ForumPostData, WebhookMessageCreateAction<Message>> sendMessageToTransferrer =
160+
post -> event.getHook()
161+
.sendMessage("Transferred to %s"
162+
.formatted(post.forumPost.getThreadChannel().getAsMention()));
157163

158164
event.getJDA()
159165
.retrieveUserById(authorId)
160166
.flatMap(fetchedUser -> createForumPost(event, fetchedUser))
161-
.flatMap(createdforumPost -> dmUser(event.getChannel(), createdforumPost,
162-
event.getGuild()))
167+
.flatMap(createdForumPost -> dmUser(event.getChannel(), createdForumPost,
168+
event.getGuild()).and(sendMessageToTransferrer.apply(createdForumPost)))
163169
.flatMap(dmSent -> deleteOriginalMessage(event.getJDA(), channelId, messageId))
164170
.queue();
165171
}
166172

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()))
173+
private void alreadyHandled(ModalInteractionEvent event, ForumChannel helperForum) {
174+
event.getHook()
175+
.sendMessage(
176+
"It appears that someone else has already transferred this question. Kindly see %s for details."
177+
.formatted(helperForum.getAsMention()))
171178
.queue();
172179
}
173180

@@ -192,8 +199,8 @@ private static boolean isTitleValid(CharSequence title) {
192199
&& titleCompact.length() <= TITLE_MAX_LENGTH;
193200
}
194201

195-
private RestAction<ForumPost> createForumPost(ModalInteractionEvent event, User originalUser) {
196-
202+
private RestAction<ForumPostData> createForumPost(ModalInteractionEvent event,
203+
User originalUser) {
197204
String originalMessage = event.getValue(MODAL_INPUT_ID).getAsString();
198205

199206
MessageEmbed embedForPost = makeEmbedForPost(originalUser, originalMessage);
@@ -217,11 +224,11 @@ private RestAction<ForumPost> createForumPost(ModalInteractionEvent event, User
217224

218225
return questionsForum.createForumPost(forumTitle, forumMessage)
219226
.setTags(ForumTagSnowflake.fromId(tag.getId()))
220-
.map(createdPost -> new ForumPost(originalUser, createdPost.getMessage()));
227+
.map(createdPost -> new ForumPostData(createdPost, originalUser));
221228
}
222229

223-
private RestAction<Message> dmUser(MessageChannelUnion sourceChannel, ForumPost forumPost,
224-
Guild guild) {
230+
private RestAction<Message> dmUser(MessageChannelUnion sourceChannel,
231+
ForumPostData forumPostData, Guild guild) {
225232

226233
String messageTemplate =
227234
"""
@@ -232,14 +239,14 @@ private RestAction<Message> dmUser(MessageChannelUnion sourceChannel, ForumPost
232239

233240
// Prevents discord from creating a distracting auto-preview for the link
234241
String jumpUrlSuffix = " ";
242+
String postUrl = forumPostData.forumPost().getMessage().getJumpUrl() + jumpUrlSuffix;
235243

236-
String messageForDm = messageTemplate.formatted("", " on " + guild.getName(),
237-
forumPost.message.getJumpUrl() + jumpUrlSuffix);
244+
String messageForDm = messageTemplate.formatted("", " on " + guild.getName(), postUrl);
238245

239-
String messageOnDmFailure = messageTemplate.formatted(" " + forumPost.author.getAsMention(),
240-
"", forumPost.message.getJumpUrl() + jumpUrlSuffix);
246+
String messageOnDmFailure =
247+
messageTemplate.formatted(" " + forumPostData.author.getAsMention(), "", postUrl);
241248

242-
return forumPost.author.openPrivateChannel()
249+
return forumPostData.author.openPrivateChannel()
243250
.flatMap(channel -> channel.sendMessage(messageForDm))
244251
.onErrorFlatMap(error -> sourceChannel.sendMessage(messageOnDmFailure));
245252
}
@@ -275,7 +282,7 @@ private MessageEmbed makeEmbedForPost(User originalUser, String originalMessage)
275282
.build();
276283
}
277284

278-
private record ForumPost(User author, Message message) {
285+
private record ForumPostData(ForumPost forumPost, User author) {
279286
}
280287

281288
private boolean isBotMessageTransfer(User author) {

0 commit comments

Comments
 (0)