1010import dev .cerus .jdasc .command .ApplicationCommandOptionType ;
1111import dev .cerus .jdasc .command .permissions .ApplicationCommandPermissions ;
1212import dev .cerus .jdasc .command .permissions .GuildApplicationCommandPermissions ;
13+ import dev .cerus .jdasc .components .Button ;
14+ import dev .cerus .jdasc .components .Component ;
15+ import dev .cerus .jdasc .components .ComponentListener ;
1316import dev .cerus .jdasc .http .DiscordHttpClient ;
1417import dev .cerus .jdasc .interaction .Interaction ;
1518import dev .cerus .jdasc .interaction .followup .FollowupMessage ;
2023import java .io .IOException ;
2124import java .util .ArrayList ;
2225import java .util .Arrays ;
26+ import java .util .Collections ;
2327import java .util .HashMap ;
2428import java .util .List ;
2529import java .util .Map ;
@@ -50,13 +54,23 @@ public class JDASlashCommands {
5054 private static final Map <Long , ApplicationCommand > discordCommands = new HashMap <>();
5155 private static final Map <Long , Map <Long , ApplicationCommand >> guildCommands = new HashMap <>();
5256 private static final Map <ApplicationCommand , ApplicationCommandListener > commandListenerMap = new HashMap <>();
57+ private static final Map <String , ComponentListener > oneTimeComponentListeners = new HashMap <>();
58+ private static final List <ComponentListener > componentListeners = new ArrayList <>();
5359
5460 private static DiscordHttpClient discordHttpClient ;
5561 private static EntityBuilder entityBuilder ;
5662
5763 private JDASlashCommands () {
5864 }
5965
66+ public static void addComponentListener (final ComponentListener listener ) {
67+ componentListeners .add (listener );
68+ }
69+
70+ public static void addOneTimeComponentListener (final String buttonId , final ComponentListener listener ) {
71+ oneTimeComponentListeners .put (buttonId , listener );
72+ }
73+
6074 /**
6175 * Delete a followup message
6276 *
@@ -79,7 +93,7 @@ public static CompletableFuture<Void> deleteFollowupMessage(final Interaction in
7993 * @return A future
8094 */
8195 public static CompletableFuture <Void > editFollowupMessage (final Interaction interaction , final long messageId , final MessageEmbed ... embeds ) {
82- return editFollowupMessage (interaction , messageId , new FollowupMessage ("" , false , Arrays .asList (embeds ), 0 ));
96+ return editFollowupMessage (interaction , messageId , new FollowupMessage ("" , false , Arrays .asList (embeds ), 0 , Collections . emptyList () ));
8397 }
8498
8599 /**
@@ -93,7 +107,7 @@ public static CompletableFuture<Void> editFollowupMessage(final Interaction inte
93107 * @return A future
94108 */
95109 public static CompletableFuture <Void > editFollowupMessage (final Interaction interaction , final long messageId , final int flags , final MessageEmbed ... embeds ) {
96- return editFollowupMessage (interaction , messageId , new FollowupMessage ("" , false , Arrays .asList (embeds ), flags ));
110+ return editFollowupMessage (interaction , messageId , new FollowupMessage ("" , false , Arrays .asList (embeds ), flags , Collections . emptyList () ));
97111 }
98112
99113 /**
@@ -106,7 +120,7 @@ public static CompletableFuture<Void> editFollowupMessage(final Interaction inte
106120 * @return A future
107121 */
108122 public static CompletableFuture <Void > editFollowupMessage (final Interaction interaction , final long messageId , final String message ) {
109- return editFollowupMessage (interaction , messageId , new FollowupMessage (message , false , new ArrayList <>(), 0 ));
123+ return editFollowupMessage (interaction , messageId , new FollowupMessage (message , false , new ArrayList <>(), 0 , Collections . emptyList () ));
110124 }
111125
112126 /**
@@ -120,7 +134,7 @@ public static CompletableFuture<Void> editFollowupMessage(final Interaction inte
120134 * @return A future
121135 */
122136 public static CompletableFuture <Void > editFollowupMessage (final Interaction interaction , final long messageId , final String message , final int flags ) {
123- return editFollowupMessage (interaction , messageId , new FollowupMessage (message , false , new ArrayList <>(), flags ));
137+ return editFollowupMessage (interaction , messageId , new FollowupMessage (message , false , new ArrayList <>(), flags , Collections . emptyList () ));
124138 }
125139
126140 /**
@@ -146,7 +160,7 @@ public static CompletableFuture<Void> editFollowupMessage(final Interaction inte
146160 * @return The sent message
147161 */
148162 public static CompletableFuture <Message > submitFollowupMessage (final Interaction interaction , final MessageEmbed ... embeds ) {
149- return submitFollowupMessage (interaction , new FollowupMessage ("" , false , Arrays .asList (embeds ), 0 ));
163+ return submitFollowupMessage (interaction , new FollowupMessage ("" , false , Arrays .asList (embeds ), 0 , Collections . emptyList () ));
150164 }
151165
152166 /**
@@ -160,7 +174,7 @@ public static CompletableFuture<Message> submitFollowupMessage(final Interaction
160174 * @return The sent message
161175 */
162176 public static CompletableFuture <Message > submitFollowupMessage (final Interaction interaction , final int flags , final MessageEmbed ... embeds ) {
163- return submitFollowupMessage (interaction , new FollowupMessage ("" , false , Arrays .asList (embeds ), flags ));
177+ return submitFollowupMessage (interaction , new FollowupMessage ("" , false , Arrays .asList (embeds ), flags , Collections . emptyList () ));
164178 }
165179
166180 /**
@@ -173,7 +187,7 @@ public static CompletableFuture<Message> submitFollowupMessage(final Interaction
173187 * @return The sent message
174188 */
175189 public static CompletableFuture <Message > submitFollowupMessage (final Interaction interaction , final String message ) {
176- return submitFollowupMessage (interaction , new FollowupMessage (message , false , new ArrayList <>(), 0 ));
190+ return submitFollowupMessage (interaction , new FollowupMessage (message , false , new ArrayList <>(), 0 , Collections . emptyList () ));
177191 }
178192
179193 /**
@@ -187,7 +201,7 @@ public static CompletableFuture<Message> submitFollowupMessage(final Interaction
187201 * @return The sent message
188202 */
189203 public static CompletableFuture <Message > submitFollowupMessage (final Interaction interaction , final String message , final int flags ) {
190- return submitFollowupMessage (interaction , new FollowupMessage (message , false , new ArrayList <>(), flags ));
204+ return submitFollowupMessage (interaction , new FollowupMessage (message , false , new ArrayList <>(), flags , Collections . emptyList () ));
191205 }
192206
193207 /**
@@ -333,8 +347,7 @@ public static CompletableFuture<Void> deleteInteractionResponse(final Interactio
333347 */
334348 public static CompletableFuture <Void > editInteractionResponse (final Interaction interaction , final MessageEmbed ... embeds ) {
335349 return editInteractionResponse (interaction , new InteractionApplicationCommandCallbackData (
336- false , "" , Arrays .asList (embeds ), 0
337- ));
350+ false , "" , Arrays .asList (embeds ), 0 ));
338351 }
339352
340353 /**
@@ -350,8 +363,7 @@ public static CompletableFuture<Void> editInteractionResponse(final Interaction
350363 */
351364 public static CompletableFuture <Void > editInteractionResponse (final Interaction interaction , final String message ) {
352365 return editInteractionResponse (interaction , new InteractionApplicationCommandCallbackData (
353- false , message , new ArrayList <>(), 0
354- ));
366+ false , message , new ArrayList <>(), 0 ));
355367 }
356368
357369 /**
@@ -368,8 +380,7 @@ public static CompletableFuture<Void> editInteractionResponse(final Interaction
368380 */
369381 public static CompletableFuture <Void > editInteractionResponse (final Interaction interaction , final String message , final int flags ) {
370382 return editInteractionResponse (interaction , new InteractionApplicationCommandCallbackData (
371- false , message , new ArrayList <>(), flags
372- ));
383+ false , message , new ArrayList <>(), flags ));
373384 }
374385
375386 /**
@@ -589,18 +600,33 @@ public void onShutdown(@NotNull final ShutdownEvent event) {
589600 * @param interaction The interaction
590601 */
591602 public static void handleInteraction (final Interaction interaction ) {
592- final ApplicationCommand command = commandMap .get (interaction .getCommandId ());
593- final ApplicationCommandListener listener = commandListenerMap .get (command );
594-
595- if (command == null || listener == null ) {
596- // Discord sent us a command that wasn't registered. We can't do anything about that so we just do nothing
597- return ;
598- }
603+ switch (interaction .getType ()) {
604+ case APPLICATION_COMMAND :
605+ final ApplicationCommand command = commandMap .get (interaction .getCommandId ());
606+ final ApplicationCommandListener listener = commandListenerMap .get (command );
607+
608+ if (command == null || listener == null ) {
609+ // Discord sent us a command that wasn't registered. We can't do anything about that so we just do nothing
610+ return ;
611+ }
599612
600- listener .onInteraction (interaction );
601- final Map <String , InteractionResponseOption > arguments = findArguments (command , interaction );
602- if (arguments != null && !arguments .isEmpty ()) {
603- listener .handleArguments (interaction , arguments );
613+ listener .onInteraction (interaction );
614+ final Map <String , InteractionResponseOption > arguments = findArguments (command , interaction );
615+ if (arguments != null && !arguments .isEmpty ()) {
616+ listener .handleArguments (interaction , arguments );
617+ }
618+ break ;
619+ case MESSAGE_COMPONENT :
620+ final Component clickedComponent = interaction .getClickedComponent ();
621+ if (clickedComponent instanceof Button ) {
622+ final Button button = (Button ) clickedComponent ;
623+ final ComponentListener componentListener = oneTimeComponentListeners .remove (button .getCustomId ());
624+ if (componentListener != null ) {
625+ componentListener .onInteraction (interaction );
626+ }
627+ }
628+ componentListeners .forEach (componentListener -> componentListener .onInteraction (interaction ));
629+ break ;
604630 }
605631 }
606632
0 commit comments