Skip to content

Commit

Permalink
published discord api and server instance
Browse files Browse the repository at this point in the history
  • Loading branch information
codejanovic committed Apr 28, 2024
1 parent 2f5e66d commit 2074b55
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Release artifact
<dependency>
<groupId>com.github.zrdj</groupId>
<artifactId>javachord</artifactId>
<version>3.8.0.4</version>
<version>3.8.0.5</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.zrdj</groupId>
<artifactId>javachord</artifactId>
<version>3.8.0.4</version>
<version>3.8.0.5</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/github/zrdj/javachord/DiscordInstanceAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.zrdj.javachord;

import org.javacord.api.DiscordApi;
import org.javacord.api.entity.server.Server;

import java.util.Optional;

public interface DiscordInstanceAware {
default DiscordApi discordInstance() {
return Javachord.Instance.Get.discordInstance;
}
default Optional<Server> serverInstance() {
return Javachord.Instance.Get.serverInstance;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/zrdj/javachord/Javachord.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -42,6 +43,8 @@ enum Instance implements Javachord {
Get;
private final List<ApplicationCommand> _commands = new ArrayList<>();
private final List<GloballyAttachableListener> _listeners = new ArrayList<>();
public DiscordApi discordInstance;
public Optional<Server> serverInstance = Optional.empty();

public void addCommand(final ApplicationCommand command) {
_commands.add(command);
Expand All @@ -50,11 +53,14 @@ public void addListener(final GloballyAttachableListener listener) {
_listeners.add(listener);
}
public void register(final DiscordApi discordApi) {
discordInstance = discordApi;
var commandSet = _commands.stream().map(ApplicationCommand::toSlashCommand).collect(Collectors.toSet());
discordApi.bulkOverwriteGlobalApplicationCommands(commandSet);
_listeners.forEach(discordApi::addListener);
}
public void register(final DiscordApi discordApi, final Server server) {
discordInstance = discordApi;
serverInstance = Optional.of(server);
var commandSet = _commands.stream().map(ApplicationCommand::toSlashCommand).collect(Collectors.toSet());
discordApi.bulkOverwriteServerApplicationCommands(server, commandSet);
_listeners.forEach(discordApi::addListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.zrdj.javachord.Javachord;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.server.Server;
import org.javacord.api.event.interaction.SlashCommandCreateEvent;
import org.javacord.api.interaction.SlashCommand;
import org.javacord.api.interaction.SlashCommandBuilder;
Expand All @@ -18,7 +19,6 @@ abstract class ApplicationCommandBehavior implements ApplicationCommand, SlashCo
protected final String _description;
protected final Optional<ApplicationCommandGroup> _parentCommand;
private final List<ApplicationCommandOption<?>> _options;
protected DiscordApi _discordApi;

ApplicationCommandBehavior(final String name, final String description, ApplicationCommandGroup parentCommand, List<ApplicationCommandOption<?>> options) {
_name = name.contains(" ") ? name.replaceAll(" ", "-") : name;
Expand Down

0 comments on commit 2074b55

Please sign in to comment.