Skip to content
This repository was archived by the owner on Jun 23, 2021. It is now read-only.

Commit 13bb08e

Browse files
authored
Merge pull request #16 from cerus/develop
Develop
2 parents 021a4c6 + 04ef60d commit 13bb08e

18 files changed

+772
-70
lines changed

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ This library is also lacking some documentation. If you have any questions feel
1515
- Followup messages
1616
- Command permissions
1717
- [Command routers](https://github.com/cerus/jda-slash-commands/wiki/Command-Routers) (Automatically route interactions to your command framework)
18+
- Message components
1819

1920
## Example
2021

22+
Slash Commands:
23+
2124
```java
2225
public class MyBot {
2326

@@ -88,6 +91,82 @@ public class MyBot {
8891

8992
</details>
9093

94+
Message components:
95+
96+
```java
97+
public class MyBot {
98+
99+
private static final String BOT_TOKEN = "********";
100+
private static final String APPLICATION_ID = "12345654321";
101+
102+
public static void main(String[] args) {
103+
JDA jda = JDABuilder.setRawEventsEnabled(true).createDefault(BOT_TOKEN).build();
104+
initCommands(jda);
105+
}
106+
107+
public void initCommands(JDA jda) {
108+
JDASlashCommands.initialize(jda, BOT_TOKEN, APPLICATION_ID);
109+
110+
// Add a component listener that will get called
111+
// every time a component is clicked
112+
JDASlashCommands.addComponentListener(interaction -> {
113+
final Component clickedComponent = interaction.getClickedComponent();
114+
if (clickedComponent instanceof Button) {
115+
final Button button = clickedComponent.cast();
116+
interaction.respond("You clicked " + button.getLabel() + "!");
117+
}
118+
});
119+
120+
// Add a one time component listener that will
121+
// get called when a button with the provided
122+
// button id is clicked
123+
JDASlashCommands.addOneTimeComponentListener("my_btn", interaction -> {
124+
final Component clickedComponent = interaction.getClickedComponent();
125+
final Button button = clickedComponent.cast();
126+
interaction.respond("You clicked " + button.getLabel() + "!");
127+
});
128+
129+
// Create an example command and respond to
130+
// interactions with message components
131+
JDASlashCommands.submitGlobalCommand(new CommandBuilder()
132+
.name("test")
133+
.desc("Test command")
134+
.build(), interaction -> {
135+
interaction.respond("Available actions:", Arrays.asList(
136+
ActionRow.of(
137+
Button.normalButton(Button.Style.PRIMARY, "Action 1", "my_btn"),
138+
Button.normalButton(Button.Style.SECONDARY, "Action 2", "my_btn_0"),
139+
Button.normalButton(Button.Style.DANGER, "Action 3", "my_btn_1"),
140+
Button.normalButton(Button.Style.SUCCESS, "Action 4", "my_btn_2")
141+
),
142+
ActionRow.of(
143+
Button.emojiButton(Button.Style.SUCCESS, "Emoji!", "my_btn_3",
144+
Button.PartialEmoji.getDefaultEmoji("❤️")),
145+
Button.emojiButton(Button.Style.DANGER, "Another emoji!", "my_btn_4",
146+
Button.PartialEmoji.getEmojiFromEmote(jda.getEmoteById(850779306803986442L)))
147+
//
148+
// Link buttons seem to be broken at the moment
149+
//
150+
//Button.emojiLinkButton(Button.Style.DANGER, "Emoji with link!", "https://cerus.dev",
151+
// Button.PartialEmoji.getDefaultEmoji("✨")),
152+
//Button.linkButton(Button.Style.SECONDARY, "Link!", "https://discord.com")
153+
)
154+
));
155+
});
156+
}
157+
158+
}
159+
```
160+
161+
<details>
162+
<summary>Pictures</summary>
163+
164+
![Img 1](https://i.imgur.com/xlg2hYm.png)
165+
166+
![Img 2](https://i.imgur.com/vpGX60r.png)
167+
168+
</details>
169+
91170
## Installation
92171

93172
Since version 1.2.2, you can install `jda-slash-commands` from the central repository.

pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>dev.cerus</groupId>
88
<artifactId>jda-slash-commands</artifactId>
9-
<version>1.2.2</version>
9+
<version>1.3.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>${project.artifactId}</name>
@@ -24,11 +24,17 @@
2424
<developer>
2525
<name>Maximilian Dorn</name>
2626
<email>[email protected]</email>
27-
<organization>PypeWare GmbH</organization>
28-
<organizationUrl>https://github.com/pypeware</organizationUrl>
27+
<url>https://cerus.dev</url>
2928
</developer>
3029
</developers>
3130

31+
<contributors>
32+
<contributor>
33+
<name>PringlePot</name>
34+
<url>https://github.com/PringlePot</url>
35+
</contributor>
36+
</contributors>
37+
3238
<scm>
3339
<connection>scm:git:git://github.com/cerus/jda-slash-commands.git</connection>
3440
<developerConnection>scm:git:ssh://github.com:cerus/jda-slash-commands.git</developerConnection>

src/main/java/dev/cerus/jdasc/JDASlashCommands.java

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import dev.cerus.jdasc.command.ApplicationCommandOptionType;
1111
import dev.cerus.jdasc.command.permissions.ApplicationCommandPermissions;
1212
import 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;
1316
import dev.cerus.jdasc.http.DiscordHttpClient;
1417
import dev.cerus.jdasc.interaction.Interaction;
1518
import dev.cerus.jdasc.interaction.followup.FollowupMessage;
@@ -20,6 +23,7 @@
2023
import java.io.IOException;
2124
import java.util.ArrayList;
2225
import java.util.Arrays;
26+
import java.util.Collections;
2327
import java.util.HashMap;
2428
import java.util.List;
2529
import 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

src/main/java/dev/cerus/jdasc/command/ApplicationCommandListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface ApplicationCommandListener {
1414
*
1515
* @param interaction The interaction
1616
*/
17-
void onInteraction(Interaction interaction);
17+
void onInteraction(final Interaction interaction);
1818

1919
/**
2020
* Gets called if a argument was specified
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dev.cerus.jdasc.components;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class ActionRow implements Component {
7+
8+
private final List<Component> components;
9+
private final ComponentType type = ComponentType.ACTION_ROW;
10+
11+
public ActionRow(final List<Component> components) {
12+
this.components = components;
13+
}
14+
15+
public static ActionRow of(final Component... components) {
16+
return new ActionRow(Arrays.asList(components));
17+
}
18+
19+
public List<Component> getComponents() {
20+
return this.components;
21+
}
22+
23+
@Override
24+
public ComponentType getType() {
25+
return this.type;
26+
}
27+
28+
}

0 commit comments

Comments
 (0)