8
8
import net .dv8tion .jda .api .entities .Role ;
9
9
import net .dv8tion .jda .api .entities .User ;
10
10
import net .dv8tion .jda .api .entities .channel .concrete .TextChannel ;
11
+ import net .dv8tion .jda .api .events .interaction .ModalInteractionEvent ;
11
12
import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
12
13
import net .dv8tion .jda .api .interactions .InteractionHook ;
13
14
import net .dv8tion .jda .api .interactions .commands .Command ;
14
15
import net .dv8tion .jda .api .interactions .commands .OptionType ;
15
16
import net .dv8tion .jda .api .interactions .commands .build .OptionData ;
17
+ import net .dv8tion .jda .api .interactions .components .Modal ;
18
+ import net .dv8tion .jda .api .interactions .components .text .TextInput ;
19
+ import net .dv8tion .jda .api .interactions .components .text .TextInputStyle ;
16
20
import net .dv8tion .jda .api .requests .restaction .MessageCreateAction ;
17
21
import org .jetbrains .annotations .Nullable ;
18
22
import org .slf4j .Logger ;
@@ -64,8 +68,6 @@ public ModMailCommand(JDA jda, Config config) {
64
68
super (COMMAND_NAME , "Contact the moderators of the selected guild" ,
65
69
CommandVisibility .GLOBAL );
66
70
67
- OptionData messageOption = new OptionData (OptionType .STRING , OPTION_MESSAGE ,
68
- "What do you want to tell them?" , true );
69
71
OptionData guildOption = new OptionData (OptionType .STRING , OPTION_GUILD ,
70
72
"The server to contact mods from" , true );
71
73
OptionData anonymousOption = new OptionData (OptionType .BOOLEAN , OPTION_STAY_ANONYMOUS ,
@@ -78,7 +80,7 @@ public ModMailCommand(JDA jda, Config config) {
78
80
79
81
guildOption .addChoices (choices );
80
82
81
- getData ().addOptions (messageOption , guildOption , anonymousOption );
83
+ getData ().addOptions (guildOption , anonymousOption );
82
84
83
85
modMailChannelNamePredicate =
84
86
Pattern .compile (config .getModMailChannelPattern ()).asMatchPredicate ();
@@ -104,9 +106,38 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
104
106
return ;
105
107
}
106
108
authorToLastModMailInvocation .put (userId , Instant .now ());
107
- event .deferReply ().setEphemeral (true ).queue ();
108
109
110
+ sendMessageModal (event );
111
+ }
112
+
113
+ private void sendMessageModal (SlashCommandInteractionEvent event ) {
109
114
long userGuildId = event .getOption (OPTION_GUILD ).getAsLong ();
115
+ boolean wantsToStayAnonymous = event .getOption (OPTION_STAY_ANONYMOUS ).getAsBoolean ();
116
+
117
+ TextInput message =
118
+ TextInput .create (OPTION_MESSAGE , "Your message" , TextInputStyle .PARAGRAPH )
119
+ .setPlaceholder ("What do you want to tell them?" )
120
+ .setMinLength (3 )
121
+ .build ();
122
+
123
+ String componentId = generateComponentId (String .valueOf (userGuildId ),
124
+ String .valueOf (wantsToStayAnonymous ));
125
+
126
+ Modal modal = Modal .create (componentId , "Send message to moderators" )
127
+ .addActionRow (message )
128
+ .build ();
129
+
130
+ event .replyModal (modal ).queue ();
131
+ }
132
+
133
+ @ Override
134
+ public void onModalSubmitted (ModalInteractionEvent event , List <String > args ) {
135
+ String userMessage = event .getValue (OPTION_MESSAGE ).getAsString ();
136
+ long userId = event .getUser ().getIdLong ();
137
+
138
+ long userGuildId = Long .parseLong (args .get (0 ));
139
+ boolean wantsToStayAnonymous = Boolean .parseBoolean (args .get (1 ));
140
+
110
141
Optional <TextChannel > modMailAuditLog = getModMailChannel (event .getJDA (), userGuildId );
111
142
if (modMailAuditLog .isEmpty ()) {
112
143
logger .warn (
@@ -115,11 +146,11 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
115
146
return ;
116
147
}
117
148
118
- MessageCreateAction message =
119
- createModMessage (event , userId , modMailAuditLog .orElseThrow ());
149
+ event .deferReply ().setEphemeral (true ).queue ();
150
+ MessageCreateAction message = createModMessage (event , userId , userMessage ,
151
+ wantsToStayAnonymous , modMailAuditLog .orElseThrow ());
120
152
121
153
sendMessage (event , message );
122
-
123
154
}
124
155
125
156
private boolean handleIsOnCooldown (long userId , SlashCommandInteractionEvent event ) {
@@ -140,11 +171,8 @@ private Optional<TextChannel> getModMailChannel(JDA jda, long guildId) {
140
171
.findAny ();
141
172
}
142
173
143
- private MessageCreateAction createModMessage (SlashCommandInteractionEvent event , long userId ,
144
- TextChannel modMailAuditLog ) {
145
- String userMessage = event .getOption (OPTION_MESSAGE ).getAsString ();
146
- boolean wantsToStayAnonymous = event .getOption (OPTION_STAY_ANONYMOUS ).getAsBoolean ();
147
-
174
+ private MessageCreateAction createModMessage (ModalInteractionEvent event , long userId ,
175
+ String userMessage , boolean wantsToStayAnonymous , TextChannel modMailAuditLog ) {
148
176
User user = wantsToStayAnonymous ? null : event .getUser ();
149
177
MessageCreateAction message =
150
178
modMailAuditLog .sendMessageEmbeds (createModMailMessage (user , userMessage ));
@@ -164,7 +192,7 @@ private MessageCreateAction createModMessage(SlashCommandInteractionEvent event,
164
192
return message ;
165
193
}
166
194
167
- private void sendMessage (SlashCommandInteractionEvent event , MessageCreateAction message ) {
195
+ private void sendMessage (ModalInteractionEvent event , MessageCreateAction message ) {
168
196
InteractionHook hook = event .getHook ();
169
197
message .mapToResult ().map (result -> {
170
198
if (result .isSuccess ()) {
0 commit comments