88import net .dv8tion .jda .api .entities .Role ;
99import net .dv8tion .jda .api .entities .User ;
1010import net .dv8tion .jda .api .entities .channel .concrete .TextChannel ;
11+ import net .dv8tion .jda .api .events .interaction .ModalInteractionEvent ;
1112import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
1213import net .dv8tion .jda .api .interactions .InteractionHook ;
1314import net .dv8tion .jda .api .interactions .commands .Command ;
1415import net .dv8tion .jda .api .interactions .commands .OptionType ;
1516import 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 ;
1620import net .dv8tion .jda .api .requests .restaction .MessageCreateAction ;
1721import org .jetbrains .annotations .Nullable ;
1822import org .slf4j .Logger ;
@@ -64,8 +68,6 @@ public ModMailCommand(JDA jda, Config config) {
6468 super (COMMAND_NAME , "Contact the moderators of the selected guild" ,
6569 CommandVisibility .GLOBAL );
6670
67- OptionData messageOption = new OptionData (OptionType .STRING , OPTION_MESSAGE ,
68- "What do you want to tell them?" , true );
6971 OptionData guildOption = new OptionData (OptionType .STRING , OPTION_GUILD ,
7072 "The server to contact mods from" , true );
7173 OptionData anonymousOption = new OptionData (OptionType .BOOLEAN , OPTION_STAY_ANONYMOUS ,
@@ -78,7 +80,7 @@ public ModMailCommand(JDA jda, Config config) {
7880
7981 guildOption .addChoices (choices );
8082
81- getData ().addOptions (messageOption , guildOption , anonymousOption );
83+ getData ().addOptions (guildOption , anonymousOption );
8284
8385 modMailChannelNamePredicate =
8486 Pattern .compile (config .getModMailChannelPattern ()).asMatchPredicate ();
@@ -104,9 +106,38 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
104106 return ;
105107 }
106108 authorToLastModMailInvocation .put (userId , Instant .now ());
107- event .deferReply ().setEphemeral (true ).queue ();
108109
110+ sendMessageModal (event );
111+ }
112+
113+ private void sendMessageModal (SlashCommandInteractionEvent event ) {
109114 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+
110141 Optional <TextChannel > modMailAuditLog = getModMailChannel (event .getJDA (), userGuildId );
111142 if (modMailAuditLog .isEmpty ()) {
112143 logger .warn (
@@ -115,11 +146,11 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
115146 return ;
116147 }
117148
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 ());
120152
121153 sendMessage (event , message );
122-
123154 }
124155
125156 private boolean handleIsOnCooldown (long userId , SlashCommandInteractionEvent event ) {
@@ -140,11 +171,8 @@ private Optional<TextChannel> getModMailChannel(JDA jda, long guildId) {
140171 .findAny ();
141172 }
142173
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 ) {
148176 User user = wantsToStayAnonymous ? null : event .getUser ();
149177 MessageCreateAction message =
150178 modMailAuditLog .sendMessageEmbeds (createModMailMessage (user , userMessage ));
@@ -164,7 +192,7 @@ private MessageCreateAction createModMessage(SlashCommandInteractionEvent event,
164192 return message ;
165193 }
166194
167- private void sendMessage (SlashCommandInteractionEvent event , MessageCreateAction message ) {
195+ private void sendMessage (ModalInteractionEvent event , MessageCreateAction message ) {
168196 InteractionHook hook = event .getHook ();
169197 message .mapToResult ().map (result -> {
170198 if (result .isSuccess ()) {
0 commit comments