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

Commit 2a30769

Browse files
committed
Merge branch 'develop'
"Merge"
2 parents d21fea1 + fc33756 commit 2a30769

35 files changed

+1112
-193
lines changed

.github/workflows/maven.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- uses: actions/checkout@v2
19-
- name: Set up JDK 11
20-
uses: actions/setup-java@v2
21-
with:
22-
java-version: '11'
23-
distribution: 'adopt'
24-
- name: Build with Maven
25-
run: mvn -B package --file pom.xml
18+
- uses: actions/checkout@v2
19+
- name: Set up JDK 11
20+
uses: actions/setup-java@v2
21+
with:
22+
java-version: '11'
23+
distribution: 'adopt'
24+
- name: Build with Maven
25+
run: mvn -B package --file pom.xml

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ out/
33
target/
44
.idea/
55
*.iml
6-
dependency-reduced-pom.xml
6+
dependency-reduced-pom.xml
7+
src/main/java/dev/cerus/jdasc/test.java

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,36 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66

7-
<groupId>de.cerus</groupId>
7+
<groupId>dev.cerus</groupId>
88
<artifactId>jda-slash-commands</artifactId>
9-
<version>1.1.0</version>
9+
<version>1.2.2</version>
10+
<packaging>jar</packaging>
11+
12+
<name>${project.artifactId}</name>
13+
<description>Discord slash commands for JDA</description>
14+
<url>https://github.com/cerus/jda-slash-commands</url>
15+
16+
<licenses>
17+
<license>
18+
<name>GNU General Public License v3.0</name>
19+
<url>https://www.gnu.org/licenses/gpl-3.0.en.html</url>
20+
</license>
21+
</licenses>
22+
23+
<developers>
24+
<developer>
25+
<name>Maximilian Dorn</name>
26+
<email>[email protected]</email>
27+
<organization>PypeWare GmbH</organization>
28+
<organizationUrl>https://github.com/pypeware</organizationUrl>
29+
</developer>
30+
</developers>
31+
32+
<scm>
33+
<connection>scm:git:git://github.com/cerus/jda-slash-commands.git</connection>
34+
<developerConnection>scm:git:ssh://github.com:cerus/jda-slash-commands.git</developerConnection>
35+
<url>http://github.com/cerus/jda-slash-commands/tree/master</url>
36+
</scm>
1037

1138
<properties>
1239
<maven.compiler.source>1.8</maven.compiler.source>
@@ -30,7 +57,7 @@
3057
<groupId>net.dv8tion</groupId>
3158
<artifactId>JDA</artifactId>
3259
<version>4.2.1_253</version>
33-
<scope>provided</scope>
60+
<scope>compile</scope>
3461
</dependency>
3562

3663
<dependency>
@@ -39,7 +66,11 @@
3966
<version>2.8.6</version>
4067
<scope>compile</scope>
4168
</dependency>
42-
69+
<dependency>
70+
<groupId>com.google.guava</groupId>
71+
<artifactId>guava</artifactId>
72+
<version>18.0</version>
73+
</dependency>
4374
<dependency>
4475
<groupId>co.aikar</groupId>
4576
<artifactId>acf-jda</artifactId>
@@ -55,6 +86,13 @@
5586
</dependency>
5687
</dependencies>
5788

89+
<distributionManagement>
90+
<snapshotRepository>
91+
<id>ossrh</id>
92+
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
93+
</snapshotRepository>
94+
</distributionManagement>
95+
5896
<build>
5997
<plugins>
6098
<plugin>
@@ -92,6 +130,57 @@
92130
</execution>
93131
</executions>
94132
</plugin>
133+
<plugin>
134+
<groupId>org.sonatype.plugins</groupId>
135+
<artifactId>nexus-staging-maven-plugin</artifactId>
136+
<version>1.6.7</version>
137+
<extensions>true</extensions>
138+
<configuration>
139+
<serverId>ossrh</serverId>
140+
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
141+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
142+
</configuration>
143+
</plugin>
144+
<plugin>
145+
<groupId>org.apache.maven.plugins</groupId>
146+
<artifactId>maven-source-plugin</artifactId>
147+
<version>2.2.1</version>
148+
<executions>
149+
<execution>
150+
<id>attach-sources</id>
151+
<goals>
152+
<goal>jar-no-fork</goal>
153+
</goals>
154+
</execution>
155+
</executions>
156+
</plugin>
157+
<plugin>
158+
<groupId>org.apache.maven.plugins</groupId>
159+
<artifactId>maven-javadoc-plugin</artifactId>
160+
<version>2.9.1</version>
161+
<executions>
162+
<execution>
163+
<id>attach-javadocs</id>
164+
<goals>
165+
<goal>jar</goal>
166+
</goals>
167+
</execution>
168+
</executions>
169+
</plugin>
170+
<plugin>
171+
<groupId>org.apache.maven.plugins</groupId>
172+
<artifactId>maven-gpg-plugin</artifactId>
173+
<version>1.5</version>
174+
<executions>
175+
<execution>
176+
<id>sign-artifacts</id>
177+
<phase>verify</phase>
178+
<goals>
179+
<goal>sign</goal>
180+
</goals>
181+
</execution>
182+
</executions>
183+
</plugin>
95184
</plugins>
96185
<defaultGoal>clean package</defaultGoal>
97186
<resources>

