From 2074b558e43494ede178aa961b0434550c3a5fdc Mon Sep 17 00:00:00 2001 From: Aleksandar Damjanovic Date: Sun, 28 Apr 2024 20:26:59 +0200 Subject: [PATCH] published discord api and server instance --- README.md | 2 +- pom.xml | 2 +- .../zrdj/javachord/DiscordInstanceAware.java | 15 +++++++++++++++ .../java/com/github/zrdj/javachord/Javachord.java | 6 ++++++ .../command/ApplicationCommandBehavior.java | 2 +- 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/github/zrdj/javachord/DiscordInstanceAware.java diff --git a/README.md b/README.md index 5db7408..2e80d0f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Release artifact com.github.zrdj javachord - 3.8.0.4 + 3.8.0.5 ``` diff --git a/pom.xml b/pom.xml index 1a04366..210ae84 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.zrdj javachord - 3.8.0.4 + 3.8.0.5 11 diff --git a/src/main/java/com/github/zrdj/javachord/DiscordInstanceAware.java b/src/main/java/com/github/zrdj/javachord/DiscordInstanceAware.java new file mode 100644 index 0000000..e2440d1 --- /dev/null +++ b/src/main/java/com/github/zrdj/javachord/DiscordInstanceAware.java @@ -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 serverInstance() { + return Javachord.Instance.Get.serverInstance; + } +} diff --git a/src/main/java/com/github/zrdj/javachord/Javachord.java b/src/main/java/com/github/zrdj/javachord/Javachord.java index 81075c3..fedf689 100644 --- a/src/main/java/com/github/zrdj/javachord/Javachord.java +++ b/src/main/java/com/github/zrdj/javachord/Javachord.java @@ -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; @@ -42,6 +43,8 @@ enum Instance implements Javachord { Get; private final List _commands = new ArrayList<>(); private final List _listeners = new ArrayList<>(); + public DiscordApi discordInstance; + public Optional serverInstance = Optional.empty(); public void addCommand(final ApplicationCommand command) { _commands.add(command); @@ -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); diff --git a/src/main/java/com/github/zrdj/javachord/command/ApplicationCommandBehavior.java b/src/main/java/com/github/zrdj/javachord/command/ApplicationCommandBehavior.java index 6f52731..085cdc2 100644 --- a/src/main/java/com/github/zrdj/javachord/command/ApplicationCommandBehavior.java +++ b/src/main/java/com/github/zrdj/javachord/command/ApplicationCommandBehavior.java @@ -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; @@ -18,7 +19,6 @@ abstract class ApplicationCommandBehavior implements ApplicationCommand, SlashCo protected final String _description; protected final Optional _parentCommand; private final List> _options; - protected DiscordApi _discordApi; ApplicationCommandBehavior(final String name, final String description, ApplicationCommandGroup parentCommand, List> options) { _name = name.contains(" ") ? name.replaceAll(" ", "-") : name;