8
8
import net .dv8tion .jda .api .entities .User ;
9
9
import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
10
10
import net .dv8tion .jda .api .entities .channel .concrete .TextChannel ;
11
+ import net .dv8tion .jda .api .entities .channel .forums .ForumPost ;
11
12
import net .dv8tion .jda .api .entities .channel .forums .ForumTag ;
12
13
import net .dv8tion .jda .api .entities .channel .forums .ForumTagSnowflake ;
13
14
import net .dv8tion .jda .api .entities .channel .unions .MessageChannelUnion ;
21
22
import net .dv8tion .jda .api .interactions .components .text .TextInputStyle ;
22
23
import net .dv8tion .jda .api .requests .ErrorResponse ;
23
24
import net .dv8tion .jda .api .requests .RestAction ;
25
+ import net .dv8tion .jda .api .requests .restaction .WebhookMessageCreateAction ;
24
26
import net .dv8tion .jda .api .utils .messages .MessageCreateBuilder ;
25
27
import net .dv8tion .jda .api .utils .messages .MessageCreateData ;
26
28
import org .slf4j .Logger ;
37
39
import java .util .Objects ;
38
40
import java .util .Optional ;
39
41
import java .util .function .Consumer ;
42
+ import java .util .function .Function ;
40
43
import java .util .function .Predicate ;
41
44
import java .util .function .Supplier ;
42
45
import java .util .regex .Pattern ;
@@ -126,13 +129,12 @@ public void onMessageContext(MessageContextInteractionEvent event) {
126
129
127
130
@ Override
128
131
public void onModalSubmitted (ModalInteractionEvent event , List <String > args ) {
129
- event .deferEdit ( ).queue ();
132
+ event .deferReply ( true ).queue ();
130
133
131
134
String authorId = args .get (0 );
132
135
String messageId = args .get (1 );
133
136
String channelId = args .get (2 );
134
137
ForumChannel helperForum = getHelperForum (event .getJDA ());
135
- TextChannel sourceChannel = event .getChannel ().asTextChannel ();
136
138
137
139
// Has been handled if original message was deleted by now.
138
140
// Deleted messages cause retrieveMessageById to fail.
@@ -142,7 +144,7 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
142
144
Consumer <Throwable > handledAction = failure -> {
143
145
if (failure instanceof ErrorResponseException errorResponseException
144
146
&& errorResponseException .getErrorResponse () == ErrorResponse .UNKNOWN_MESSAGE ) {
145
- alreadyHandled (sourceChannel , helperForum );
147
+ alreadyHandled (event , helperForum );
146
148
return ;
147
149
}
148
150
logger .warn ("Unknown error occurred on modal submission during question transfer." ,
@@ -154,20 +156,25 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
154
156
155
157
private void transferFlow (ModalInteractionEvent event , String channelId , String authorId ,
156
158
String messageId ) {
159
+ Function <ForumPostData , WebhookMessageCreateAction <Message >> sendMessageToTransferrer =
160
+ post -> event .getHook ()
161
+ .sendMessage ("Transferred to %s"
162
+ .formatted (post .forumPost .getThreadChannel ().getAsMention ()));
157
163
158
164
event .getJDA ()
159
165
.retrieveUserById (authorId )
160
166
.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 )) )
163
169
.flatMap (dmSent -> deleteOriginalMessage (event .getJDA (), channelId , messageId ))
164
170
.queue ();
165
171
}
166
172
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 ()))
171
178
.queue ();
172
179
}
173
180
@@ -192,8 +199,8 @@ private static boolean isTitleValid(CharSequence title) {
192
199
&& titleCompact .length () <= TITLE_MAX_LENGTH ;
193
200
}
194
201
195
- private RestAction <ForumPost > createForumPost (ModalInteractionEvent event , User originalUser ) {
196
-
202
+ private RestAction <ForumPostData > createForumPost (ModalInteractionEvent event ,
203
+ User originalUser ) {
197
204
String originalMessage = event .getValue (MODAL_INPUT_ID ).getAsString ();
198
205
199
206
MessageEmbed embedForPost = makeEmbedForPost (originalUser , originalMessage );
@@ -217,11 +224,11 @@ private RestAction<ForumPost> createForumPost(ModalInteractionEvent event, User
217
224
218
225
return questionsForum .createForumPost (forumTitle , forumMessage )
219
226
.setTags (ForumTagSnowflake .fromId (tag .getId ()))
220
- .map (createdPost -> new ForumPost ( originalUser , createdPost . getMessage () ));
227
+ .map (createdPost -> new ForumPostData ( createdPost , originalUser ));
221
228
}
222
229
223
- private RestAction <Message > dmUser (MessageChannelUnion sourceChannel , ForumPost forumPost ,
224
- Guild guild ) {
230
+ private RestAction <Message > dmUser (MessageChannelUnion sourceChannel ,
231
+ ForumPostData forumPostData , Guild guild ) {
225
232
226
233
String messageTemplate =
227
234
"""
@@ -232,14 +239,14 @@ private RestAction<Message> dmUser(MessageChannelUnion sourceChannel, ForumPost
232
239
233
240
// Prevents discord from creating a distracting auto-preview for the link
234
241
String jumpUrlSuffix = " " ;
242
+ String postUrl = forumPostData .forumPost ().getMessage ().getJumpUrl () + jumpUrlSuffix ;
235
243
236
- String messageForDm = messageTemplate .formatted ("" , " on " + guild .getName (),
237
- forumPost .message .getJumpUrl () + jumpUrlSuffix );
244
+ String messageForDm = messageTemplate .formatted ("" , " on " + guild .getName (), postUrl );
238
245
239
- String messageOnDmFailure = messageTemplate . formatted ( " " + forumPost . author . getAsMention (),
240
- "" , forumPost . message . getJumpUrl () + jumpUrlSuffix );
246
+ String messageOnDmFailure =
247
+ messageTemplate . formatted ( " " + forumPostData . author . getAsMention (), "" , postUrl );
241
248
242
- return forumPost .author .openPrivateChannel ()
249
+ return forumPostData .author .openPrivateChannel ()
243
250
.flatMap (channel -> channel .sendMessage (messageForDm ))
244
251
.onErrorFlatMap (error -> sourceChannel .sendMessage (messageOnDmFailure ));
245
252
}
@@ -275,7 +282,7 @@ private MessageEmbed makeEmbedForPost(User originalUser, String originalMessage)
275
282
.build ();
276
283
}
277
284
278
- private record ForumPost ( User author , Message message ) {
285
+ private record ForumPostData ( ForumPost forumPost , User author ) {
279
286
}
280
287
281
288
private boolean isBotMessageTransfer (User author ) {
0 commit comments