src/main/java/de/cerus/jdasc/command/permissions/ApplicationCommandPermissions.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/main/java/de/cerus/jdasc/JDASlashCommands.java renamed to src/main/java/dev/cerus/jdasc/JDASlashCommands.java

Lines changed: 100 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
package de.cerus.jdasc;
1+
package dev.cerus.jdasc;
22

33
import com.google.gson.JsonArray;
44
import com.google.gson.JsonElement;
55
import com.google.gson.JsonObject;
66
import com.google.gson.JsonParser;
7-
import de.cerus.jdasc.command.ApplicationCommand;
8-
import de.cerus.jdasc.command.ApplicationCommandListener;
9-
import de.cerus.jdasc.command.ApplicationCommandOption;
10-
import de.cerus.jdasc.command.ApplicationCommandOptionType;
11-
import de.cerus.jdasc.command.permissions.ApplicationCommandPermissions;
12-
import de.cerus.jdasc.command.permissions.GuildApplicationCommandPermissions;
13-
import de.cerus.jdasc.http.DiscordHttpClient;
14-
import de.cerus.jdasc.interaction.Interaction;
15-
import de.cerus.jdasc.interaction.followup.FollowupMessage;
16-
import de.cerus.jdasc.interaction.response.InteractionApplicationCommandCallbackData;
17-
import de.cerus.jdasc.interaction.response.InteractionResponse;
18-
import de.cerus.jdasc.interaction.response.InteractionResponseOption;
19-
import de.cerus.jdasc.listener.InteractionListener;
7+
import dev.cerus.jdasc.command.ApplicationCommand;
8+
import dev.cerus.jdasc.command.ApplicationCommandListener;
9+
import dev.cerus.jdasc.command.ApplicationCommandOption;
10+
import dev.cerus.jdasc.command.ApplicationCommandOptionType;
11+
import dev.cerus.jdasc.command.permissions.ApplicationCommandPermissions;
12+
import dev.cerus.jdasc.command.permissions.GuildApplicationCommandPermissions;
13+
import dev.cerus.jdasc.http.DiscordHttpClient;
14+
import dev.cerus.jdasc.interaction.Interaction;
15+
import dev.cerus.jdasc.interaction.followup.FollowupMessage;
16+
import dev.cerus.jdasc.interaction.response.InteractionApplicationCommandCallbackData;
17+
import dev.cerus.jdasc.interaction.response.InteractionResponse;
18+
import dev.cerus.jdasc.interaction.response.InteractionResponseOption;
19+
import dev.cerus.jdasc.listener.InteractionListener;
2020
import java.io.IOException;
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
2323
import java.util.HashMap;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.Objects;
2627
import java.util.Set;
2728
import java.util.concurrent.CompletableFuture;
2829
import java.util.concurrent.CompletionException;
@@ -44,7 +45,10 @@
4445

