1717import net .dv8tion .jda .api .EmbedBuilder ;
1818import net .dv8tion .jda .api .entities .*;
1919import net .dv8tion .jda .api .entities .channel .Channel ;
20+ import net .dv8tion .jda .api .entities .channel .concrete .TextChannel ;
2021import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
2122import net .dv8tion .jda .api .entities .channel .middleman .MessageChannel ;
2223import net .dv8tion .jda .api .events .interaction .ModalInteractionEvent ;
4344 */
4445@ Slf4j
4546@ RequiredArgsConstructor
46- @ AutoDetectableComponentHandler ({"resolve-report" ,"report" })
47+ @ AutoDetectableComponentHandler ({"resolve-report" , ReportManager . REPORT_INTERACTION_NAME })
4748public class ReportManager implements ButtonHandler , ModalHandler {
49+ static final String REPORT_INTERACTION_NAME = "report" ;
4850 private final BotConfig botConfig ;
4951
5052 @ Override
5153 public void handleButton (@ NotNull ButtonInteractionEvent event , Button button ) {
52- event .deferReply (true ).queue ();
5354 String [] id = ComponentIdBuilder .split (event .getComponentId ());
55+ if ("resolve-report" .equals (id [0 ])) {
56+ handleResolveReportButton (event , id );
57+ } else if (REPORT_INTERACTION_NAME .equals (id [0 ])&&"create-thread" .equals (id [1 ])) {
58+ createReportUserThread (event , id );
59+ }else {
60+ Responses .error (event , "Unexpected button" ).queue ();
61+ }
62+ }
63+
64+ private void createReportUserThread (ButtonInteractionEvent event , String [] id ) {
65+ TextChannel reportUserChannel = botConfig .get (event .getGuild ())
66+ .getModerationConfig ()
67+ .getReportUserThreadHolder ();
68+ ThreadChannel reportThread = event .getGuild ().getThreadChannelById (id [2 ]);
69+ if (reportThread ==null ) {
70+ Responses .error (event , "This report has been handled already." ).queue ();
71+ return ;
72+ }
73+ List <MessageEmbed > reportEmbeds = event .getMessage ().getEmbeds ();
74+ String title ;
75+ if (reportEmbeds .isEmpty ()) {
76+ title = "report information" ;
77+ } else {
78+ title = reportEmbeds .get (0 ).getTitle ();
79+ }
80+ reportUserChannel
81+ .createThreadChannel (title , true )
82+ .queue (reporterThread -> {
83+ reporterThread .getManager ().setInvitable (false ).queue ();
84+ reporterThread
85+ .sendMessage (event .getUser ().getAsMention () +
86+ "\n You can provide additional information regarding your report here.\n "
87+ + "Messages sent in this thread can be seen by staff members but not other users." )
88+ .addEmbeds (reportEmbeds )
89+ .queue ();
90+ reporterThread .addThreadMember (event .getUser ()).queue ();
91+ reportThread
92+ .sendMessageEmbeds (
93+ new EmbedBuilder ()
94+ .setTitle ("Additional information from reporter" )
95+ .setDescription ("The reporter created a thread for additional information: " + reporterThread .getAsMention () + "\n \n [thread link](" + reporterThread .getJumpUrl () + ")" )
96+ .build ())
97+ .queue ();
98+ event .editComponents (ActionRow .of (event .getComponent ().asDisabled ())).queue (success -> {
99+ Responses .info (event .getHook (), "Information thread created" , "The thread " +reporterThread .getAsMention ()+" has been created for you. You can provide additional details related to your report there." ).queue ();
100+ });
101+
102+ });
103+ }
104+
105+ private void handleResolveReportButton (ButtonInteractionEvent event , String [] id ) {
106+ event .deferReply (true ).queue ();
54107 ThreadChannel thread = event .getGuild ().getThreadChannelById (id [1 ]);
55108 if (thread == null ) {
56109 Responses .error (event .getHook (), "Could not find the corresponding thread channel." ).queue ();
@@ -64,7 +117,7 @@ public void handleButton(@NotNull ButtonInteractionEvent event, Button button) {
64117 /**
65118 * Resolves a report thread.
66119 * This closes the current thread.
67- * Tis method does not check whether the current thread is actually a report thread.
120+ * This method does not check whether the current thread is actually a report thread.
68121 * @param resolver the {@link User} responsible for resolving the report
69122 * @param reportThread the thread of the report to resolve
70123 */
@@ -99,7 +152,7 @@ protected Modal buildUserReportModal(@NotNull UserContextInteractionEvent event)
99152 .setMaxLength (MessageEmbed .VALUE_MAX_LENGTH )
100153 .build ();
101154 String title = "Report " + UserUtils .getUserTag (event .getTarget ());
102- return Modal .create (ComponentIdBuilder .build ("report" , "user" , event .getTarget ().getId ()), title .substring (0 , Math .min (title .length (), Modal .MAX_TITLE_LENGTH )))
155+ return Modal .create (ComponentIdBuilder .build (REPORT_INTERACTION_NAME , "user" , event .getTarget ().getId ()), title .substring (0 , Math .min (title .length (), Modal .MAX_TITLE_LENGTH )))
103156 .addActionRow (messageInput )
104157 .build ();
105158 }
@@ -120,7 +173,7 @@ protected Modal buildMessageReportModal(@NotNull MessageContextInteractionEvent
120173 TextInput messageInput = TextInput .create ("reason" , "Report Description" , TextInputStyle .PARAGRAPH )
121174 .setMaxLength (MessageEmbed .VALUE_MAX_LENGTH )
122175 .build ();
123- return Modal .create (ComponentIdBuilder .build ("report" , "message" , event .getTarget ().getId ()), title .substring (0 , Math .min (title .length (), Modal .MAX_TITLE_LENGTH )))
176+ return Modal .create (ComponentIdBuilder .build (REPORT_INTERACTION_NAME , "message" , event .getTarget ().getId ()), title .substring (0 , Math .min (title .length (), Modal .MAX_TITLE_LENGTH )))
124177 .addActionRow (messageInput )
125178 .build ();
126179 }
@@ -133,7 +186,7 @@ protected Modal buildMessageReportModal(@NotNull MessageContextInteractionEvent
133186 * @param targetId The targeted user's id.
134187 * @return The {@link WebhookMessageCreateAction}.
135188 */
136- protected WebhookMessageCreateAction <Message > handleUserReport (InteractionHook hook , @ NotNull String reason , String targetId ) {
189+ WebhookMessageCreateAction <Message > handleUserReport (InteractionHook hook , @ NotNull String reason , String targetId ) {
137190 if (reason .isBlank ()) {
138191 return Responses .error (hook , "No report reason was provided." );
139192 }
@@ -147,16 +200,26 @@ protected WebhookMessageCreateAction<Message> handleUserReport(InteractionHook h
147200 return ;
148201 }
149202 reportChannel .sendMessageEmbeds (embed .build ())
150- .queue (m -> this .createReportThread (m , target .getIdLong (), config .getModerationConfig ()));
151- embed .setDescription ("Successfully reported " + "`" + UserUtils .getUserTag (target ) + "`!\n Your report has been send to our Moderators" );
152- hook .sendMessageEmbeds (embed .build ()).queue ();
203+ .queue (m -> this .createReportThread (m , target .getIdLong (), config .getModerationConfig (),
204+ reportThread -> {
205+ sendReportResponse (hook , target , embed , reportThread );
206+ }));
153207 }, failure -> {
154208 Responses .error (hook , "The user to report seems not to exist any more." ).queue ();
155209 log .warn ("Cannot retrieve user {} when reporting them" , targetId , failure );
156210 });
157211 return null ;
158212 }
159213
214+ private void sendReportResponse (InteractionHook hook , User targetUser , EmbedBuilder reportEmbed , ThreadChannel reportThread ) {
215+ reportEmbed .setDescription ("Successfully reported " + "`" + UserUtils .getUserTag (targetUser ) + "`!\n Your report has been send to our Moderators.\n In case you want to supply additional details, please use the \" Create thread\" button below." );
216+ hook .sendMessageEmbeds (reportEmbed .build ())
217+ .addActionRow (Button .secondary (
218+ ComponentIdBuilder .build (REPORT_INTERACTION_NAME , "create-thread" , reportThread .getId ()),
219+ "Create thread for providing further details" ))
220+ .queue ();
221+ }
222+
160223 private void handleMessageReport (ModalInteractionEvent event , String messageId ) {
161224 String reason = event .getValue ("reason" ).getAsString ();
162225 if (reason .isBlank ()) {
@@ -170,12 +233,11 @@ private void handleMessageReport(ModalInteractionEvent event, String messageId)
170233 embed .addField ("Message" , String .format ("[Jump to Message](%s)" , target .getJumpUrl ()), false );
171234 MessageChannel reportChannel = config .getModerationConfig ().getReportChannel ();
172235 reportChannel .sendMessageEmbeds (embed .build ()).queue (m -> createReportThread (m , target .getAuthor ().getIdLong (), config .getModerationConfig (), thread ->{
236+ sendReportResponse (event .getHook (), target .getAuthor (), embed , thread );
173237 WebhookUtil .ensureWebhookExists (thread .getParentChannel ().asStandardGuildMessageChannel (), wh ->{
174238 WebhookUtil .mirrorMessageToWebhook (wh , target , target .getContentRaw (), thread .getIdLong (), null , null );
175239 });
176240 }));
177- embed .setDescription ("Successfully reported " + "`" + UserUtils .getUserTag (target .getAuthor ()) + "`!\n Your report has been send to our Moderators" );
178- event .getHook ().sendMessageEmbeds (embed .build ()).queue ();
179241 }, failure -> {
180242 Responses .error (event .getHook (), "The author of the message to report seems not to exist any more." ).queue ();
181243 log .info ("Cannot retrieve reported message {} in channel {} - the message might have been deleted" , messageId , event .getChannel (), failure );
@@ -192,10 +254,6 @@ private ActionRow setComponents(long targetId, long threadId) {
192254 );
193255 }
194256
195- private void createReportThread (Message message , long targetId , ModerationConfig config ) {
196- createReportThread (message , targetId , config , thread ->{});
197- }
198-
199257 private void createReportThread (Message message , long targetId , ModerationConfig config , Consumer <ThreadChannel > onSuccess ) {
200258 message .createThreadChannel (message .getEmbeds ().get (0 ).getTitle ()).queue (
201259 thread -> {
@@ -207,10 +265,6 @@ private void createReportThread(Message message, long targetId, ModerationConfig
207265 );
208266 }
209267
210-
211-
212-
213-
214268 private EmbedBuilder buildReportEmbed (User reported , User reportedBy , String reason , Channel channel ) {
215269 return new EmbedBuilder ()
216270 .setAuthor (UserUtils .getUserTag (reported ), null , reported .getEffectiveAvatarUrl ())
0 commit comments