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,38 @@ 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 (DELETE_ORIGINAL_TEMPLATE .formatted (senderId ), "\uD83D \uDDD1 ️" );
117+ }
118+
108119 private void kick (ModalInteraction interaction , @ NotNull Guild guild , String memberId , String reason ) {
109120 if (!interaction .getMember ().hasPermission (Permission .KICK_MEMBERS )) {
110121 Responses .error (interaction .getHook (), "Missing permissions" ).queue ();
@@ -119,7 +130,7 @@ private void kick(ModalInteraction interaction, @NotNull Guild guild, String mem
119130 }, error -> Responses .error (interaction .getHook (), "Could not find member: " + error .getMessage ()).queue ()
120131 );
121132 }
122-
133+
123134 private void warn (ModalInteraction interaction , @ NotNull Guild guild , String memberId , WarnSeverity severity , String reason ) {
124135 if (!interaction .getMember ().hasPermission (Permission .MODERATE_MEMBERS )) {
125136 Responses .error (interaction .getHook (), "Missing permissions" ).queue ();
@@ -149,7 +160,7 @@ private void ban(ModalInteraction interaction, @NotNull Guild guild, String memb
149160 }, error -> Responses .error (interaction .getHook (), "Could not find member: " + error .getMessage ()).queue ()
150161 );
151162 }
152-
163+
153164 private void resolveIfInReport (MessageChannelUnion currentChannel , User actioner ) {
154165 if (!currentChannel .getType ().isThread ()) {
155166 return ;
@@ -178,9 +189,9 @@ public void handleButton(@NotNull ButtonInteractionEvent event, @NotNull Button
178189 Responses .error (event .getHook (), "This button may only be used in context of a server." ).queue ();
179190 return ;
180191 }
181-
192+
182193 (switch (id [1 ]) {
183- case "delete" -> delete (event .getInteraction ());
194+ case "delete" -> delete (event .getInteraction (), id );
184195 case "kick" -> generateModal (event , "Kick user" );
185196 case "ban" -> generateModal (event , "Ban user" );
186197 case "unban" -> generateModal (event , "Unban user" );
@@ -209,21 +220,21 @@ public void handleModal(@Nonnull ModalInteractionEvent event, @Nonnull List<Moda
209220 }
210221 String reason = "None" ;
211222 WarnSeverity severity = WarnSeverity .MEDIUM ;
212-
223+
213224 if (id .length > 3 ) {
214225 try {
215226 severity = WarnSeverity .valueOf (id [3 ]);
216227 } catch (IllegalArgumentException e ) {
217228 ExceptionLogger .capture (e , "Cannot load warn severity" );
218229 }
219230 }
220-
231+
221232 for (ModalMapping mapping : mappings ) {
222233 if ("reason" .equals (mapping .getId ())) {
223234 reason = mapping .getAsString ();
224235 }
225236 }
226-
237+
227238 switch (id [1 ]) {
228239 case "kick" -> kick (event .getInteraction (), event .getGuild (), id [2 ], reason );
229240 case "ban" -> ban (event .getInteraction (), event .getGuild (), id [2 ], reason );
0 commit comments