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

Commit

Permalink
Merge pull request #16 from cerus/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
cerus authored Jun 5, 2021
2 parents 021a4c6 + 04ef60d commit 13bb08e
Show file tree
Hide file tree
Showing 18 changed files with 772 additions and 70 deletions.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ This library is also lacking some documentation. If you have any questions feel
- Followup messages
- Command permissions
- [Command routers](https://github.com/cerus/jda-slash-commands/wiki/Command-Routers) (Automatically route interactions to your command framework)
- Message components

## Example

Slash Commands:

```java
public class MyBot {

Expand Down Expand Up @@ -88,6 +91,82 @@ public class MyBot {

</details>

Message components:

```java
public class MyBot {

private static final String BOT_TOKEN = "********";
private static final String APPLICATION_ID = "12345654321";

public static void main(String[] args) {
JDA jda = JDABuilder.setRawEventsEnabled(true).createDefault(BOT_TOKEN).build();
initCommands(jda);
}

public void initCommands(JDA jda) {
JDASlashCommands.initialize(jda, BOT_TOKEN, APPLICATION_ID);

// Add a component listener that will get called
// every time a component is clicked
JDASlashCommands.addComponentListener(interaction -> {
final Component clickedComponent = interaction.getClickedComponent();
if (clickedComponent instanceof Button) {
final Button button = clickedComponent.cast();
interaction.respond("You clicked " + button.getLabel() + "!");
}
});

// Add a one time component listener that will
// get called when a button with the provided
// button id is clicked
JDASlashCommands.addOneTimeComponentListener("my_btn", interaction -> {
final Component clickedComponent = interaction.getClickedComponent();
final Button button = clickedComponent.cast();
interaction.respond("You clicked " + button.getLabel() + "!");
});

// Create an example command and respond to
// interactions with message components
JDASlashCommands.submitGlobalCommand(new CommandBuilder()
.name("test")
.desc("Test command")
.build(), interaction -> {
interaction.respond("Available actions:", Arrays.asList(
ActionRow.of(
Button.normalButton(Button.Style.PRIMARY, "Action 1", "my_btn"),
Button.normalButton(Button.Style.SECONDARY, "Action 2", "my_btn_0"),
Button.normalButton(Button.Style.DANGER, "Action 3", "my_btn_1"),
Button.normalButton(Button.Style.SUCCESS, "Action 4", "my_btn_2")
),
ActionRow.of(
Button.emojiButton(Button.Style.SUCCESS, "Emoji!", "my_btn_3",
Button.PartialEmoji.getDefaultEmoji("❤️")),
Button.emojiButton(Button.Style.DANGER, "Another emoji!", "my_btn_4",
Button.PartialEmoji.getEmojiFromEmote(jda.getEmoteById(850779306803986442L)))
//
// Link buttons seem to be broken at the moment
//
//Button.emojiLinkButton(Button.Style.DANGER, "Emoji with link!", "https://cerus.dev",
// Button.PartialEmoji.getDefaultEmoji("✨")),
//Button.linkButton(Button.Style.SECONDARY, "Link!", "https://discord.com")
)
));
});
}

}
```

<details>
<summary>Pictures</summary>

![Img 1](https://i.imgur.com/xlg2hYm.png)

![Img 2](https://i.imgur.com/vpGX60r.png)

</details>

## Installation

Since version 1.2.2, you can install `jda-slash-commands` from the central repository.
Expand Down
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.cerus</groupId>
<artifactId>jda-slash-commands</artifactId>
<version>1.2.2</version>
<version>1.3.0</version>
<packaging>jar</packaging>

<name>${project.artifactId}</name>
Expand All @@ -24,11 +24,17 @@
<developer>
<name>Maximilian Dorn</name>
<email>[email protected]</email>
<organization>PypeWare GmbH</organization>
<organizationUrl>https://github.com/pypeware</organizationUrl>
<url>https://cerus.dev</url>
</developer>
</developers>

<contributors>
<contributor>
<name>PringlePot</name>
<url>https://github.com/PringlePot</url>
</contributor>
</contributors>

<scm>
<connection>scm:git:git://github.com/cerus/jda-slash-commands.git</connection>
<developerConnection>scm:git:ssh://github.com:cerus/jda-slash-commands.git</developerConnection>
Expand Down
76 changes: 51 additions & 25 deletions src/main/java/dev/cerus/jdasc/JDASlashCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import dev.cerus.jdasc.command.ApplicationCommandOptionType;
import dev.cerus.jdasc.command.permissions.ApplicationCommandPermissions;
import dev.cerus.jdasc.command.permissions.GuildApplicationCommandPermissions;
import dev.cerus.jdasc.components.Button;
import dev.cerus.jdasc.components.Component;
import dev.cerus.jdasc.components.ComponentListener;
import dev.cerus.jdasc.http.DiscordHttpClient;
import dev.cerus.jdasc.interaction.Interaction;
import dev.cerus.jdasc.interaction.followup.FollowupMessage;
Expand All @@ -20,6 +23,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -50,13 +54,23 @@ public class JDASlashCommands {
private static final Map<Long, ApplicationCommand> discordCommands = new HashMap<>();
private static final Map<Long, Map<Long, ApplicationCommand>> guildCommands = new HashMap<>();
private static final Map<ApplicationCommand, ApplicationCommandListener> commandListenerMap = new HashMap<>();
private static final Map<String, ComponentListener> oneTimeComponentListeners = new HashMap<>();
private static final List<ComponentListener> componentListeners = new ArrayList<>();

private static DiscordHttpClient discordHttpClient;
private static EntityBuilder entityBuilder;

private JDASlashCommands() {
}

public static void addComponentListener(final ComponentListener listener) {
componentListeners.add(listener);
}

public static void addOneTimeComponentListener(final String buttonId, final ComponentListener listener) {
oneTimeComponentListeners.put(buttonId, listener);
}

/**
* Delete a followup message
*
Expand All @@ -79,7 +93,7 @@ public static CompletableFuture<Void> deleteFollowupMessage(final Interaction in
* @return A future
*/
public static CompletableFuture<Void> editFollowupMessage(final Interaction interaction, final long messageId, final MessageEmbed... embeds) {
return editFollowupMessage(interaction, messageId, new FollowupMessage("", false, Arrays.asList(embeds), 0));
return editFollowupMessage(interaction, messageId, new FollowupMessage("", false, Arrays.asList(embeds), 0, Collections.emptyList()));
}

/**
Expand All @@ -93,7 +107,7 @@ public static CompletableFuture<Void> editFollowupMessage(final Interaction inte
* @return A future
*/
public static CompletableFuture<Void> editFollowupMessage(final Interaction interaction, final long messageId, final int flags, final MessageEmbed... embeds) {
return editFollowupMessage(interaction, messageId, new FollowupMessage("", false, Arrays.asList(embeds), flags));
return editFollowupMessage(interaction, messageId, new FollowupMessage("", false, Arrays.asList(embeds), flags, Collections.emptyList()));
}

/**
Expand All @@ -106,7 +120,7 @@ public static CompletableFuture<Void> editFollowupMessage(final Interaction inte
* @return A future
*/
public static CompletableFuture<Void> editFollowupMessage(final Interaction interaction, final long messageId, final String message) {
return editFollowupMessage(interaction, messageId, new FollowupMessage(message, false, new ArrayList<>(), 0));
return editFollowupMessage(interaction, messageId, new FollowupMessage(message, false, new ArrayList<>(), 0, Collections.emptyList()));
}

/**
Expand All @@ -120,7 +134,7 @@ public static CompletableFuture<Void> editFollowupMessage(final Interaction inte
* @return A future
*/
public static CompletableFuture<Void> editFollowupMessage(final Interaction interaction, final long messageId, final String message, final int flags) {
return editFollowupMessage(interaction, messageId, new FollowupMessage(message, false, new ArrayList<>(), flags));
return editFollowupMessage(interaction, messageId, new FollowupMessage(message, false, new ArrayList<>(), flags, Collections.emptyList()));
}

/**
Expand All @@ -146,7 +160,7 @@ public static CompletableFuture<Void> editFollowupMessage(final Interaction inte
* @return The sent message
*/
public static CompletableFuture<Message> submitFollowupMessage(final Interaction interaction, final MessageEmbed... embeds) {
return submitFollowupMessage(interaction, new FollowupMessage("", false, Arrays.asList(embeds), 0));
return submitFollowupMessage(interaction, new FollowupMessage("", false, Arrays.asList(embeds), 0, Collections.emptyList()));
}

/**
Expand All @@ -160,7 +174,7 @@ public static CompletableFuture<Message> submitFollowupMessage(final Interaction
* @return The sent message
*/
public static CompletableFuture<Message> submitFollowupMessage(final Interaction interaction, final int flags, final MessageEmbed... embeds) {
return submitFollowupMessage(interaction, new FollowupMessage("", false, Arrays.asList(embeds), flags));
return submitFollowupMessage(interaction, new FollowupMessage("", false, Arrays.asList(embeds), flags, Collections.emptyList()));
}

/**
Expand All @@ -173,7 +187,7 @@ public static CompletableFuture<Message> submitFollowupMessage(final Interaction
* @return The sent message
*/
public static CompletableFuture<Message> submitFollowupMessage(final Interaction interaction, final String message) {
return submitFollowupMessage(interaction, new FollowupMessage(message, false, new ArrayList<>(), 0));
return submitFollowupMessage(interaction, new FollowupMessage(message, false, new ArrayList<>(), 0, Collections.emptyList()));
}

/**
Expand All @@ -187,7 +201,7 @@ public static CompletableFuture<Message> submitFollowupMessage(final Interaction
* @return The sent message
*/
public static CompletableFuture<Message> submitFollowupMessage(final Interaction interaction, final String message, final int flags) {
return submitFollowupMessage(interaction, new FollowupMessage(message, false, new ArrayList<>(), flags));
return submitFollowupMessage(interaction, new FollowupMessage(message, false, new ArrayList<>(), flags, Collections.emptyList()));
}

/**
Expand Down Expand Up @@ -333,8 +347,7 @@ public static CompletableFuture<Void> deleteInteractionResponse(final Interactio
*/
public static CompletableFuture<Void> editInteractionResponse(final Interaction interaction, final MessageEmbed... embeds) {
return editInteractionResponse(interaction, new InteractionApplicationCommandCallbackData(
false, "", Arrays.asList(embeds), 0
));
false, "", Arrays.asList(embeds), 0));
}

/**
Expand All @@ -350,8 +363,7 @@ public static CompletableFuture<Void> editInteractionResponse(final Interaction
*/
public static CompletableFuture<Void> editInteractionResponse(final Interaction interaction, final String message) {
return editInteractionResponse(interaction, new InteractionApplicationCommandCallbackData(
false, message, new ArrayList<>(), 0
));
false, message, new ArrayList<>(), 0));
}

/**
Expand All @@ -368,8 +380,7 @@ public static CompletableFuture<Void> editInteractionResponse(final Interaction
*/
public static CompletableFuture<Void> editInteractionResponse(final Interaction interaction, final String message, final int flags) {
return editInteractionResponse(interaction, new InteractionApplicationCommandCallbackData(
false, message, new ArrayList<>(), flags
));
false, message, new ArrayList<>(), flags));
}

/**
Expand Down Expand Up @@ -589,18 +600,33 @@ public void onShutdown(@NotNull final ShutdownEvent event) {
* @param interaction The interaction
*/
public static void handleInteraction(final Interaction interaction) {
final ApplicationCommand command = commandMap.get(interaction.getCommandId());
final ApplicationCommandListener listener = commandListenerMap.get(command);

if (command == null || listener == null) {
// Discord sent us a command that wasn't registered. We can't do anything about that so we just do nothing
return;
}
switch (interaction.getType()) {
case APPLICATION_COMMAND:
final ApplicationCommand command = commandMap.get(interaction.getCommandId());
final ApplicationCommandListener listener = commandListenerMap.get(command);

if (command == null || listener == null) {
// Discord sent us a command that wasn't registered. We can't do anything about that so we just do nothing
return;
}

listener.onInteraction(interaction);
final Map<String, InteractionResponseOption> arguments = findArguments(command, interaction);
if (arguments != null && !arguments.isEmpty()) {
listener.handleArguments(interaction, arguments);
listener.onInteraction(interaction);
final Map<String, InteractionResponseOption> arguments = findArguments(command, interaction);
if (arguments != null && !arguments.isEmpty()) {
listener.handleArguments(interaction, arguments);
}
break;
case MESSAGE_COMPONENT:
final Component clickedComponent = interaction.getClickedComponent();
if (clickedComponent instanceof Button) {
final Button button = (Button) clickedComponent;
final ComponentListener componentListener = oneTimeComponentListeners.remove(button.getCustomId());
if (componentListener != null) {
componentListener.onInteraction(interaction);
}
}
componentListeners.forEach(componentListener -> componentListener.onInteraction(interaction));
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface ApplicationCommandListener {
*
* @param interaction The interaction
*/
void onInteraction(Interaction interaction);
void onInteraction(final Interaction interaction);

/**
* Gets called if a argument was specified
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/dev/cerus/jdasc/components/ActionRow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.cerus.jdasc.components;

import java.util.Arrays;
import java.util.List;

public class ActionRow implements Component {

private final List<Component> components;
private final ComponentType type = ComponentType.ACTION_ROW;

public ActionRow(final List<Component> components) {
this.components = components;
}

public static ActionRow of(final Component... components) {
return new ActionRow(Arrays.asList(components));
}

public List<Component> getComponents() {
return this.components;
}

@Override
public ComponentType getType() {
return this.type;
}

}
Loading

0 comments on commit 13bb08e

Please sign in to comment.