5151@ AutoDetectableComponentHandler ("utils" )
5252@ RequiredArgsConstructor
5353public class InteractionUtils implements ButtonHandler , ModalHandler , StringSelectMenuHandler {
54- /**
55- * Template Interaction ID for deleting the original Message.
56- */
57- public static final String DELETE_ORIGINAL_TEMPLATE = "utils:delete" ;
5854 /**
5955 * Template Interaction ID for banning a single member from the current guild.
6056 */
@@ -72,6 +68,11 @@ public class InteractionUtils implements ButtonHandler, ModalHandler, StringSele
7268 */
7369 public static final String WARN_TEMPLATE = "utils:warn:%s" ;
7470
71+ /**
72+ * Template Interaction ID for deleting the original Message.
73+ */
74+ private static final String DELETE_ORIGINAL_TEMPLATE = "utils:delete:%d" ;
75+
7576 private final NotificationService notificationService ;
7677 private final BotConfig botConfig ;
7778 private final WarnRepository warnRepository ;
@@ -83,28 +84,42 @@ public class InteractionUtils implements ButtonHandler, ModalHandler, StringSele
8384 * of the message, a staff member, or the owner.
8485 *
8586 * @param interaction The button interaction.
87+ * @param componentId The split id of the interaction component.
8688 * @return the {@link ReplyCallbackAction} for responding to the request
8789 */
8890 @ CheckReturnValue
89- private InteractionCallbackAction <?> delete (@ NotNull ButtonInteraction interaction ) {
91+ private InteractionCallbackAction <?> delete (@ NotNull ButtonInteraction interaction , String [] componentId ) {
9092 Member member = interaction .getMember ();
9193 if (member == null ) {
9294 return Responses .warning (interaction , "Could not get member." );
9395 }
9496 GuildConfig config = botConfig .get (interaction .getGuild ());
9597 Message msg = interaction .getMessage ();
96- if (
97- member .getUser ().getIdLong () == msg .getAuthor ().getIdLong () ||
98- member .getRoles ().contains (config .getModerationConfig ().getStaffRole ()) ||
99- member .isOwner ()
100- ) {
98+
99+ String authorId = "" ;
100+
101+ if (componentId .length >1 ) {
102+ authorId = componentId [1 ];
103+ }
104+
105+ if (authorId .equals (member .getUser ().getId ()) ||
106+ member .getRoles ().contains (config .getModerationConfig ().getStaffRole ()) ||
107+ member .isOwner ()) {
101108 msg .delete ().queue ();
102109 return interaction .deferEdit ();
103110 } else {
104111 return Responses .warning (interaction , "You don't have permission to delete this message." );
105112 }
106113 }
107114
115+ public static Button createDeleteButton (long senderId ) {
116+ return Button .secondary (createDeleteInteractionId (senderId ), "\uD83D \uDDD1 ️" );
117+ }
118+
119+ public static String createDeleteInteractionId (long senderId ) {
120+ return DELETE_ORIGINAL_TEMPLATE .formatted (senderId );
121+ }
122+
108123 private void kick (ModalInteraction interaction , @ NotNull Guild guild , String memberId , String reason ) {
109124 if (!interaction .getMember ().hasPermission (Permission .KICK_MEMBERS )) {
110125 Responses .error (interaction .getHook (), "Missing permissions" ).queue ();
@@ -119,7 +134,7 @@ private void kick(ModalInteraction interaction, @NotNull Guild guild, String mem
119134 }, error -> Responses .error (interaction .getHook (), "Could not find member: " + error .getMessage ()).queue ()
120135 );
121136 }
122-
137+
123138 private void warn (ModalInteraction interaction , @ NotNull Guild guild , String memberId , WarnSeverity severity , String reason ) {
124139 if (!interaction .getMember ().hasPermission (Permission .MODERATE_MEMBERS )) {
125140 Responses .error (interaction .getHook (), "Missing permissions" ).queue ();
@@ -149,7 +164,7 @@ private void ban(ModalInteraction interaction, @NotNull Guild guild, String memb
149164 }, error -> Responses .error (interaction .getHook (), "Could not find member: " + error .getMessage ()).queue ()
150165 );
151166 }
152-
167+
153168 private void resolveIfInReport (MessageChannelUnion currentChannel , User actioner ) {
154169 if (!currentChannel .getType ().isThread ()) {
155170 return ;
@@ -178,9 +193,9 @@ public void handleButton(@NotNull ButtonInteractionEvent event, @NotNull Button
178193 Responses .error (event .getHook (), "This button may only be used in context of a server." ).queue ();
179194 return ;
180195 }
181-
196+
182197 (switch (id [1 ]) {
183- case "delete" -> delete (event .getInteraction ());
198+ case "delete" -> delete (event .getInteraction (), id );
184199 case "kick" -> generateModal (event , "Kick user" );
185200 case "ban" -> generateModal (event , "Ban user" );
186201 case "unban" -> generateModal (event , "Unban user" );
@@ -209,21 +224,21 @@ public void handleModal(@Nonnull ModalInteractionEvent event, @Nonnull List<Moda
209224 }
210225 String reason = "None" ;
211226 WarnSeverity severity = WarnSeverity .MEDIUM ;
212-
227+
213228 if (id .length > 3 ) {
214229 try {
215230 severity = WarnSeverity .valueOf (id [3 ]);
216231 } catch (IllegalArgumentException e ) {
217232 ExceptionLogger .capture (e , "Cannot load warn severity" );
218233 }
219234 }
220-
235+
221236 for (ModalMapping mapping : mappings ) {
222237 if ("reason" .equals (mapping .getId ())) {
223238 reason = mapping .getAsString ();
224239 }
225240 }
226-
241+
227242 switch (id [1 ]) {
228243 case "kick" -> kick (event .getInteraction (), event .getGuild (), id [2 ], reason );
229244 case "ban" -> ban (event .getInteraction (), event .getGuild (), id [2 ], reason );
0 commit comments