4546
public class JDASlashCommands {
4647

48+
4749
private static final Map<Long, ApplicationCommand> commandMap = new HashMap<>();
50+
private static final Map<Long, ApplicationCommand> discordCommands = new HashMap<>();
51+
private static final Map<Long, Map<Long, ApplicationCommand>> guildCommands = new HashMap<>();
4852
private static final Map<ApplicationCommand, ApplicationCommandListener> commandListenerMap = new HashMap<>();
4953

5054
private static DiscordHttpClient discordHttpClient;
@@ -214,20 +218,35 @@ public static CompletableFuture<Message> submitFollowupMessage(final Interaction
214218
* @return The command id
215219
*/
216220
public static CompletableFuture<Long> submitGlobalCommand(final ApplicationCommand command, final ApplicationCommandListener listener) {
217-
return discordHttpClient.submitGlobalCommand(command).thenApply(response -> {
218-
final JsonObject object;
219-
try {
220-
object = JsonParser.parseString(response.body().string()).getAsJsonObject();
221-
} catch (final IOException e) {
222-
throw new CompletionException(e);
223-
}
221+
if (discordCommands.containsValue(command)) {
222+
return getLongCompletableFuture(command, listener, discordCommands);
223+
} else {
224+
return discordHttpClient.submitGlobalCommand(command).thenApply(response -> {
225+
final JsonObject object;
226+
try {
227+
object = JsonParser.parseString(response.body().string()).getAsJsonObject();
228+
} catch (final IOException e) {
229+
throw new CompletionException(e);
230+
}
231+
232+
return object.get("id").getAsLong();
233+
}).thenApply(commandId -> {
234+
commandMap.put(commandId, command);
235+
commandListenerMap.put(command, listener);
236+
return commandId;
237+
});
238+
}
239+
240+
241+
}
242+
243+
@NotNull
244+
private static CompletableFuture<Long> getLongCompletableFuture(final ApplicationCommand command, final ApplicationCommandListener listener, final Map<Long, ApplicationCommand> commands) {
245+
final Long id = Objects.requireNonNull(commands.values().stream().filter(applicationCommand -> applicationCommand.equals(command)).findFirst().orElse(null)).getId();
246+
commandMap.put(id, command);
247+
commandListenerMap.put(command, listener);
248+
return CompletableFuture.completedFuture(id);
224249

225-
return object.get("id").getAsLong();
226-
}).thenApply(commandId -> {
227-
commandMap.put(commandId, command);
228-
commandListenerMap.put(command, listener);
229-
return commandId;
230-
});
231250
}
232251

233252
/**
@@ -257,20 +276,36 @@ public static CompletableFuture<Long> submitGuildCommand(final ApplicationComman
257276
public static CompletableFuture<Long> submitGuildCommand(final ApplicationCommand command,
258277
final long guildId,
259278
final ApplicationCommandListener listener) {
260-
return discordHttpClient.submitGuildCommand(command, guildId).thenApply(response -> {
261-
final JsonObject object;
262-
try {
263-
object = JsonParser.parseString(response.body().string()).getAsJsonObject();
264-
} catch (final IOException e) {
265-
throw new CompletionException(e);
279+
280+
if (guildCommands.get(guildId) == null || guildCommands.get(guildId).isEmpty()) {
281+
final Map<Long, ApplicationCommand> commandMap = new HashMap<>();
282+
for (final ApplicationCommand applicationCommand : getGuildCommands(guildId).join()) {
283+
commandMap.put(applicationCommand.getId(), applicationCommand);
266284
}
285+
guildCommands.put(guildId, commandMap);
286+
287+
}
288+
289+
if (guildCommands.get(guildId).values().stream().anyMatch(applicationCommand -> applicationCommand.equals(command))) {
290+
return getLongCompletableFuture(command, listener, guildCommands.get(guildId));
291+
} else {
292+
return discordHttpClient.submitGuildCommand(command, guildId).thenApply(response -> {
293+
final JsonObject object;
294+
try {
295+
object = JsonParser.parseString(response.body().string()).getAsJsonObject();
296+
} catch (final IOException e) {
297+
throw new CompletionException(e);
298+
}
299+
300+
return object.get("id").getAsLong();
301+
}).thenApply(commandId -> {
302+
commandMap.put(commandId, command);
303+
commandListenerMap.put(command, listener);
304+
return commandId;
305+
});
306+
}
307+
267308

268-
return object.get("id").getAsLong();
269-
}).thenApply(commandId -> {
270-
commandMap.put(commandId, command);
271-
commandListenerMap.put(command, listener);
272-
return commandId;
273-
});
274309
}
275310

276311

@@ -506,11 +541,23 @@ private static List<ApplicationCommand> parseCommands(final Response response) {
506541
* @param botToken The bot token
507542
* @param applicationId The bot application id
508543
*/
509-
public static void initialize(final JDA jda, final String botToken, final String applicationId) {
544+
public static void initializeBlocking(final JDA jda, final String botToken, final String applicationId) {
545+
initialize(jda, botToken, applicationId).join();
546+
}
547+
548+
/**
549+
* Initialize this library
550+
*
551+
* @param jda The JDA instance
552+
* @param botToken The bot token
553+
* @param applicationId The bot application id
554+
*/
555+
public static CompletableFuture<Void> initialize(final JDA jda, final String botToken, final String applicationId) {
510556
if (jda instanceof JDAImpl && !((JDAImpl) jda).isRawEvents()) {
511557
throw new IllegalStateException("Slash commands will not work without raw events! See JDABuilder#setRawEventsEnabled()");
512558
}
513559

560+
final CompletableFuture<Void> future = new CompletableFuture<>();
514561
discordHttpClient = new DiscordHttpClient(botToken, applicationId, jda);
515562
entityBuilder = new EntityBuilder(jda);
516563

@@ -521,6 +568,19 @@ public void onShutdown(@NotNull final ShutdownEvent event) {
521568
discordHttpClient.shutdown();
522569
}
523570
});
571+
572+
getGlobalCommands().whenComplete((applicationCommands, throwable) ->
573+
applicationCommands.forEach(applicationCommand ->
574+
discordCommands.put(applicationCommand.getId(), applicationCommand)))
575+
.whenComplete((applicationCommands, throwable) -> {
576+
if (throwable != null) {
577+
future.completeExceptionally(throwable);
578+
} else {
579+
future.complete(null);
580+
}
581+
});
582+
583+
return future;
524584
}
525585

526586
/**

0 commit comments

Comments
 (0)