13
13
import net .dv8tion .jda .api .entities .channel .unions .MessageChannelUnion ;
14
14
import net .dv8tion .jda .api .events .interaction .ModalInteractionEvent ;
15
15
import net .dv8tion .jda .api .events .interaction .command .MessageContextInteractionEvent ;
16
+ import net .dv8tion .jda .api .exceptions .ErrorResponseException ;
16
17
import net .dv8tion .jda .api .interactions .commands .build .Commands ;
17
18
import net .dv8tion .jda .api .interactions .components .Modal ;
18
19
import net .dv8tion .jda .api .interactions .components .text .TextInput ;
19
20
import net .dv8tion .jda .api .interactions .components .text .TextInput .Builder ;
20
21
import net .dv8tion .jda .api .interactions .components .text .TextInputStyle ;
22
+ import net .dv8tion .jda .api .requests .ErrorResponse ;
21
23
import net .dv8tion .jda .api .requests .RestAction ;
22
24
import net .dv8tion .jda .api .utils .messages .MessageCreateBuilder ;
23
25
import net .dv8tion .jda .api .utils .messages .MessageCreateData ;
26
+ import org .slf4j .Logger ;
27
+ import org .slf4j .LoggerFactory ;
24
28
25
29
import org .togetherjava .tjbot .config .Config ;
26
30
import org .togetherjava .tjbot .features .BotCommandAdapter ;
32
36
import java .util .List ;
33
37
import java .util .Objects ;
34
38
import java .util .Optional ;
39
+ import java .util .function .Consumer ;
35
40
import java .util .function .Predicate ;
36
41
import java .util .function .Supplier ;
37
42
import java .util .regex .Pattern ;
44
49
*/
45
50
public final class TransferQuestionCommand extends BotCommandAdapter
46
51
implements MessageContextCommand {
52
+ private static final Logger logger = LoggerFactory .getLogger (TransferQuestionCommand .class );
47
53
private static final String COMMAND_NAME = "transfer-question" ;
48
54
private static final String MODAL_TITLE_ID = "transferID" ;
49
55
private static final String MODAL_INPUT_ID = "transferQuestion" ;
@@ -125,6 +131,29 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
125
131
String authorId = args .get (0 );
126
132
String messageId = args .get (1 );
127
133
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 ) {
128
157
129
158
event .getJDA ()
130
159
.retrieveUserById (authorId )
@@ -135,6 +164,13 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
135
164
.queue ();
136
165
}
137
166
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
+
138
174
private static String createTitle (String message ) {
139
175
if (message .length () >= TITLE_MAX_LENGTH ) {
140
176
int lastWordEnd = message .lastIndexOf (' ' , TITLE_MAX_LENGTH );
0 commit comments