getEnabledPermissions() {
+ return enabledPermissions;
+ }
+
+ public LocalizationFunction getLocalizationFunction() {
+ return localizationFunction;
+ }
+
+ /**
+ * Gets the {@link SlashCommand.CommandScope CommandScope} of this command.
+ *
+ * @return the {@link SlashCommand.CommandScope CommandScope} of this command
+ */
+ public SlashCommand.CommandScope getCommandScope() {
+ return scope;
+ }
+
+ @Override
+ public String toString() {
+ return "ContextMenuDefinition{" +
+ "name='" + name + '\'' +
+ ", permissions=" + permissions +
+ ", isGuildOnly=" + isGuildOnly +
+ ", isNSFW=" + isNSFW +
+ ", commandType=" + commandType +
+ ", enabledPermissions=" + enabledPermissions +
+ ", scope=" + scope +
+ ", localizationFunction=" + localizationFunction +
+ ", id='" + id + '\'' +
+ ", method=" + method +
+ '}';
+ }
+}
From 89ea6f1f706988394d1fb3cf0102b7e298677747 Mon Sep 17 00:00:00 2001
From: Kaktushose <42280757+Kaktushose@users.noreply.github.com>
Date: Sun, 29 Oct 2023 13:07:26 +0100
Subject: [PATCH 2/9] implement execution chain for modals
---
.../annotations/interactions/Modal.java | 38 +++++
.../annotations/interactions/TextInput.java | 75 +++++++++
.../dispatching/DispatcherSupervisor.java | 3 +
.../dispatching/ParserSupervisor.java | 3 +
.../dispatching/RuntimeSupervisor.java | 18 +++
.../autocomplete/AutoCompleteDispatcher.java | 3 +-
.../buttons/ButtonDispatcher.java | 4 +-
.../commands/CommandDispatcher.java | 2 -
.../contextmenu/ContextMenuDispatcher.java | 6 +-
.../menus/SelectMenuDispatcher.java | 4 +-
.../interactions/modals/ModalContext.java | 18 +++
.../interactions/modals/ModalDispatcher.java | 93 +++++++++++
.../interactions/modals/ModalEvent.java | 48 ++++++
.../interactions/modals/ModalParser.java | 16 ++
.../dispatching/reply/ReplyContext.java | 5 +-
.../commands/dispatching/reply/Replyable.java | 31 +++-
.../reflect/ControllerDefinition.java | 27 +++-
.../commands/reflect/InteractionRegistry.java | 12 +-
.../commands/reflect/TextInputDefinition.java | 153 ++++++++++++++++++
.../reflect/interactions/ModalDefinition.java | 129 +++++++++++++++
20 files changed, 659 insertions(+), 29 deletions(-)
create mode 100644 src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/Modal.java
create mode 100644 src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/TextInput.java
create mode 100644 src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalContext.java
create mode 100644 src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalDispatcher.java
create mode 100644 src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalEvent.java
create mode 100644 src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalParser.java
create mode 100644 src/main/java/com/github/kaktushose/jda/commands/reflect/TextInputDefinition.java
create mode 100644 src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ModalDefinition.java
diff --git a/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/Modal.java b/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/Modal.java
new file mode 100644
index 00000000..869eba5c
--- /dev/null
+++ b/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/Modal.java
@@ -0,0 +1,38 @@
+package com.github.kaktushose.jda.commands.annotations.interactions;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Methods annotated with Modal will be registered as a modal at startup.
+ *
+ * Therefore the method must be declared inside a class that is annotated with
+ * {@link Interaction}.
+ *
+ * @author Kaktushose
+ * @version 4.0.0
+ * @see Interaction
+ * @see TextInput
+ * @since 4.0.0
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Modal {
+
+ /**
+ * Gets the title of this modal.
+ *
+ * @return the title of the modal
+ */
+ String value();
+
+ /**
+ * Whether this Modal should send ephemeral replies by default.
+ *
+ * @return {@code true} if to send ephemeral replies
+ */
+ boolean ephemeral() default false;
+
+}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/TextInput.java b/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/TextInput.java
new file mode 100644
index 00000000..711df451
--- /dev/null
+++ b/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/TextInput.java
@@ -0,0 +1,75 @@
+package com.github.kaktushose.jda.commands.annotations.interactions;
+
+import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation used to add TextInputs to {@link Modal Modals}.
+ *
+ * @author Kaktushose
+ * @version 4.0.0
+ * @since 4.0.0
+ */
+@Target(ElementType.PARAMETER)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface TextInput {
+
+ /**
+ * The label shown above this text input box
+ *
+ * @return Label for the input
+ */
+ String label() default "";
+
+ /**
+ * The placeholder of this TextInput
+ * This is the short hint that describes the expected value of the TextInput field.
+ *
+ * @return Placeholder
+ */
+ String value() default "";
+
+ /**
+ * The default value of this TextInput.
+ * This sets a pre-populated text for this TextInput field
+ *
+ * @return default value
+ */
+ String defaultValue() default "";
+
+ /**
+ * The minimum length. This is -1 if none has been set.
+ *
+ * @return Minimum length or -1
+ */
+ int minValue() default -1;
+
+ /**
+ * The maximum length. This is -1 if none has been set.
+ *
+ * @return Maximum length or -1
+ */
+ int maxValue() default -1;
+
+ /**
+ * The {@link TextInputStyle TextInputStyle}. The default value is {@link TextInputStyle#PARAGRAPH}.
+ *
+ * @return The TextInputStyle
+ */
+ TextInputStyle style() default TextInputStyle.PARAGRAPH;
+
+ /**
+ * Whether this TextInput is required.
+ * If this is True, the user must populate this TextInput field before they can submit the Modal.
+ *
+ * @return True if this TextInput is required
+ *
+ * @see net.dv8tion.jda.api.interactions.components.text.TextInput#isRequired()
+ */
+ boolean required() default true;
+
+}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/DispatcherSupervisor.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/DispatcherSupervisor.java
index 731bd0af..b979f067 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/DispatcherSupervisor.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/DispatcherSupervisor.java
@@ -13,6 +13,8 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.contextmenu.ContextMenuDispatcher;
import com.github.kaktushose.jda.commands.dispatching.interactions.menus.SelectMenuContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.menus.SelectMenuDispatcher;
+import com.github.kaktushose.jda.commands.dispatching.interactions.modals.ModalContext;
+import com.github.kaktushose.jda.commands.dispatching.interactions.modals.ModalDispatcher;
import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
@@ -49,6 +51,7 @@ public DispatcherSupervisor(JDACommands jdaCommands) {
register(SelectMenuContext.class, new SelectMenuDispatcher(this, runtimeSupervisor));
register(AutoCompleteContext.class, new AutoCompleteDispatcher(this, runtimeSupervisor));
register(ContextMenuContext.class, new ContextMenuDispatcher(this, runtimeSupervisor));
+ register(ModalContext.class, new ModalDispatcher(this, runtimeSupervisor));
}
/**
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/ParserSupervisor.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/ParserSupervisor.java
index c7051839..9608795d 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/ParserSupervisor.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/ParserSupervisor.java
@@ -7,8 +7,10 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandParser;
import com.github.kaktushose.jda.commands.dispatching.interactions.contextmenu.ContextMenuParser;
import com.github.kaktushose.jda.commands.dispatching.interactions.menus.SelectMenuParser;
+import com.github.kaktushose.jda.commands.dispatching.interactions.modals.ModalParser;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
+import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@@ -53,6 +55,7 @@ public ParserSupervisor(@NotNull DispatcherSupervisor dispatcher) {
register(CommandAutoCompleteInteractionEvent.class, new AutoCompleteParser());
register(MessageContextInteractionEvent.class, new ContextMenuParser());
register(UserContextInteractionEvent.class, new ContextMenuParser());
+ register(ModalInteractionEvent.class, new ModalParser());
}
/**
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/RuntimeSupervisor.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/RuntimeSupervisor.java
index 55fd0bf2..fd99979a 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/RuntimeSupervisor.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/RuntimeSupervisor.java
@@ -2,6 +2,7 @@
import com.github.kaktushose.jda.commands.dependency.DependencyInjector;
import com.github.kaktushose.jda.commands.reflect.interactions.GenericInteraction;
+import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent;
@@ -108,6 +109,23 @@ public Optional getRuntime(GenericComponentInteractionCreate
return Optional.ofNullable(runtimes.get(split[2]));
}
+ /**
+ * Gets an {@link Optional} holding the {@link InteractionRuntime}. Returns an empty {@link Optional} if no
+ * {@link InteractionRuntime} has been created yet by calling
+ * {@link #newRuntime(GenericCommandInteractionEvent, GenericInteraction)}, if the underlying component wasn't
+ * created by jda-commands or if the {@link InteractionRuntime} expired.
+ *
+ * @param event the {@link ModalInteractionEvent} to get the {@link InteractionRuntime} for
+ * @return an {@link Optional} holding the {@link InteractionRuntime}
+ */
+ public Optional getRuntime(ModalInteractionEvent event) {
+ String[] split = event.getModalId().split("\\.");
+ if (split.length != 3) {
+ return Optional.empty();
+ }
+ return Optional.ofNullable(runtimes.get(split[2]));
+ }
+
/**
* A runtime used for executing interactions. This class holds the instance of the class annotated with
* {@link com.github.kaktushose.jda.commands.annotations.interactions.Interaction Interaction} where commands,
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/autocomplete/AutoCompleteDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/autocomplete/AutoCompleteDispatcher.java
index 28270542..086db655 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/autocomplete/AutoCompleteDispatcher.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/autocomplete/AutoCompleteDispatcher.java
@@ -35,7 +35,6 @@ public AutoCompleteDispatcher(DispatcherSupervisor supervisor, RuntimeSupervisor
@Override
public void onEvent(AutoCompleteContext context) {
- log.debug("Acknowledging event");
CommandAutoCompleteInteractionEvent event = context.getEvent();
Optional optionalAutoComplete = interactionRegistry.getAutoCompletes().stream()
.filter(it -> it.getCommandNames().contains(event.getFullCommandName()))
@@ -47,6 +46,8 @@ public void onEvent(AutoCompleteContext context) {
}
AutoCompleteDefinition autoComplete = optionalAutoComplete.get();
+ log.debug("Input matches auto complete: {}", autoComplete);
+
log.info("Executing auto complete {} for user {}", autoComplete.getMethod().getName(), event.getMember());
try {
autoComplete.getMethod().invoke(runtimeSupervisor.getOrCreateInstance(event, autoComplete), new AutoCompleteEvent(context));
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/buttons/ButtonDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/buttons/ButtonDispatcher.java
index beb74f66..c11b3ddd 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/buttons/ButtonDispatcher.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/buttons/ButtonDispatcher.java
@@ -44,9 +44,7 @@ public ButtonDispatcher(DispatcherSupervisor supervisor, RuntimeSupervisor runti
*/
@Override
public void onEvent(ButtonContext context) {
- log.debug("Acknowledging event");
ButtonInteractionEvent event = context.getEvent();
- event.deferEdit().queue();
ErrorMessageFactory messageFactory = implementationRegistry.getErrorMessageFactory();
@@ -57,7 +55,7 @@ public void onEvent(ButtonContext context) {
return;
}
InteractionRuntime runtime = optionalRuntime.get();
- log.debug("Found corresponding runtime with id \"{}\"", runtime);
+ log.debug("Found corresponding runtime with id \"{}\"", runtime.getInstanceId());
String[] splitId = event.getButton().getId().split("\\.");
String buttonId = String.format("%s.%s", splitId[0], splitId[1]);
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java
index 094cbd48..f7e74eea 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java
@@ -75,8 +75,6 @@ public void onEvent(CommandContext context) {
context.setCommand(command).setEphemeral(command.isEphemeral());
log.debug("Input matches command: {}", command);
- log.debug("Acknowledging event");
- context.getEvent().deferReply(context.isEphemeral()).queue();
List parameters = new ArrayList<>();
Map options = context.getOptionsAsMap();
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java
index 4f52612f..7c2e983b 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java
@@ -47,12 +47,8 @@ public void onEvent(ContextMenuContext context) {
}
ContextMenuDefinition command = optional.get();
+ context.setEphemeral(command.isEphemeral());
log.debug("Input matches command: {}", command);
-
- log.debug("Acknowledging event");
- context.getEvent().deferReply(context.isEphemeral()).queue();
-
-
log.info("Executing command {} for user {}", command.getMethod().getName(), context.getEvent().getMember());
try {
RuntimeSupervisor.InteractionRuntime runtime = runtimeSupervisor.newRuntime(context.getEvent(), command);
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/menus/SelectMenuDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/menus/SelectMenuDispatcher.java
index 4df9976a..f44bf510 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/menus/SelectMenuDispatcher.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/menus/SelectMenuDispatcher.java
@@ -40,9 +40,7 @@ public SelectMenuDispatcher(DispatcherSupervisor supervisor, RuntimeSupervisor r
*/
@Override
public void onEvent(SelectMenuContext context) {
- log.debug("Acknowledging event");
GenericSelectMenuInteractionEvent, ?> event = context.getEvent();
- event.deferEdit().queue();
ErrorMessageFactory messageFactory = implementationRegistry.getErrorMessageFactory();
@@ -53,7 +51,7 @@ public void onEvent(SelectMenuContext context) {
return;
}
RuntimeSupervisor.InteractionRuntime runtime = optionalRuntime.get();
- log.debug("Found corresponding runtime with id \"{}\"", runtime);
+ log.debug("Found corresponding runtime with id \"{}\"", runtime.getInstanceId());
String[] splitId = event.getComponentId().split("\\.");
String menuId = String.format("%s.%s", splitId[0], splitId[1]);
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalContext.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalContext.java
new file mode 100644
index 00000000..a525e453
--- /dev/null
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalContext.java
@@ -0,0 +1,18 @@
+package com.github.kaktushose.jda.commands.dispatching.interactions.modals;
+
+import com.github.kaktushose.jda.commands.JDACommands;
+import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
+import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
+
+public class ModalContext extends GenericContext {
+
+ /**
+ * Constructs a new ModalContext.
+ *
+ * @param event the corresponding {@link ModalContext}
+ * @param jdaCommands the corresponding {@link JDACommands} instance
+ */
+ public ModalContext(ModalInteractionEvent event, JDACommands jdaCommands) {
+ super(event, jdaCommands);
+ }
+}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalDispatcher.java
new file mode 100644
index 00000000..8fdc5417
--- /dev/null
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalDispatcher.java
@@ -0,0 +1,93 @@
+package com.github.kaktushose.jda.commands.dispatching.interactions.modals;
+
+import com.github.kaktushose.jda.commands.dispatching.DispatcherSupervisor;
+import com.github.kaktushose.jda.commands.dispatching.RuntimeSupervisor;
+import com.github.kaktushose.jda.commands.dispatching.interactions.GenericDispatcher;
+import com.github.kaktushose.jda.commands.dispatching.interactions.buttons.ButtonDispatcher;
+import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
+import com.github.kaktushose.jda.commands.embeds.ErrorMessageFactory;
+import com.github.kaktushose.jda.commands.reflect.interactions.ModalDefinition;
+import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
+import net.dv8tion.jda.api.interactions.modals.ModalMapping;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+public class ModalDispatcher extends GenericDispatcher {
+
+ private static final Logger log = LoggerFactory.getLogger(ButtonDispatcher.class);
+ private final RuntimeSupervisor runtimeSupervisor;
+
+ /**
+ * Constructs a new ButtonDispatcher.
+ *
+ * @param supervisor the {@link DispatcherSupervisor} which supervises this dispatcher.
+ * @param runtimeSupervisor the corresponding {@link RuntimeSupervisor}
+ */
+ public ModalDispatcher(DispatcherSupervisor supervisor, RuntimeSupervisor runtimeSupervisor) {
+ super(supervisor);
+ this.runtimeSupervisor = runtimeSupervisor;
+ }
+ @Override
+ public void onEvent(ModalContext context) {
+ ModalInteractionEvent event = context.getEvent();
+ ErrorMessageFactory messageFactory = implementationRegistry.getErrorMessageFactory();
+
+ Optional optionalRuntime = runtimeSupervisor.getRuntime(event);
+ if (optionalRuntime.isEmpty()) {
+ event.getHook().sendMessage(messageFactory.getUnknownInteractionMessage(context)).setEphemeral(true).queue();
+ return;
+ }
+ RuntimeSupervisor.InteractionRuntime runtime = optionalRuntime.get();
+ log.debug("Found corresponding runtime with id \"{}\"", runtime.getInstanceId());
+
+ String[] splitId = event.getModalId().split("\\.");
+ String modalId = String.format("%s.%s", splitId[0], splitId[1]);
+ Optional optionalModal = interactionRegistry.getModals().stream()
+ .filter(it -> it.getId().equals(modalId))
+ .findFirst();
+
+ if (optionalModal.isEmpty()) {
+ IllegalStateException exception = new IllegalStateException(
+ "No Modal found! Please report this error the the devs of jda-commands."
+ );
+ context.setCancelled(true).setErrorMessage(messageFactory.getCommandExecutionFailedMessage(context, exception));
+ checkCancelled(context);
+ throw exception;
+ }
+
+ ModalDefinition modal = optionalModal.get();
+ context.setEphemeral(modal.isEphemeral());
+ log.debug("Input matches Modal: {}", modal);
+ log.info("Executing Modal {} for user {}", modal.getMethod().getName(), event.getMember());
+ try {
+ context.setRuntime(runtime);
+ List arguments = new ArrayList<>();
+ arguments.add(new ModalEvent(modal, context));
+ arguments.addAll(event.getValues().stream().map(ModalMapping::getAsString).collect(Collectors.toSet()));
+ modal.getMethod().invoke(runtime.getInstance(), arguments.toArray());
+ } catch (Exception exception) {
+ log.error("Button execution failed!", exception);
+ // this unwraps the underlying error in case of an exception inside the command class
+ Throwable throwable = exception instanceof InvocationTargetException ? exception.getCause() : exception;
+ context.setCancelled(true).setErrorMessage(messageFactory.getCommandExecutionFailedMessage(context, throwable));
+ checkCancelled(context);
+ }
+ }
+
+ @SuppressWarnings("ConstantConditions")
+ private boolean checkCancelled(ModalContext context) {
+ if (context.isCancelled()) {
+ ReplyContext replyContext = new ReplyContext(context);
+ replyContext.getBuilder().applyData(context.getErrorMessage());
+ replyContext.setEditReply(false).queue();
+ return true;
+ }
+ return false;
+ }
+}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalEvent.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalEvent.java
new file mode 100644
index 00000000..e9e41844
--- /dev/null
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalEvent.java
@@ -0,0 +1,48 @@
+package com.github.kaktushose.jda.commands.dispatching.interactions.modals;
+
+import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
+import com.github.kaktushose.jda.commands.dispatching.interactions.GenericEvent;
+import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
+import com.github.kaktushose.jda.commands.dispatching.reply.Replyable;
+import com.github.kaktushose.jda.commands.reflect.interactions.ModalDefinition;
+import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
+import net.dv8tion.jda.api.utils.messages.MessageCreateData;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Optional;
+
+public class ModalEvent extends GenericEvent implements Replyable {
+
+ private final ModalContext context;
+ private final ReplyContext replyContext;
+
+ protected ModalEvent(ModalDefinition modal, ModalContext context) {
+ super(GenericEvent.fromEvent(context.getEvent()));
+ this.context = context;
+ replyContext = new ReplyContext(context);
+ }
+
+ @Override
+ public GenericContext extends GenericInteractionCreateEvent> getContext() {
+ return context;
+ }
+
+ @Override
+ public @NotNull ReplyContext getReplyContext() {
+ return replyContext;
+ }
+
+ @Override
+ public void reply() {
+ Optional optional = context.getRuntime().getLatestReply();
+ if (optional.isPresent()) {
+ MessageCreateData cached = optional.get();
+ if (replyContext.isKeepComponents() && replyContext.getBuilder().getComponents().isEmpty()) {
+ replyContext.getBuilder().setComponents(cached.getComponents());
+ }
+
+ }
+ replyContext.setEditReply(false).queue();
+ context.getRuntime().setLatestReply(replyContext.toMessageCreateData());
+ }
+}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalParser.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalParser.java
new file mode 100644
index 00000000..848b04e4
--- /dev/null
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/modals/ModalParser.java
@@ -0,0 +1,16 @@
+package com.github.kaktushose.jda.commands.dispatching.interactions.modals;
+
+import com.github.kaktushose.jda.commands.JDACommands;
+import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
+import com.github.kaktushose.jda.commands.dispatching.interactions.GenericParser;
+import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
+import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
+import org.jetbrains.annotations.NotNull;
+
+public class ModalParser extends GenericParser {
+
+ @Override
+ public @NotNull GenericContext extends GenericInteractionCreateEvent> parse(@NotNull ModalInteractionEvent event, @NotNull JDACommands jdaCommands) {
+ return new ModalContext(event, jdaCommands);
+ }
+}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/reply/ReplyContext.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/reply/ReplyContext.java
index 5bbdb9bc..4e6c8808 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/reply/ReplyContext.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/reply/ReplyContext.java
@@ -210,10 +210,9 @@ public void queue() {
String.format("Cannot reply to '%s'! Please report this error to the jda-commands devs!", event.getClass().getName())
);
}
- // The ReplyContext is also used for error messages and some appear even before acknowledging took place
- // In this case the event gets acknowledged with ephemeral set to false
+
if (!event.isAcknowledged()) {
- callback.deferReply(false).queue();
+ callback.deferReply(ephemeralReply).queue();
}
callback.getHook().setEphemeral(ephemeralReply);
if (editReply) {
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/reply/Replyable.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/reply/Replyable.java
index 13c58fdc..221ae018 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/reply/Replyable.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/reply/Replyable.java
@@ -7,9 +7,11 @@
import com.github.kaktushose.jda.commands.dispatching.reply.components.Buttons;
import com.github.kaktushose.jda.commands.dispatching.reply.components.Component;
import com.github.kaktushose.jda.commands.dispatching.reply.components.SelectMenus;
+import com.github.kaktushose.jda.commands.reflect.interactions.ModalDefinition;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
+import net.dv8tion.jda.api.interactions.callbacks.IModalCallback;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.ItemComponent;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
@@ -196,6 +198,31 @@ default Replyable with(@NotNull Component... components) {
return this;
}
+ /**
+ * Replies to this Interaction with a Modal. This will open a popup on the target user's Discord client.
+ *
+ * @param modal the id of the modal to reply with
+ */
+ default void replyModal(String modal) {
+ IModalCallback callback;
+ GenericContext> context = getContext();
+ GenericInteractionCreateEvent event = context.getEvent();
+ if (event instanceof IModalCallback) {
+ callback = (IModalCallback) event;
+ } else {
+ throw new IllegalArgumentException(
+ String.format("Cannot reply to '%s'! Please report this error to the jda-commands devs!", event.getClass().getName())
+ );
+ }
+
+ String id = String.format("%s.%s", context.getInteraction().getMethod().getDeclaringClass().getSimpleName(), modal);
+
+ ModalDefinition modalDefinition = context.getJdaCommands().getInteractionRegistry().getModals().stream()
+ .filter(it -> it.getId().equals(id)).findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown Modal"));
+
+ callback.replyModal(modalDefinition.toModal(modalDefinition.getRuntimeId(context))).queue();
+ }
+
GenericContext extends GenericInteractionCreateEvent> getContext();
/**
@@ -245,7 +272,7 @@ default Replyable setEphemeral(boolean ephemeral) {
* @param success the callback consumer
* @return the current instance for fluent interface
*/
- private Replyable setSuccessCallback(Consumer success) {
+ default Replyable setSuccessCallback(Consumer success) {
getReplyContext().setSuccessCallback(success);
return this;
}
@@ -256,7 +283,7 @@ private Replyable setSuccessCallback(Consumer success) {
* @param failure the callback consumer
* @return the current instance for fluent interface
*/
- private Replyable setFailureCallback(Consumer failure) {
+ default Replyable setFailureCallback(Consumer failure) {
getReplyContext().setFailureCallback(failure);
return this;
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java
index f9584cc6..f1b80859 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java
@@ -4,10 +4,7 @@
import com.github.kaktushose.jda.commands.annotations.interactions.*;
import com.github.kaktushose.jda.commands.dependency.DependencyInjector;
import com.github.kaktushose.jda.commands.dispatching.validation.ValidatorRegistry;
-import com.github.kaktushose.jda.commands.reflect.interactions.AutoCompleteDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.ButtonDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.ContextMenuDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.*;
import com.github.kaktushose.jda.commands.reflect.interactions.menus.EntitySelectMenuDefinition;
import com.github.kaktushose.jda.commands.reflect.interactions.menus.GenericSelectMenuDefinition;
import com.github.kaktushose.jda.commands.reflect.interactions.menus.StringSelectMenuDefinition;
@@ -38,16 +35,20 @@ public class ControllerDefinition {
private final List> selectMenus;
private final List autoCompletes;
private final Collection contextMenus;
+ private final List modals;
private ControllerDefinition(List commands,
List buttons,
List> selectMenus,
- List autoCompletes, Collection contextMenus) {
+ List autoCompletes,
+ Collection contextMenus,
+ List modals) {
this.commands = commands;
this.buttons = buttons;
this.selectMenus = selectMenus;
this.autoCompletes = autoCompletes;
this.contextMenus = contextMenus;
+ this.modals = modals;
}
/**
@@ -98,6 +99,7 @@ public static Optional build(@NotNull Class> controllerC
List> selectMenus = new ArrayList<>();
List contextMenus = new ArrayList<>();
List autoCompletes = new ArrayList<>();
+ List modals = new ArrayList<>();
for (Method method : controllerClass.getDeclaredMethods()) {
if (method.isAnnotationPresent(SlashCommand.class)) {
@@ -168,9 +170,18 @@ public static Optional build(@NotNull Class> controllerC
contextMenus.add(menu);
});
}
+
+ if (method.isAnnotationPresent(Modal.class)) {
+ ModalDefinition.build(method).ifPresent(modal -> {
+ if (interaction.ephemeral()) {
+ modal.setEphemeral(true);
+ }
+ modals.add(modal);
+ });
+ }
}
- return Optional.of(new ControllerDefinition(commands, buttons, selectMenus, autoCompletes, contextMenus));
+ return Optional.of(new ControllerDefinition(commands, buttons, selectMenus, autoCompletes, contextMenus, modals));
}
/**
@@ -208,6 +219,10 @@ public Collection getContextMenus() {
return contextMenus;
}
+ public List getModals() {
+ return modals;
+ }
+
@Override
public String toString() {
return "ControllerDefinition{" +
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionRegistry.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionRegistry.java
index 802572ec..38e7b95d 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionRegistry.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionRegistry.java
@@ -3,10 +3,7 @@
import com.github.kaktushose.jda.commands.annotations.interactions.Interaction;
import com.github.kaktushose.jda.commands.dependency.DependencyInjector;
import com.github.kaktushose.jda.commands.dispatching.validation.ValidatorRegistry;
-import com.github.kaktushose.jda.commands.reflect.interactions.AutoCompleteDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.ButtonDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.ContextMenuDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.*;
import com.github.kaktushose.jda.commands.reflect.interactions.menus.GenericSelectMenuDefinition;
import net.dv8tion.jda.api.interactions.commands.localization.LocalizationFunction;
import net.dv8tion.jda.api.interactions.components.selections.SelectMenu;
@@ -43,6 +40,7 @@ public class InteractionRegistry {
private final Set> selectMenus;
private final Set autoCompletes;
private final Set contextMenus;
+ private final Set modals;
/**
* Constructs a new CommandRegistry.
@@ -63,6 +61,7 @@ public InteractionRegistry(@NotNull ValidatorRegistry validatorRegistry,
selectMenus = new HashSet<>();
autoCompletes = new HashSet<>();
contextMenus = new HashSet<>();
+ modals = new HashSet<>();
}
/**
@@ -109,6 +108,7 @@ public void index(@NotNull Class> clazz, @NotNull String... packages) {
selectMenus.addAll(controller.getSelectMenus());
autoCompletes.addAll(controller.getAutoCompletes());
contextMenus.addAll(controller.getContextMenus());
+ modals.addAll(controller.getModals());
log.debug("Registered controller {}", controller);
}
@@ -165,4 +165,8 @@ public Set> getSelectMenus() {
public Set getContextMenus() {
return Collections.unmodifiableSet(contextMenus);
}
+
+ public Set getModals() {
+ return Collections.unmodifiableSet(modals);
+ }
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/TextInputDefinition.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/TextInputDefinition.java
new file mode 100644
index 00000000..634e4f23
--- /dev/null
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/TextInputDefinition.java
@@ -0,0 +1,153 @@
+package com.github.kaktushose.jda.commands.reflect;
+
+import com.github.kaktushose.jda.commands.annotations.interactions.TextInput;
+import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
+import org.jetbrains.annotations.NotNull;
+import net.dv8tion.jda.api.interactions.components.text.TextInput.Builder;
+
+import java.lang.reflect.Parameter;
+import java.util.Optional;
+
+/**
+ * Representation of a {@link TextInput} of a
+ * {@link com.github.kaktushose.jda.commands.annotations.interactions.Modal Modal}.
+ *
+ * @see TextInput
+ * @see com.github.kaktushose.jda.commands.annotations.interactions.Modal Modal
+ * @version 4.0.0
+ * @since 4.0.0
+ * @author Kaktushose
+ */
+public class TextInputDefinition {
+
+ private final String label;
+ private final String placeholder;
+ private final String defaultValue;
+ private final int minValue;
+ private final int maxValue;
+ private final TextInputStyle style;
+ private final boolean required;
+
+ private TextInputDefinition(String label,
+ String placeholder,
+ String defaultValue,
+ int minValue,
+ int maxValue,
+ TextInputStyle style,
+ boolean required) {
+ this.label = label;
+ this.placeholder = placeholder;
+ this.defaultValue = defaultValue;
+ this.minValue = minValue;
+ this.maxValue = maxValue;
+ this.style = style;
+ this.required = required;
+ }
+
+ /**
+ * Builds a new TextInputDefinition.
+ *
+ * @param parameter the {@link Parameter} of the text input
+ * @return an {@link Optional} holding the TextInputDefinition
+ */
+ public static Optional build(@NotNull Parameter parameter) {
+ if (!parameter.isAnnotationPresent(TextInput.class)) {
+ return Optional.empty();
+ }
+
+ // TODO for now we only allow Strings, maybe add type adapting in the future
+ if (!String.class.isAssignableFrom(parameter.getType())) {
+ return Optional.empty();
+ }
+
+ TextInput textInput = parameter.getAnnotation(TextInput.class);
+
+ return Optional.of(new TextInputDefinition(
+ textInput.label().isEmpty() ? parameter.getName() : textInput.label(),
+ textInput.value(),
+ textInput.defaultValue(),
+ textInput.minValue(),
+ textInput.maxValue(),
+ textInput.style(),
+ textInput.required()
+ ));
+ }
+
+ /**
+ * Transforms this TextInputDefinition to a {@link net.dv8tion.jda.api.interactions.components.text.TextInput TextInput}.
+ *
+ * @return the transformed {@link net.dv8tion.jda.api.interactions.components.text.TextInput TextInput}
+ */
+ public net.dv8tion.jda.api.interactions.components.text.TextInput toTextInput() {
+ Builder textInput = net.dv8tion.jda.api.interactions.components.text.TextInput.create(label, label, style).setRequired(required);
+
+ if (minValue != -1) {
+ textInput.setMinLength(minValue);
+ }
+ if (maxValue != -1) {
+ textInput.setMaxLength(maxValue);
+ }
+ if (!placeholder.isBlank()) {
+ textInput.setPlaceholder(placeholder);
+ }
+ if (!placeholder.isBlank()) {
+ textInput.setValue(defaultValue);
+ }
+
+ return textInput.build();
+ }
+
+ /**
+ * Gets the label of the TextInput.
+ *
+ * @return the label of the TextInput
+ */
+ public String getLabel() {
+ return label;
+ }
+
+ /**
+ * Gets the placeholder of the TextInput.
+ *
+ * @return the placeholder of the TextInput
+ */
+ public String getPlaceholder() {
+ return placeholder;
+ }
+
+ /**
+ * Gets the minimum value of the TextInput.
+ *
+ * @return the minimum value of the TextInput
+ */
+ public int getMinValue() {
+ return minValue;
+ }
+
+ /**
+ * Gets the maximum value of the TextInput.
+ *
+ * @return the maximum value of the TextInput
+ */
+ public int getMaxValue() {
+ return maxValue;
+ }
+
+ /**
+ * Gets the {@link TextInputStyle} of the TextInput.
+ *
+ * @return the {@link TextInputStyle} of the TextInput
+ */
+ public TextInputStyle getStyle() {
+ return style;
+ }
+
+ /**
+ * Whether this TextInput is required.
+ *
+ * @return {@code true} if this TextInput is required
+ */
+ public boolean isRequired() {
+ return required;
+ }
+}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ModalDefinition.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ModalDefinition.java
new file mode 100644
index 00000000..74a62012
--- /dev/null
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ModalDefinition.java
@@ -0,0 +1,129 @@
+package com.github.kaktushose.jda.commands.reflect.interactions;
+
+import com.github.kaktushose.jda.commands.annotations.interactions.Interaction;
+import com.github.kaktushose.jda.commands.annotations.interactions.Modal;
+import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
+import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
+import com.github.kaktushose.jda.commands.dispatching.interactions.modals.ModalEvent;
+import com.github.kaktushose.jda.commands.reflect.TextInputDefinition;
+import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
+import net.dv8tion.jda.api.interactions.modals.Modal.Builder;
+import org.jetbrains.annotations.NotNull;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Parameter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Representation of a Modal.
+ *
+ * @author Kaktushose
+ * @version 4.0.0
+ * @see Modal
+ * @since 4.0.0
+ */
+public class ModalDefinition extends EphemeralInteraction {
+
+ private final String title;
+ private final List textInputs;
+
+ protected ModalDefinition(Method method, boolean ephemeral, String title, List textInputs) {
+ super(method, ephemeral);
+ this.title = title;
+ this.textInputs = textInputs;
+ }
+
+ /**
+ * Builds a new ModalDefinition.
+ *
+ * @param method the {@link Method} of the Modal
+ * @return an {@link Optional} holding the ModalDefinition
+ */
+ public static Optional build(@NotNull Method method) {
+ if (!method.isAnnotationPresent(Modal.class) || !method.getDeclaringClass().isAnnotationPresent(Interaction.class)) {
+ return Optional.empty();
+ }
+
+ // Modals support up to 5 TextInputs
+ if (method.getParameters().length < 1 || method.getParameters().length > 6) {
+ log.error("An error has occurred! Skipping Modal {}.{}:",
+ method.getDeclaringClass().getSimpleName(),
+ method.getName(),
+ new IllegalArgumentException("Invalid amount of parameters!"));
+ return Optional.empty();
+ }
+
+ if (!ModalEvent.class.isAssignableFrom(method.getParameters()[0].getType())) {
+ log.error("An error has occurred! Skipping Modal {}.{}:",
+ method.getDeclaringClass().getSimpleName(),
+ method.getName(),
+ new IllegalArgumentException(String.format("First parameter must be of type %s!", ModalEvent.class.getSimpleName())));
+ return Optional.empty();
+ }
+
+ List textInputs = new ArrayList<>();
+ for (int i = 1; i < method.getParameters().length; i++) {
+ Parameter parameter = method.getParameters()[i];
+ TextInputDefinition.build(parameter).ifPresent(textInputs::add);
+ }
+
+ if (textInputs.isEmpty()) {
+ log.error("An error has occurred! Skipping Modal {}.{}:",
+ method.getDeclaringClass().getSimpleName(),
+ method.getName(),
+ new IllegalArgumentException("Modals need at least one valid TextInput"));
+ return Optional.empty();
+ }
+
+ Modal modal = method.getAnnotation(Modal.class);
+
+ return Optional.of(new ModalDefinition(method, modal.ephemeral(), modal.value(), textInputs));
+ }
+
+ /**
+ * Transforms this ModalDefinition to a {@link net.dv8tion.jda.api.interactions.modals.Modal Modal}.
+ *
+ * @param id the id to use, see {@link #getRuntimeId(GenericContext)}
+ * @return the transformed {@link net.dv8tion.jda.api.interactions.modals.Modal Modal}
+ */
+ public net.dv8tion.jda.api.interactions.modals.Modal toModal(String id) {
+ Builder modal = net.dv8tion.jda.api.interactions.modals.Modal.create(id, title);
+
+ textInputs.forEach(textInput -> modal.addActionRow(textInput.toTextInput()));
+
+ return modal.build();
+ }
+
+ /**
+ * Gets the title of the modal.
+ *
+ * @return the title of the modal
+ */
+ public String getTitle() {
+ return title;
+ }
+
+ /**
+ * Gets all text inputs this modal uses
+ *
+ * @return a list of {@link TextInputDefinition TextInputDefinitions}
+ */
+ public List getTextInputs() {
+ return textInputs;
+ }
+
+ /**
+ * Gets the runtime id. The runtime id is composed of the static interaction id and the
+ * snowflake id of the interaction event that created the runtime.
+ *
+ * @param context the {@link CommandContext} this button will be attached to
+ * @return the runtime id
+ */
+ @NotNull
+ public String getRuntimeId(GenericContext extends GenericInteractionCreateEvent> context) {
+ return String.format("%s.%s", getId(), context.getRuntime().getInstanceId());
+ }
+
+}
From adbdc4190ee87549677303869868acebb2887d18 Mon Sep 17 00:00:00 2001
From: Kaktushose <42280757+Kaktushose@users.noreply.github.com>
Date: Sun, 29 Oct 2023 13:27:02 +0100
Subject: [PATCH 3/9] allow top-level command names for autocomplete
registration
---
.../reflect/ControllerDefinition.java | 43 +++++++++++--------
.../interactions/AutoCompleteDefinition.java | 11 ++++-
2 files changed, 36 insertions(+), 18 deletions(-)
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java
index f1b80859..52b60850 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java
@@ -130,7 +130,6 @@ public static Optional build(@NotNull Class> controllerC
buttons.add(button);
});
}
-
if (method.isAnnotationPresent(EntitySelectMenu.class)) {
EntitySelectMenuDefinition.build(method).ifPresent(menu -> {
if (interaction.ephemeral()) {
@@ -147,21 +146,6 @@ public static Optional build(@NotNull Class> controllerC
selectMenus.add(menu);
});
}
-
- if (method.isAnnotationPresent(AutoComplete.class)) {
- AutoCompleteDefinition.build(
- method,
- autoCompletes.stream().flatMap(it -> it.getCommandNames().stream()).collect(Collectors.toList())
- ).ifPresent(autoComplete -> {
- autoCompletes.add(autoComplete);
-
- autoComplete.getCommandNames().forEach(name ->
- commands.stream().filter(it -> it.getName().equals(name))
- .findFirst().ifPresent(it -> it.setAutoComplete(true))
- );
- });
- }
-
if (method.isAnnotationPresent(ContextMenu.class)) {
ContextMenuDefinition.build(method, localizationFunction).ifPresent(menu -> {
if (interaction.ephemeral()) {
@@ -170,7 +154,6 @@ public static Optional build(@NotNull Class> controllerC
contextMenus.add(menu);
});
}
-
if (method.isAnnotationPresent(Modal.class)) {
ModalDefinition.build(method).ifPresent(modal -> {
if (interaction.ephemeral()) {
@@ -181,6 +164,32 @@ public static Optional build(@NotNull Class> controllerC
}
}
+ //loop again once all commands got indexed because we need all commands for autocomplete registration to work
+ for (Method method : controllerClass.getDeclaredMethods()) {
+ if (method.isAnnotationPresent(AutoComplete.class)) {
+ AutoCompleteDefinition.build(
+ method,
+ autoCompletes.stream().flatMap(it -> it.getCommandNames().stream()).collect(Collectors.toList())
+ ).ifPresent(autoComplete -> {
+ autoCompletes.add(autoComplete);
+
+ Set commandNames = new HashSet<>();
+ autoComplete.getCommandNames().forEach(name -> commands.stream()
+ .filter(command -> {
+ System.out.println(command.getName());
+ System.out.println(command.getName().startsWith(name));
+ return command.getName().startsWith(name);
+ })
+ .forEach(command -> {
+ command.setAutoComplete(true);
+ commandNames.add(command.getName());
+ })
+ );
+ autoComplete.setCommandNames(commandNames);
+ });
+ }
+ }
+
return Optional.of(new ControllerDefinition(commands, buttons, selectMenus, autoCompletes, contextMenus, modals));
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/AutoCompleteDefinition.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/AutoCompleteDefinition.java
index e1256a05..b94704ad 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/AutoCompleteDefinition.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/AutoCompleteDefinition.java
@@ -20,7 +20,7 @@
*/
public class AutoCompleteDefinition extends GenericInteraction {
- private final Set commands;
+ private Set commands;
protected AutoCompleteDefinition(Method method, Set commands) {
super(method);
@@ -69,6 +69,15 @@ public static Optional build(@NotNull Method method, Lis
return Optional.of(new AutoCompleteDefinition(method, values));
}
+ /**
+ * Set the command names this AutoComplete can handle
+ *
+ * @param commands a set of command names
+ */
+ public void setCommandNames(Set commands) {
+ this.commands = commands;
+ }
+
/**
* Gets a set of names of all the commands this AutoComplete can handle
*
From 73ab769ed3f25d0097a3db7e9e22ca6e94635abc Mon Sep 17 00:00:00 2001
From: Kaktushose <42280757+Kaktushose@users.noreply.github.com>
Date: Sun, 29 Oct 2023 13:56:46 +0100
Subject: [PATCH 4/9] rename interaction definitions to be more consistent
---
.../jda/commands/SlashCommandUpdater.java | 20 +++---
.../jda/commands/data/CommandTree.java | 18 ++---
.../jda/commands/data/TreeNode.java | 22 +++---
.../adapter/TypeAdapterRegistry.java | 6 +-
.../filter/impl/ConstraintFilter.java | 4 +-
.../filter/impl/CooldownFilter.java | 14 ++--
.../filter/impl/DirectMessageFilter.java | 6 +-
.../interactions/commands/CommandContext.java | 16 ++---
.../commands/CommandDispatcher.java | 6 +-
.../interactions/commands/CommandEvent.java | 16 ++---
.../contextmenu/ContextMenuDispatcher.java | 6 +-
.../contextmenu/ContextMenuEvent.java | 6 +-
.../embeds/DefaultErrorMessageFactory.java | 6 +-
.../embeds/JsonErrorMessageFactory.java | 6 +-
...nition.java => InteractionDefinition.java} | 68 +++++++++----------
.../commands/reflect/InteractionRegistry.java | 26 +++----
...ion.java => ContextCommandDefinition.java} | 26 +++----
.../interactions/EphemeralInteraction.java | 10 +++
...ition.java => SlashCommandDefinition.java} | 36 +++++-----
.../adapting/TypeAdapterRegistryTest.java | 8 +--
...t.java => SlashCommandDefinitionTest.java} | 32 ++++-----
...st.java => InteractionDefinitionTest.java} | 18 ++---
22 files changed, 193 insertions(+), 183 deletions(-)
rename src/main/java/com/github/kaktushose/jda/commands/reflect/{ControllerDefinition.java => InteractionDefinition.java} (74%)
rename src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/{ContextMenuDefinition.java => ContextCommandDefinition.java} (84%)
rename src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/{CommandDefinition.java => SlashCommandDefinition.java} (90%)
rename src/test/java/commands/{CommandDefinitionTest.java => SlashCommandDefinitionTest.java} (78%)
rename src/test/java/controller/{ControllerDefinitionTest.java => InteractionDefinitionTest.java} (75%)
diff --git a/src/main/java/com/github/kaktushose/jda/commands/SlashCommandUpdater.java b/src/main/java/com/github/kaktushose/jda/commands/SlashCommandUpdater.java
index 9088d514..2033f59b 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/SlashCommandUpdater.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/SlashCommandUpdater.java
@@ -2,8 +2,8 @@
import com.github.kaktushose.jda.commands.annotations.interactions.SlashCommand;
import com.github.kaktushose.jda.commands.data.CommandTree;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.ContextMenuDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.ContextCommandDefinition;
import com.github.kaktushose.jda.commands.scope.GuildScopeProvider;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
@@ -16,7 +16,7 @@
/**
* Class that sends the {@link SlashCommandData} to Discord. Uses a {@link CommandTree} to properly transpile all
- * {@link CommandDefinition CommandDefinitions} to {@link SlashCommandData}.
+ * {@link SlashCommandDefinition CommandDefinitions} to {@link SlashCommandData}.
*
* @author Kaktushose
* @version 4.0.0
@@ -27,16 +27,16 @@ public class SlashCommandUpdater {
private static final Logger log = LoggerFactory.getLogger(SlashCommandUpdater.class);
private final JDAContext jdaContext;
- private final Collection commands;
+ private final Collection commands;
private final GuildScopeProvider guildScopeProvider;
- private final Collection contextMenus;
+ private final Collection contextMenus;
/**
* Constructs a new SlashCommandUpdater.
*
* @param jdaCommands the corresponding {@link JDACommands} instance
*/
- public SlashCommandUpdater(JDACommands jdaCommands, Collection commands, Collection contextMenus) {
+ public SlashCommandUpdater(JDACommands jdaCommands, Collection commands, Collection contextMenus) {
this.jdaContext = jdaCommands.getJdaContext();
this.commands = commands;
this.contextMenus = contextMenus;
@@ -57,7 +57,7 @@ public void updateAllCommands() {
*/
public void updateGuildCommands() {
log.debug("Updating guild slash commands...");
- Set globalCommands = commands.stream()
+ Set globalCommands = commands.stream()
.filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GUILD)
.collect(Collectors.toSet());
CommandTree tree = new CommandTree(globalCommands);
@@ -80,7 +80,7 @@ public void updateGuildCommands() {
});
}
- for (ContextMenuDefinition command : contextMenus.stream().filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GUILD).collect(Collectors.toSet())) {
+ for (ContextCommandDefinition command : contextMenus.stream().filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GUILD).collect(Collectors.toSet())) {
// create a copy so that a user doesn't modify the command data used for registration
Set guildIds = guildScopeProvider.getGuildsForCommand(CommandData.fromData(command.toCommandData().toData()));
if (guildIds.isEmpty()) {
@@ -106,7 +106,7 @@ public void updateGuildCommands() {
*/
public void updateGlobalCommands() {
log.debug("Updating global slash commands...");
- Set globalCommands = commands.stream()
+ Set globalCommands = commands.stream()
.filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GLOBAL)
.collect(Collectors.toSet());
CommandTree tree = new CommandTree(globalCommands);
@@ -114,7 +114,7 @@ public void updateGlobalCommands() {
Collection labels = tree.getNames();
log.debug("Using commands: " + labels);
jdaContext.performTask(jda -> jda.updateCommands().addCommands(tree.getCommands()).queue());
- jdaContext.performTask(jda -> jda.updateCommands().addCommands(contextMenus.stream().filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GLOBAL).map(ContextMenuDefinition::toCommandData).collect(Collectors.toSet())).queue());
+ jdaContext.performTask(jda -> jda.updateCommands().addCommands(contextMenus.stream().filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GLOBAL).map(ContextCommandDefinition::toCommandData).collect(Collectors.toSet())).queue());
log.debug("Done!");
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/data/CommandTree.java b/src/main/java/com/github/kaktushose/jda/commands/data/CommandTree.java
index 4b8d979d..f1c99197 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/data/CommandTree.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/data/CommandTree.java
@@ -1,6 +1,6 @@
package com.github.kaktushose.jda.commands.data;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import java.util.Collection;
@@ -30,29 +30,29 @@ public CommandTree() {
/**
* Constructs a new CommandTree.
*/
- public CommandTree(Collection commands) {
+ public CommandTree(Collection commands) {
root = new TreeNode();
addAll(commands);
}
/**
- * Adds a {@link CommandDefinition} to the {@link CommandTree}. The label of the {@link CommandDefinition} will be
+ * Adds a {@link SlashCommandDefinition} to the {@link CommandTree}. The label of the {@link SlashCommandDefinition} will be
* sanitized to match the regex {@code ^[\w-]+$}. Furthermore, if the label consists of more than three spaces any
* additional space will be replaced with {@code _} due to Discords limitations on SubcommandGroups.
*
- * @param command the {@link CommandDefinition} to add
+ * @param command the {@link SlashCommandDefinition} to add
*/
- public void add(CommandDefinition command) {
+ public void add(SlashCommandDefinition command) {
root.addChild(resolveLabel(command.getName()), command);
}
/**
- * Adds all {@link CommandDefinition CommandDefinitions} of the {@link Collection} to the {@link CommandTree}.
+ * Adds all {@link SlashCommandDefinition CommandDefinitions} of the {@link Collection} to the {@link CommandTree}.
*
- * @param commands a {@link Collection} of {@link CommandDefinition CommandDefinitions} to add
- * @see #add(CommandDefinition)
+ * @param commands a {@link Collection} of {@link SlashCommandDefinition CommandDefinitions} to add
+ * @see #add(SlashCommandDefinition)
*/
- public void addAll(Collection commands) {
+ public void addAll(Collection commands) {
commands.forEach(this::add);
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/data/TreeNode.java b/src/main/java/com/github/kaktushose/jda/commands/data/TreeNode.java
index 97b3572d..2cbb4628 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/data/TreeNode.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/data/TreeNode.java
@@ -1,7 +1,7 @@
package com.github.kaktushose.jda.commands.data;
import com.github.kaktushose.jda.commands.SlashCommandUpdater;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData;
@@ -24,7 +24,7 @@ public class TreeNode implements Iterable {
private static final Logger log = LoggerFactory.getLogger(SlashCommandUpdater.class);
private final String name;
- private final CommandDefinition command;
+ private final SlashCommandDefinition command;
private final List children;
/**
@@ -38,9 +38,9 @@ public TreeNode() {
* Constructs a new TreeNode.
*
* @param name the name of the command
- * @param command the {@link CommandDefinition}
+ * @param command the {@link SlashCommandDefinition}
*/
- public TreeNode(@NotNull String name, @Nullable CommandDefinition command) {
+ public TreeNode(@NotNull String name, @Nullable SlashCommandDefinition command) {
this.name = name;
this.command = command;
children = new ArrayList<>();
@@ -57,9 +57,9 @@ public TreeNode(@NotNull String name, @Nullable CommandDefinition command) {
* This guarantees to create a {@link CommandTree} that respects Subcommands and SubcommandGroups.
*
* @param labels an Array of all labels, can be empty
- * @param command the {@link CommandDefinition} to add
+ * @param command the {@link SlashCommandDefinition} to add
*/
- public void addChild(@NotNull String[] labels, @NotNull CommandDefinition command) {
+ public void addChild(@NotNull String[] labels, @NotNull SlashCommandDefinition command) {
if (labels.length < 1) {
return;
}
@@ -89,9 +89,9 @@ public Optional getChild(String name) {
}
/**
- * Gets the label of the {@link CommandDefinition} of this {@link TreeNode}.
+ * Gets the label of the {@link SlashCommandDefinition} of this {@link TreeNode}.
*
- * @return the label of the {@link CommandDefinition}
+ * @return the label of the {@link SlashCommandDefinition}
*/
public String getName() {
return name;
@@ -116,12 +116,12 @@ public boolean hasChildren() {
}
/**
- * Gets the {@link CommandDefinition} of this {@link TreeNode}. Returns an empty {@link Optional} if one or more
- * children exist or if the {@link CommandDefinition} is {@code null}.
+ * Gets the {@link SlashCommandDefinition} of this {@link TreeNode}. Returns an empty {@link Optional} if one or more
+ * children exist or if the {@link SlashCommandDefinition} is {@code null}.
*
* @return an {@link Optional} holding the result
*/
- public Optional getCommand() {
+ public Optional getCommand() {
if (children.size() > 0) {
return Optional.empty();
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/adapter/TypeAdapterRegistry.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/adapter/TypeAdapterRegistry.java
index 3f251c95..e8978cf0 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/adapter/TypeAdapterRegistry.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/adapter/TypeAdapterRegistry.java
@@ -5,7 +5,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import com.github.kaktushose.jda.commands.embeds.ErrorMessageFactory;
import com.github.kaktushose.jda.commands.reflect.ParameterDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.User;
@@ -129,12 +129,12 @@ public Optional> get(@Nullable Class> type) {
/**
* Takes a {@link CommandContext} and attempts to type adapt the command input to the type specified by the
- * {@link CommandDefinition}. Cancels the {@link CommandContext} if the type adapting fails.
+ * {@link SlashCommandDefinition}. Cancels the {@link CommandContext} if the type adapting fails.
*
* @param context the {@link CommandContext} to type adapt
*/
public void adapt(@NotNull CommandContext context) {
- CommandDefinition command = Objects.requireNonNull(context.getCommand());
+ SlashCommandDefinition command = Objects.requireNonNull(context.getCommand());
List arguments = new ArrayList<>();
String[] input = context.getInput();
ErrorMessageFactory messageFactory = context.getImplementationRegistry().getErrorMessageFactory();
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/ConstraintFilter.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/ConstraintFilter.java
index 0416cc84..fb4c6bbf 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/ConstraintFilter.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/ConstraintFilter.java
@@ -5,7 +5,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
import com.github.kaktushose.jda.commands.reflect.ConstraintDefinition;
import com.github.kaktushose.jda.commands.reflect.ParameterDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -15,7 +15,7 @@
/**
* A {@link Filter} implementation that will check the parameter constraints a
- * {@link CommandDefinition} might have.
+ * {@link SlashCommandDefinition} might have.
*
* @author Kaktushose
* @version 2.0.0
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/CooldownFilter.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/CooldownFilter.java
index 247158fe..9604ad92 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/CooldownFilter.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/CooldownFilter.java
@@ -5,7 +5,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
import com.github.kaktushose.jda.commands.reflect.CooldownDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -15,7 +15,7 @@
/**
* A {@link Filter} implementation that contains the business logic behind command cooldowns.
* If the command isn't annotated with {@link Cooldown Cooldown} or more
- * formally if the {@link CommandDefinition} doesn't hold a {@link CooldownDefinition} or the delay of the
+ * formally if the {@link SlashCommandDefinition} doesn't hold a {@link CooldownDefinition} or the delay of the
* {@link CooldownDefinition} amounts to {@code 0} this filter has no effect.
*
* @author Kaktushose
@@ -33,14 +33,14 @@ public CooldownFilter() {
}
/**
- * Checks if an active cooldown for the given {@link CommandDefinition} exists and will eventually cancel the
+ * Checks if an active cooldown for the given {@link SlashCommandDefinition} exists and will eventually cancel the
* context.
*
* @param context the {@link GenericContext} to filter
*/
@Override
public void apply(@NotNull GenericContext context) {
- CommandDefinition command = ((CommandContext) context).getCommand();
+ SlashCommandDefinition command = ((CommandContext) context).getCommand();
if (!command.hasCooldown()) {
return;
@@ -73,17 +73,17 @@ public void apply(@NotNull GenericContext context) {
}
private static class CooldownEntry {
- private final CommandDefinition command;
+ private final SlashCommandDefinition command;
private final long startTime;
private final long duration;
- public CooldownEntry(CommandDefinition command, long startTime, long duration) {
+ public CooldownEntry(SlashCommandDefinition command, long startTime, long duration) {
this.command = command;
this.startTime = startTime;
this.duration = duration;
}
- public CommandDefinition getCommand() {
+ public SlashCommandDefinition getCommand() {
return command;
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/DirectMessageFilter.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/DirectMessageFilter.java
index d520be07..7bc5c9c9 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/DirectMessageFilter.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/DirectMessageFilter.java
@@ -4,7 +4,7 @@
import com.github.kaktushose.jda.commands.dispatching.filter.Filter;
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import org.jetbrains.annotations.NotNull;
@@ -13,7 +13,7 @@
/**
* A {@link Filter} implementation that checks if a
- * {@link CommandDefinition} is available for execution in direct messages.
+ * {@link SlashCommandDefinition} is available for execution in direct messages.
*
* @author Kaktushose
* @version 2.0.0
@@ -25,7 +25,7 @@ public class DirectMessageFilter implements Filter {
private static final Logger log = LoggerFactory.getLogger(DirectMessageFilter.class);
/**
- * Checks if a {@link CommandDefinition} is available for execution in
+ * Checks if a {@link SlashCommandDefinition} is available for execution in
* direct messages and if not cancels the {@link GenericContext},
*
* @param context the {@link GenericContext} to filter
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandContext.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandContext.java
index 6f69ad16..97c4d53a 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandContext.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandContext.java
@@ -2,7 +2,7 @@
import com.github.kaktushose.jda.commands.JDACommands;
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
@@ -24,7 +24,7 @@ public class CommandContext extends GenericContext
private String[] input;
private List options;
- private CommandDefinition command;
+ private SlashCommandDefinition command;
private List arguments;
/**
@@ -99,23 +99,23 @@ public CommandContext setOptions(@NotNull List options) {
}
/**
- * Gets the {@link CommandDefinition}. This will return null until the command got routed.
+ * Gets the {@link SlashCommandDefinition}. This will return null until the command got routed.
*
- * @return the {@link CommandDefinition}
+ * @return the {@link SlashCommandDefinition}
*/
@Nullable
- public CommandDefinition getCommand() {
+ public SlashCommandDefinition getCommand() {
return command;
}
/**
- * Set the {@link CommandDefinition}.
+ * Set the {@link SlashCommandDefinition}.
*
- * @param command the {@link CommandDefinition}
+ * @param command the {@link SlashCommandDefinition}
* @return the current CommandContext instance
*/
@NotNull
- public CommandContext setCommand(@Nullable CommandDefinition command) {
+ public CommandContext setCommand(@Nullable SlashCommandDefinition command) {
this.command = command;
setInteraction(command);
return this;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java
index f7e74eea..697b074f 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java
@@ -8,7 +8,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericDispatcher;
import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
import com.github.kaktushose.jda.commands.embeds.ErrorMessageFactory;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -59,7 +59,7 @@ public void onEvent(CommandContext context) {
}
}
- Optional optional = interactionRegistry.getCommands().stream()
+ Optional optional = interactionRegistry.getCommands().stream()
.filter(it -> it.getName().equals(context.getEvent().getFullCommandName()))
.findFirst();
if (optional.isEmpty()) {
@@ -71,7 +71,7 @@ public void onEvent(CommandContext context) {
throw exception;
}
- CommandDefinition command = optional.get();
+ SlashCommandDefinition command = optional.get();
context.setCommand(command).setEphemeral(command.isEphemeral());
log.debug("Input matches command: {}", command);
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandEvent.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandEvent.java
index 7ac6a2db..b3a448ea 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandEvent.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandEvent.java
@@ -4,13 +4,13 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericEvent;
import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
import com.github.kaktushose.jda.commands.dispatching.reply.Replyable;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import org.jetbrains.annotations.NotNull;
/**
* This class is a subclass of {@link GenericEvent}.
* It provides some additional features for sending messages and also grants
- * access to the {@link CommandDefinition} object which describes the command that is executed.
+ * access to the {@link SlashCommandDefinition} object which describes the command that is executed.
*
* @author Kaktushose
* @version 4.0.0
@@ -20,17 +20,17 @@
*/
public class CommandEvent extends GenericEvent implements Replyable {
- private final CommandDefinition command;
+ private final SlashCommandDefinition command;
private final CommandContext context;
private final ReplyContext replyContext;
/**
* Constructs a CommandEvent.
*
- * @param command the underlying {@link CommandDefinition} object
+ * @param command the underlying {@link SlashCommandDefinition} object
* @param context the {@link CommandContext}
*/
- public CommandEvent(@NotNull CommandDefinition command, @NotNull CommandContext context) {
+ public CommandEvent(@NotNull SlashCommandDefinition command, @NotNull CommandContext context) {
super(GenericEvent.fromEvent(context.getEvent()));
this.command = command;
this.context = context;
@@ -38,11 +38,11 @@ public CommandEvent(@NotNull CommandDefinition command, @NotNull CommandContext
}
/**
- * Get the {@link CommandDefinition} object which describes the command that is executed.
+ * Get the {@link SlashCommandDefinition} object which describes the command that is executed.
*
- * @return the underlying {@link CommandDefinition} object
+ * @return the underlying {@link SlashCommandDefinition} object
*/
- public CommandDefinition getCommandDefinition() {
+ public SlashCommandDefinition getCommandDefinition() {
return command;
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java
index 7c2e983b..bb7b26a5 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java
@@ -6,7 +6,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandDispatcher;
import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
import com.github.kaktushose.jda.commands.embeds.ErrorMessageFactory;
-import com.github.kaktushose.jda.commands.reflect.interactions.ContextMenuDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.ContextCommandDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -33,7 +33,7 @@ public ContextMenuDispatcher(DispatcherSupervisor supervisor, RuntimeSupervisor
public void onEvent(ContextMenuContext context) {
ErrorMessageFactory messageFactory = implementationRegistry.getErrorMessageFactory();
- Optional optional = interactionRegistry.getContextMenus().stream()
+ Optional optional = interactionRegistry.getContextMenus().stream()
.filter(it -> it.getName().equals(context.getEvent().getFullCommandName()))
.findFirst();
@@ -46,7 +46,7 @@ public void onEvent(ContextMenuContext context) {
throw exception;
}
- ContextMenuDefinition command = optional.get();
+ ContextCommandDefinition command = optional.get();
context.setEphemeral(command.isEphemeral());
log.debug("Input matches command: {}", command);
log.info("Executing command {} for user {}", command.getMethod().getName(), context.getEvent().getMember());
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuEvent.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuEvent.java
index ee5b7cfd..717a1311 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuEvent.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuEvent.java
@@ -4,17 +4,17 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericEvent;
import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
import com.github.kaktushose.jda.commands.dispatching.reply.Replyable;
-import com.github.kaktushose.jda.commands.reflect.interactions.ContextMenuDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.ContextCommandDefinition;
import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
import org.jetbrains.annotations.NotNull;
public class ContextMenuEvent extends GenericEvent implements Replyable {
- private final ContextMenuDefinition command;
+ private final ContextCommandDefinition command;
private final ContextMenuContext context;
private final ReplyContext replyContext;
- protected ContextMenuEvent(@NotNull ContextMenuDefinition command, @NotNull ContextMenuContext context) {
+ protected ContextMenuEvent(@NotNull ContextCommandDefinition command, @NotNull ContextMenuContext context) {
super(GenericEvent.fromEvent(context.getEvent()));
this.command = command;
this.context = context;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/embeds/DefaultErrorMessageFactory.java b/src/main/java/com/github/kaktushose/jda/commands/embeds/DefaultErrorMessageFactory.java
index 09e9a618..3b12ef80 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/embeds/DefaultErrorMessageFactory.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/embeds/DefaultErrorMessageFactory.java
@@ -4,7 +4,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import com.github.kaktushose.jda.commands.reflect.ConstraintDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
@@ -33,7 +33,7 @@ public class DefaultErrorMessageFactory implements ErrorMessageFactory {
@Override
public MessageCreateData getTypeAdaptingFailedMessage(@NotNull CommandContext context) {
StringBuilder sbExpected = new StringBuilder();
- CommandDefinition command = context.getCommand();
+ SlashCommandDefinition command = context.getCommand();
List arguments = Arrays.asList(context.getInput());
command.getParameters().forEach(parameter -> {
@@ -66,7 +66,7 @@ public MessageCreateData getTypeAdaptingFailedMessage(@NotNull CommandContext co
@Override
public MessageCreateData getInsufficientPermissionsMessage(@NotNull CommandContext context) {
StringBuilder sbPermissions = new StringBuilder();
- CommandDefinition command = context.getCommand();
+ SlashCommandDefinition command = context.getCommand();
command.getPermissions().forEach(permission -> sbPermissions.append(permission).append(", "));
String permissions = sbPermissions.toString().isEmpty() ? "N/A" : sbPermissions.substring(0, sbPermissions.length() - 2);
MessageEmbed embed = new EmbedBuilder()
diff --git a/src/main/java/com/github/kaktushose/jda/commands/embeds/JsonErrorMessageFactory.java b/src/main/java/com/github/kaktushose/jda/commands/embeds/JsonErrorMessageFactory.java
index 07b5de8f..9c1064c8 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/embeds/JsonErrorMessageFactory.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/embeds/JsonErrorMessageFactory.java
@@ -5,7 +5,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import com.github.kaktushose.jda.commands.reflect.ConstraintDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import org.jetbrains.annotations.NotNull;
@@ -37,7 +37,7 @@ public MessageCreateData getTypeAdaptingFailedMessage(@NotNull CommandContext co
}
StringBuilder sbExpected = new StringBuilder();
- CommandDefinition command = context.getCommand();
+ SlashCommandDefinition command = context.getCommand();
List arguments = Arrays.asList(context.getInput());
command.getParameters().forEach(parameter -> {
@@ -69,7 +69,7 @@ public MessageCreateData getInsufficientPermissionsMessage(@NotNull CommandConte
return super.getInsufficientPermissionsMessage(context);
}
- CommandDefinition command = context.getCommand();
+ SlashCommandDefinition command = context.getCommand();
StringBuilder sbPermissions = new StringBuilder();
command.getPermissions().forEach(permission -> sbPermissions.append(permission).append(", "));
String permissions = sbPermissions.toString().isEmpty() ? "N/A" : sbPermissions.substring(0, sbPermissions.length() - 2);
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionDefinition.java
similarity index 74%
rename from src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java
rename to src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionDefinition.java
index 52b60850..c399a42b 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/ControllerDefinition.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionDefinition.java
@@ -21,28 +21,28 @@
import java.util.stream.Collectors;
/**
- * Representation of a interaction controller.
+ * Representation of an interaction controller.
*
* @author Kaktushose
* @version 4.0.0
* @since 2.0.0
*/
-public class ControllerDefinition {
+public class InteractionDefinition {
- private static final Logger log = LoggerFactory.getLogger(ControllerDefinition.class);
- private final List commands;
+ private static final Logger log = LoggerFactory.getLogger(InteractionDefinition.class);
+ private final List commands;
private final List buttons;
private final List> selectMenus;
private final List autoCompletes;
- private final Collection contextMenus;
+ private final Collection contextMenus;
private final List modals;
- private ControllerDefinition(List commands,
- List buttons,
- List> selectMenus,
- List autoCompletes,
- Collection contextMenus,
- List modals) {
+ private InteractionDefinition(List commands,
+ List buttons,
+ List> selectMenus,
+ List autoCompletes,
+ Collection contextMenus,
+ List modals) {
this.commands = commands;
this.buttons = buttons;
this.selectMenus = selectMenus;
@@ -54,60 +54,60 @@ private ControllerDefinition(List commands,
/**
* Builds a new ControllerDefinition.
*
- * @param controllerClass the {@link Class} of the controller
+ * @param interactionClass the {@link Class} of the controller
* @param validatorRegistry the corresponding {@link ValidatorRegistry}
* @param dependencyInjector the corresponding {@link DependencyInjector}
* @param localizationFunction the {@link LocalizationFunction} to use
* @return an {@link Optional} holding the ControllerDefinition
*/
- public static Optional build(@NotNull Class> controllerClass,
- @NotNull ValidatorRegistry validatorRegistry,
- @NotNull DependencyInjector dependencyInjector,
- @NotNull LocalizationFunction localizationFunction) {
- Interaction interaction = controllerClass.getAnnotation(Interaction.class);
+ public static Optional build(@NotNull Class> interactionClass,
+ @NotNull ValidatorRegistry validatorRegistry,
+ @NotNull DependencyInjector dependencyInjector,
+ @NotNull LocalizationFunction localizationFunction) {
+ Interaction interaction = interactionClass.getAnnotation(Interaction.class);
if (!interaction.isActive()) {
- log.warn("Interaction class {} is set inactive. Skipping the controller and its commands", controllerClass.getName());
+ log.warn("Interaction class {} is set inactive. Skipping the controller and its commands", interactionClass.getName());
return Optional.empty();
}
List fields = new ArrayList<>();
- for (Field field : controllerClass.getDeclaredFields()) {
+ for (Field field : interactionClass.getDeclaredFields()) {
if (!field.isAnnotationPresent(Inject.class)) {
continue;
}
fields.add(field);
}
- dependencyInjector.registerDependencies(controllerClass, fields);
+ dependencyInjector.registerDependencies(interactionClass, fields);
Set permissions = new HashSet<>();
// index controller level permissions
- if (controllerClass.isAnnotationPresent(Permissions.class)) {
- Permissions permission = controllerClass.getAnnotation(Permissions.class);
+ if (interactionClass.isAnnotationPresent(Permissions.class)) {
+ Permissions permission = interactionClass.getAnnotation(Permissions.class);
permissions = Arrays.stream(permission.value()).collect(Collectors.toSet());
}
// get controller level cooldown and use it if no command level cooldown is present
CooldownDefinition cooldown = null;
- if (controllerClass.isAnnotationPresent(Cooldown.class)) {
- cooldown = CooldownDefinition.build(controllerClass.getAnnotation(Cooldown.class));
+ if (interactionClass.isAnnotationPresent(Cooldown.class)) {
+ cooldown = CooldownDefinition.build(interactionClass.getAnnotation(Cooldown.class));
}
// index interactions
- List commands = new ArrayList<>();
+ List commands = new ArrayList<>();
List buttons = new ArrayList<>();
List> selectMenus = new ArrayList<>();
- List contextMenus = new ArrayList<>();
+ List contextMenus = new ArrayList<>();
List autoCompletes = new ArrayList<>();
List modals = new ArrayList<>();
- for (Method method : controllerClass.getDeclaredMethods()) {
+ for (Method method : interactionClass.getDeclaredMethods()) {
if (method.isAnnotationPresent(SlashCommand.class)) {
- Optional optional = CommandDefinition.build(method, validatorRegistry, localizationFunction);
+ Optional optional = SlashCommandDefinition.build(method, validatorRegistry, localizationFunction);
if (optional.isEmpty()) {
continue;
}
- CommandDefinition commandDefinition = optional.get();
+ SlashCommandDefinition commandDefinition = optional.get();
// add controller level permissions
commandDefinition.getPermissions().addAll(permissions);
@@ -147,7 +147,7 @@ public static Optional build(@NotNull Class> controllerC
});
}
if (method.isAnnotationPresent(ContextMenu.class)) {
- ContextMenuDefinition.build(method, localizationFunction).ifPresent(menu -> {
+ ContextCommandDefinition.build(method, localizationFunction).ifPresent(menu -> {
if (interaction.ephemeral()) {
menu.setEphemeral(true);
}
@@ -165,7 +165,7 @@ public static Optional build(@NotNull Class> controllerC
}
//loop again once all commands got indexed because we need all commands for autocomplete registration to work
- for (Method method : controllerClass.getDeclaredMethods()) {
+ for (Method method : interactionClass.getDeclaredMethods()) {
if (method.isAnnotationPresent(AutoComplete.class)) {
AutoCompleteDefinition.build(
method,
@@ -190,7 +190,7 @@ public static Optional build(@NotNull Class> controllerC
}
}
- return Optional.of(new ControllerDefinition(commands, buttons, selectMenus, autoCompletes, contextMenus, modals));
+ return Optional.of(new InteractionDefinition(commands, buttons, selectMenus, autoCompletes, contextMenus, modals));
}
/**
@@ -198,7 +198,7 @@ public static Optional build(@NotNull Class> controllerC
*
* @return a possibly-empty list of all commands
*/
- public List getCommands() {
+ public List getCommands() {
return commands;
}
@@ -224,7 +224,7 @@ public Collection getAutoCompletes() {
return autoCompletes;
}
- public Collection getContextMenus() {
+ public Collection getContextMenus() {
return contextMenus;
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionRegistry.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionRegistry.java
index 38e7b95d..1ec64297 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionRegistry.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionRegistry.java
@@ -22,7 +22,7 @@
import java.util.Set;
/**
- * Central registry for all {@link CommandDefinition CommandDefinitions}.
+ * Central registry for all {@link SlashCommandDefinition CommandDefinitions}.
*
* @author Kaktushose
* @version 2.0.0
@@ -34,12 +34,12 @@ public class InteractionRegistry {
private final ValidatorRegistry validatorRegistry;
private final DependencyInjector dependencyInjector;
private final LocalizationFunction localizationFunction;
- private final Set controllers;
- private final Set commands;
+ private final Set controllers;
+ private final Set commands;
private final Set buttons;
private final Set> selectMenus;
private final Set autoCompletes;
- private final Set contextMenus;
+ private final Set contextMenus;
private final Set modals;
/**
@@ -89,7 +89,7 @@ public void index(@NotNull Class> clazz, @NotNull String... packages) {
for (Class> aClass : controllerSet) {
log.debug("Found controller {}", aClass.getName());
- Optional optional = ControllerDefinition.build(
+ Optional optional = InteractionDefinition.build(
aClass,
validatorRegistry,
dependencyInjector,
@@ -101,7 +101,7 @@ public void index(@NotNull Class> clazz, @NotNull String... packages) {
continue;
}
- ControllerDefinition controller = optional.get();
+ InteractionDefinition controller = optional.get();
controllers.add(controller);
commands.addAll(controller.getCommands());
buttons.addAll(controller.getButtons());
@@ -118,20 +118,20 @@ public void index(@NotNull Class> clazz, @NotNull String... packages) {
}
/**
- * Gets a possibly-empty list of all {@link ControllerDefinition ControllerDefinitions}.
+ * Gets a possibly-empty list of all {@link InteractionDefinition ControllerDefinitions}.
*
- * @return a possibly-empty list of all {@link ControllerDefinition ControllerDefinitions}
+ * @return a possibly-empty list of all {@link InteractionDefinition ControllerDefinitions}
*/
- public Set getControllers() {
+ public Set getControllers() {
return Collections.unmodifiableSet(controllers);
}
/**
- * Gets a possibly-empty list of all {@link CommandDefinition CommandDefinitions}.
+ * Gets a possibly-empty list of all {@link SlashCommandDefinition CommandDefinitions}.
*
- * @return a possibly-empty list of all {@link CommandDefinition CommandDefinitions}
+ * @return a possibly-empty list of all {@link SlashCommandDefinition CommandDefinitions}
*/
- public Set getCommands() {
+ public Set getCommands() {
return Collections.unmodifiableSet(commands);
}
@@ -162,7 +162,7 @@ public Set> getSelectMenus() {
return Collections.unmodifiableSet(selectMenus);
}
- public Set getContextMenus() {
+ public Set getContextMenus() {
return Collections.unmodifiableSet(contextMenus);
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ContextMenuDefinition.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ContextCommandDefinition.java
similarity index 84%
rename from src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ContextMenuDefinition.java
rename to src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ContextCommandDefinition.java
index 5f6cacfa..8a70ba05 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ContextMenuDefinition.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/ContextCommandDefinition.java
@@ -19,7 +19,7 @@
import java.util.Set;
import java.util.stream.Collectors;
-public class ContextMenuDefinition extends EphemeralInteraction {
+public class ContextCommandDefinition extends EphemeralInteraction {
private final String name;
private final Set permissions;
@@ -30,16 +30,16 @@ public class ContextMenuDefinition extends EphemeralInteraction {
private final SlashCommand.CommandScope scope;
private final LocalizationFunction localizationFunction;
- protected ContextMenuDefinition(Method method,
- boolean ephemeral,
- String name,
- Set permissions,
- boolean isGuildOnly,
- boolean isNSFW,
- Command.Type commandType,
- Set enabledPermissions,
- SlashCommand.CommandScope scope,
- LocalizationFunction localizationFunction) {
+ protected ContextCommandDefinition(Method method,
+ boolean ephemeral,
+ String name,
+ Set permissions,
+ boolean isGuildOnly,
+ boolean isNSFW,
+ Command.Type commandType,
+ Set enabledPermissions,
+ SlashCommand.CommandScope scope,
+ LocalizationFunction localizationFunction) {
super(method, ephemeral);
this.name = name;
this.permissions = permissions;
@@ -51,7 +51,7 @@ protected ContextMenuDefinition(Method method,
this.localizationFunction = localizationFunction;
}
- public static Optional build(@NotNull Method method, @NotNull LocalizationFunction localizationFunction) {
+ public static Optional build(@NotNull Method method, @NotNull LocalizationFunction localizationFunction) {
if (!method.isAnnotationPresent(ContextMenu.class) || !method.getDeclaringClass().isAnnotationPresent(Interaction.class)) {
return Optional.empty();
}
@@ -75,7 +75,7 @@ public static Optional build(@NotNull Method method, @Not
enabledFor.clear();
}
- return Optional.of(new ContextMenuDefinition(
+ return Optional.of(new ContextCommandDefinition(
method,
command.ephemeral(),
command.value(),
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/EphemeralInteraction.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/EphemeralInteraction.java
index f558dc68..60ef7292 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/EphemeralInteraction.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/EphemeralInteraction.java
@@ -18,10 +18,20 @@ protected EphemeralInteraction(Method method, boolean ephemeral) {
this.ephemeral = ephemeral;
}
+ /**
+ * Gets whether replies should be ephemeral.
+ *
+ * @return {@code true} if replies should be ephemeral
+ */
public boolean isEphemeral() {
return ephemeral;
}
+ /**
+ * Sets whether replies should be ephemeral.
+ *
+ * @param ephemeral {@code true} if replies should be ephemeral
+ */
public void setEphemeral(boolean ephemeral) {
this.ephemeral = ephemeral;
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/CommandDefinition.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/SlashCommandDefinition.java
similarity index 90%
rename from src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/CommandDefinition.java
rename to src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/SlashCommandDefinition.java
index bf2a43a0..e1c52fcb 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/CommandDefinition.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/SlashCommandDefinition.java
@@ -28,7 +28,7 @@
* @see SlashCommand
* @since 2.0.0
*/
-public class CommandDefinition extends EphemeralInteraction implements Comparable {
+public class SlashCommandDefinition extends EphemeralInteraction implements Comparable {
private final String name;
private final String description;
@@ -42,17 +42,17 @@ public class CommandDefinition extends EphemeralInteraction implements Comparabl
private final LocalizationFunction localizationFunction;
private boolean isAutoComplete;
- protected CommandDefinition(Method method,
- boolean ephemeral,
- String name,
- String description,
- List parameters,
- Set permissions,
- Set enabledPermissions,
- CooldownDefinition cooldown,
- boolean isGuildOnly,
- boolean isNSFW,
- SlashCommand.CommandScope scope, LocalizationFunction localizationFunction) {
+ protected SlashCommandDefinition(Method method,
+ boolean ephemeral,
+ String name,
+ String description,
+ List parameters,
+ Set permissions,
+ Set enabledPermissions,
+ CooldownDefinition cooldown,
+ boolean isGuildOnly,
+ boolean isNSFW,
+ SlashCommand.CommandScope scope, LocalizationFunction localizationFunction) {
super(method, ephemeral);
this.name = name;
this.description = description;
@@ -76,9 +76,9 @@ protected CommandDefinition(Method method,
* @param localizationFunction the {@link LocalizationFunction} to use
* @return an {@link Optional} holding the CommandDefinition
*/
- public static Optional build(@NotNull Method method,
- @NotNull ValidatorRegistry validatorRegistry,
- @NotNull LocalizationFunction localizationFunction) {
+ public static Optional build(@NotNull Method method,
+ @NotNull ValidatorRegistry validatorRegistry,
+ @NotNull LocalizationFunction localizationFunction) {
if (!method.isAnnotationPresent(SlashCommand.class) || !method.getDeclaringClass().isAnnotationPresent(Interaction.class)) {
return Optional.empty();
@@ -147,7 +147,7 @@ public static Optional build(@NotNull Method method,
enabledFor.clear();
}
- return Optional.of(new CommandDefinition(
+ return Optional.of(new SlashCommandDefinition(
method,
command.ephemeral(),
label,
@@ -329,7 +329,7 @@ public boolean isAutoComplete() {
* @param autoComplete whether this can command support auto complete
* @return this instance for fluent interface
*/
- public CommandDefinition setAutoComplete(boolean autoComplete) {
+ public SlashCommandDefinition setAutoComplete(boolean autoComplete) {
this.isAutoComplete = autoComplete;
return this;
}
@@ -368,7 +368,7 @@ public String toString() {
}
@Override
- public int compareTo(@NotNull CommandDefinition command) {
+ public int compareTo(@NotNull SlashCommandDefinition command) {
return name.compareTo(command.name);
}
}
diff --git a/src/test/java/adapting/TypeAdapterRegistryTest.java b/src/test/java/adapting/TypeAdapterRegistryTest.java
index d6fdec19..d7faf012 100644
--- a/src/test/java/adapting/TypeAdapterRegistryTest.java
+++ b/src/test/java/adapting/TypeAdapterRegistryTest.java
@@ -8,7 +8,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import com.github.kaktushose.jda.commands.dispatching.validation.ValidatorRegistry;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.interactions.commands.localization.ResourceBundleLocalizationFunction;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
@@ -127,14 +127,14 @@ public void adapt_withWrongArgument_ShouldCancel() throws NoSuchMethodException
assertTrue(context.isCancelled());
}
- private CommandDefinition buildCommand(String name, Class>... parameterTypes) throws NoSuchMethodException {
+ private SlashCommandDefinition buildCommand(String name, Class>... parameterTypes) throws NoSuchMethodException {
Method method = controller.getMethod(name, parameterTypes);
- CommandDefinition command = CommandDefinition.build(method, validator, ResourceBundleLocalizationFunction.empty().build()).orElse(null);
+ SlashCommandDefinition command = SlashCommandDefinition.build(method, validator, ResourceBundleLocalizationFunction.empty().build()).orElse(null);
assertNotNull(command);
return command;
}
- private CommandContext buildContext(CommandDefinition command, String... input) {
+ private CommandContext buildContext(SlashCommandDefinition command, String... input) {
CommandContext context = new CommandContext(
new SlashCommandInteractionEventMock(),
new JDACommandsMock());
diff --git a/src/test/java/commands/CommandDefinitionTest.java b/src/test/java/commands/SlashCommandDefinitionTest.java
similarity index 78%
rename from src/test/java/commands/CommandDefinitionTest.java
rename to src/test/java/commands/SlashCommandDefinitionTest.java
index 919dfb1d..2a7bd92f 100644
--- a/src/test/java/commands/CommandDefinitionTest.java
+++ b/src/test/java/commands/SlashCommandDefinitionTest.java
@@ -3,7 +3,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import com.github.kaktushose.jda.commands.dispatching.adapter.TypeAdapterRegistry;
import com.github.kaktushose.jda.commands.dispatching.validation.ValidatorRegistry;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
import net.dv8tion.jda.api.interactions.commands.localization.LocalizationFunction;
import net.dv8tion.jda.api.interactions.commands.localization.ResourceBundleLocalizationFunction;
import org.junit.jupiter.api.BeforeAll;
@@ -16,7 +16,7 @@
import static org.junit.jupiter.api.Assertions.*;
-public class CommandDefinitionTest {
+public class SlashCommandDefinitionTest {
private static final LocalizationFunction LOCALIZATION_FUNCTION = ResourceBundleLocalizationFunction.empty().build();
@@ -38,34 +38,34 @@ public static void setup() {
public void method_withoutAnnotation_ShouldReturnEmpty() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("noAnnotation");
- assertEquals(Optional.empty(), CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
+ assertEquals(Optional.empty(), SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
}
@Test
public void method_withoutArgs_ShouldReturnEmpty() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("noArgs");
- assertEquals(Optional.empty(), CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
+ assertEquals(Optional.empty(), SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
}
@Test
public void method_withoutCommandEvent_ShouldReturnEmpty() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("noCommandEvent", int.class);
- assertEquals(Optional.empty(), CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
+ assertEquals(Optional.empty(), SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
}
@Test
public void method_withWrongCommandEvent_ShouldReturnEmpty() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("wrongCommandEvent", int.class, CommandEvent.class);
- assertEquals(Optional.empty(), CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
+ assertEquals(Optional.empty(), SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
}
@Test
public void method_withCommandEvent_ShouldWork() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Method method = controller.getDeclaredMethod("commandEvent", CommandEvent.class);
- CommandDefinition definition = CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
+ SlashCommandDefinition definition = SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
assertNotNull(definition);
@@ -79,7 +79,7 @@ public void method_withCommandEvent_ShouldWork() throws NoSuchMethodException, I
public void method_withUnsupportedType_ShouldWork() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Method method = controller.getDeclaredMethod("unsupported", CommandEvent.class, UnsupportedType.class);
- CommandDefinition definition = CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
+ SlashCommandDefinition definition = SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
assertNotNull(definition);
@@ -93,7 +93,7 @@ public void method_withUnsupportedType_ShouldWork() throws NoSuchMethodException
@Test
public void method_withStringArray_ShouldWork() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Method method = controller.getDeclaredMethod("arrayArgument", CommandEvent.class, String[].class);
- CommandDefinition definition = CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
+ SlashCommandDefinition definition = SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
assertNotNull(definition);
@@ -108,14 +108,14 @@ public void method_withStringArray_ShouldWork() throws NoSuchMethodException, In
public void method_withArgumentsAfterArray_ShouldReturnEmpty() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("argsAfterArray", CommandEvent.class, String[].class, int.class);
- assertEquals(Optional.empty(), CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
+ assertEquals(Optional.empty(), SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
}
@Test
public void method_withArgumentsAfterOptional_ShouldWork() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("argsAfterOptional", CommandEvent.class, String.class, int.class);
- CommandDefinition definition = CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
+ SlashCommandDefinition definition = SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
assertNotNull(definition);
@@ -130,7 +130,7 @@ public void method_withArgumentsAfterOptional_ShouldWork() throws NoSuchMethodEx
@Test
public void method_withOptionalAfterOptional_ShouldWork() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Method method = controller.getDeclaredMethod("optionalAfterOptional", CommandEvent.class, String.class, int.class);
- CommandDefinition definition = CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
+ SlashCommandDefinition definition = SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
assertNotNull(definition);
@@ -146,13 +146,13 @@ public void method_withOptionalAfterOptional_ShouldWork() throws NoSuchMethodExc
public void command_isInactive_ShouldReturnEmpty() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("inactive");
- assertEquals(Optional.empty(), CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
+ assertEquals(Optional.empty(), SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION));
}
@Test
public void cooldown_zeroTimeUnits_ShouldNotBeSet() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("zeroCooldown", CommandEvent.class);
- CommandDefinition definition = CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
+ SlashCommandDefinition definition = SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
assertNotNull(definition);
@@ -162,7 +162,7 @@ public void cooldown_zeroTimeUnits_ShouldNotBeSet() throws NoSuchMethodException
@Test
public void cooldown_tenMilliseconds_ShouldWork() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("cooldown", CommandEvent.class);
- CommandDefinition definition = CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
+ SlashCommandDefinition definition = SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
assertNotNull(definition);
@@ -174,7 +174,7 @@ public void cooldown_tenMilliseconds_ShouldWork() throws NoSuchMethodException {
@Test
public void permission_oneString_ShouldWork() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("permission", CommandEvent.class);
- CommandDefinition definition = CommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
+ SlashCommandDefinition definition = SlashCommandDefinition.build(method, validator, LOCALIZATION_FUNCTION).orElse(null);
assertNotNull(definition);
diff --git a/src/test/java/controller/ControllerDefinitionTest.java b/src/test/java/controller/InteractionDefinitionTest.java
similarity index 75%
rename from src/test/java/controller/ControllerDefinitionTest.java
rename to src/test/java/controller/InteractionDefinitionTest.java
index 8368f3b2..b8e3fa64 100644
--- a/src/test/java/controller/ControllerDefinitionTest.java
+++ b/src/test/java/controller/InteractionDefinitionTest.java
@@ -4,8 +4,8 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import com.github.kaktushose.jda.commands.dispatching.adapter.TypeAdapterRegistry;
import com.github.kaktushose.jda.commands.dispatching.validation.ValidatorRegistry;
-import com.github.kaktushose.jda.commands.reflect.interactions.CommandDefinition;
-import com.github.kaktushose.jda.commands.reflect.ControllerDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.InteractionDefinition;
import commands.UnsupportedType;
import net.dv8tion.jda.api.interactions.commands.localization.LocalizationFunction;
import net.dv8tion.jda.api.interactions.commands.localization.ResourceBundleLocalizationFunction;
@@ -17,7 +17,7 @@
import static org.junit.jupiter.api.Assertions.*;
-public class ControllerDefinitionTest {
+public class InteractionDefinitionTest {
private static final LocalizationFunction LOCALIZATION_FUNCTION = ResourceBundleLocalizationFunction.empty().build();
private static Class> controller;
@@ -42,9 +42,9 @@ public static void setup() {
public void command_NoValues_ShouldAdoptControllerValues() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("adopt", CommandEvent.class);
- ControllerDefinition controllerDefinition = ControllerDefinition.build(controller, validators, dependencyInjector, LOCALIZATION_FUNCTION).orElse(null);
- assertNotNull(controllerDefinition);
- CommandDefinition definition = controllerDefinition.getCommands().stream().filter(c -> c.getMethod().equals(method)).findFirst().orElse(null);
+ InteractionDefinition interactionDefinition = InteractionDefinition.build(controller, validators, dependencyInjector, LOCALIZATION_FUNCTION).orElse(null);
+ assertNotNull(interactionDefinition);
+ SlashCommandDefinition definition = interactionDefinition.getCommands().stream().filter(c -> c.getMethod().equals(method)).findFirst().orElse(null);
assertNotNull(definition);
@@ -62,9 +62,9 @@ public void command_NoValues_ShouldAdoptControllerValues() throws NoSuchMethodEx
public void command_OwnValues_ShouldCombineOrOverride() throws NoSuchMethodException {
Method method = controller.getDeclaredMethod("combine", CommandEvent.class);
- ControllerDefinition controllerDefinition = ControllerDefinition.build(controller, validators, dependencyInjector, LOCALIZATION_FUNCTION).orElse(null);
- assertNotNull(controllerDefinition);
- CommandDefinition definition = controllerDefinition.getCommands().stream().filter(c -> c.getMethod().equals(method)).findFirst().orElse(null);
+ InteractionDefinition interactionDefinition = InteractionDefinition.build(controller, validators, dependencyInjector, LOCALIZATION_FUNCTION).orElse(null);
+ assertNotNull(interactionDefinition);
+ SlashCommandDefinition definition = interactionDefinition.getCommands().stream().filter(c -> c.getMethod().equals(method)).findFirst().orElse(null);
assertNotNull(definition);
assertEquals("super sub", definition.getName());
From 0199890b7f71603050c80fdd344673d531cd700a Mon Sep 17 00:00:00 2001
From: Kaktushose <42280757+Kaktushose@users.noreply.github.com>
Date: Sun, 29 Oct 2023 14:54:12 +0100
Subject: [PATCH 5/9] generify slash commands and context commands
---
.../kaktushose/jda/commands/JDACommands.java | 2 +-
.../jda/commands/SlashCommandUpdater.java | 94 ++++++-----
.../{ContextMenu.java => ContextCommand.java} | 2 +-
.../jda/commands/data/CommandTree.java | 2 +-
.../jda/commands/data/TreeNode.java | 2 +-
.../adapter/TypeAdapterRegistry.java | 2 +-
.../filter/impl/ConstraintFilter.java | 2 +-
.../filter/impl/CooldownFilter.java | 2 +-
.../filter/impl/DirectMessageFilter.java | 2 +-
.../interactions/commands/CommandContext.java | 2 +-
.../commands/CommandDispatcher.java | 4 +-
.../interactions/commands/CommandEvent.java | 2 +-
.../contextmenu/ContextMenuDispatcher.java | 4 +-
.../contextmenu/ContextMenuEvent.java | 2 +-
.../embeds/DefaultErrorMessageFactory.java | 2 +-
.../embeds/JsonErrorMessageFactory.java | 2 +-
.../reflect/InteractionDefinition.java | 81 +++++-----
.../commands/reflect/InteractionRegistry.java | 59 ++++---
.../ContextCommandDefinition.java | 109 ++++---------
.../commands/GenericCommandDefinition.java | 146 ++++++++++++++++++
.../SlashCommandDefinition.java | 142 +++++------------
.../adapting/TypeAdapterRegistryTest.java | 2 +-
.../commands/SlashCommandDefinitionTest.java | 2 +-
.../controller/InteractionDefinitionTest.java | 15 +-
24 files changed, 386 insertions(+), 298 deletions(-)
rename src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/{ContextMenu.java => ContextCommand.java} (98%)
rename src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/{ => commands}/ContextCommandDefinition.java (52%)
create mode 100644 src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/commands/GenericCommandDefinition.java
rename src/main/java/com/github/kaktushose/jda/commands/reflect/interactions/{ => commands}/SlashCommandDefinition.java (76%)
diff --git a/src/main/java/com/github/kaktushose/jda/commands/JDACommands.java b/src/main/java/com/github/kaktushose/jda/commands/JDACommands.java
index ef4efe32..cae4dbde 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/JDACommands.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/JDACommands.java
@@ -81,7 +81,7 @@ private JDACommands(Object jda, Class> clazz, LocalizationFunction function, S
interactionRegistry.index(clazz, packages);
- updater = new SlashCommandUpdater(this, interactionRegistry.getCommands(), interactionRegistry.getContextMenus());
+ updater = new SlashCommandUpdater(this);
updater.updateAllCommands();
jdaContext.performTask(it -> it.addEventListener(parserSupervisor));
diff --git a/src/main/java/com/github/kaktushose/jda/commands/SlashCommandUpdater.java b/src/main/java/com/github/kaktushose/jda/commands/SlashCommandUpdater.java
index 2033f59b..d55e5110 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/SlashCommandUpdater.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/SlashCommandUpdater.java
@@ -2,10 +2,12 @@
import com.github.kaktushose.jda.commands.annotations.interactions.SlashCommand;
import com.github.kaktushose.jda.commands.data.CommandTree;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.ContextCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.InteractionRegistry;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.GenericCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import com.github.kaktushose.jda.commands.scope.GuildScopeProvider;
import net.dv8tion.jda.api.entities.Guild;
+import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import org.slf4j.Logger;
@@ -27,20 +29,18 @@ public class SlashCommandUpdater {
private static final Logger log = LoggerFactory.getLogger(SlashCommandUpdater.class);
private final JDAContext jdaContext;
- private final Collection commands;
private final GuildScopeProvider guildScopeProvider;
- private final Collection contextMenus;
+ private final InteractionRegistry interactionRegistry;
/**
* Constructs a new SlashCommandUpdater.
*
* @param jdaCommands the corresponding {@link JDACommands} instance
*/
- public SlashCommandUpdater(JDACommands jdaCommands, Collection commands, Collection contextMenus) {
+ public SlashCommandUpdater(JDACommands jdaCommands) {
this.jdaContext = jdaCommands.getJdaContext();
- this.commands = commands;
- this.contextMenus = contextMenus;
guildScopeProvider = jdaCommands.getImplementationRegistry().getGuildScopeProvider();
+ interactionRegistry = jdaCommands.getInteractionRegistry();
}
/**
@@ -56,33 +56,34 @@ public void updateAllCommands() {
* Sends the guild scope {@link SlashCommandData} to Discord.
*/
public void updateGuildCommands() {
- log.debug("Updating guild slash commands...");
- Set globalCommands = commands.stream()
+ log.debug("Updating guild commands...");
+
+ Set guildCommands = interactionRegistry.getCommands()
+ .stream()
.filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GUILD)
.collect(Collectors.toSet());
- CommandTree tree = new CommandTree(globalCommands);
- log.debug("Generated command tree:\n" + tree);
- Collection labels = tree.getNames();
- log.debug("Using commands: " + labels);
- Map> guildMapping = new HashMap<>();
- for (SlashCommandData command : tree.getCommands()) {
- // create a copy so that a user doesn't modify the command data used for registration
- Set guildIds = guildScopeProvider.getGuildsForCommand(SlashCommandData.fromData(command.toData()));
- if (guildIds.isEmpty()) {
- log.debug("No guilds provided for command {}", command.getName());
- } else {
- log.debug("Using guilds {} for command {}", guildIds, command.getName());
- }
- guildIds.forEach(id -> {
- guildMapping.putIfAbsent(id, new HashSet<>());
- guildMapping.get(id).add(command);
- });
- }
+ CommandTree tree = new CommandTree(
+ guildCommands.stream()
+ .filter(it -> it.getCommandType() == Command.Type.SLASH)
+ .map(it -> (SlashCommandDefinition) it)
+ .collect(Collectors.toSet())
+ );
+ log.debug("Generated slash command tree:\n" + tree);
+
+ Set result = new HashSet<>();
+ result.addAll(tree.getCommands());
+ result.addAll(guildCommands.stream().
+ filter(it -> (it.getCommandType() == Command.Type.USER || it.getCommandType() == Command.Type.MESSAGE))
+ .map(GenericCommandDefinition::toCommandData)
+ .collect(Collectors.toSet())
+ );
+ log.debug("Registering commands: " + result.stream().map(CommandData::getName).collect(Collectors.toSet()));
- for (ContextCommandDefinition command : contextMenus.stream().filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GUILD).collect(Collectors.toSet())) {
+ Map> guildMapping = new HashMap<>();
+ for (CommandData command : result) {
// create a copy so that a user doesn't modify the command data used for registration
- Set guildIds = guildScopeProvider.getGuildsForCommand(CommandData.fromData(command.toCommandData().toData()));
+ Set guildIds = guildScopeProvider.getGuildsForCommand(CommandData.fromData(command.toData()));
if (guildIds.isEmpty()) {
log.debug("No guilds provided for command {}", command.getName());
} else {
@@ -90,7 +91,7 @@ public void updateGuildCommands() {
}
guildIds.forEach(id -> {
guildMapping.putIfAbsent(id, new HashSet<>());
- guildMapping.get(id).add(command.toCommandData());
+ guildMapping.get(id).add(command);
});
}
@@ -105,18 +106,31 @@ public void updateGuildCommands() {
* Sends the global scope {@link SlashCommandData} to Discord.
*/
public void updateGlobalCommands() {
- log.debug("Updating global slash commands...");
- Set globalCommands = commands.stream()
+ log.debug("Updating global commands...");
+
+ Set globalCommands = interactionRegistry.getCommands()
+ .stream()
.filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GLOBAL)
.collect(Collectors.toSet());
- CommandTree tree = new CommandTree(globalCommands);
- log.debug("Generated command tree:\n" + tree);
- Collection labels = tree.getNames();
- log.debug("Using commands: " + labels);
- jdaContext.performTask(jda -> jda.updateCommands().addCommands(tree.getCommands()).queue());
- jdaContext.performTask(jda -> jda.updateCommands().addCommands(contextMenus.stream().filter(it -> it.getCommandScope() == SlashCommand.CommandScope.GLOBAL).map(ContextCommandDefinition::toCommandData).collect(Collectors.toSet())).queue());
- log.debug("Done!");
- }
+ CommandTree tree = new CommandTree(
+ globalCommands.stream()
+ .filter(it -> it.getCommandType() == Command.Type.SLASH)
+ .map(it -> (SlashCommandDefinition) it)
+ .collect(Collectors.toSet())
+ );
+ log.debug("Generated slash command tree:\n" + tree);
+
+ Set result = new HashSet<>();
+ result.addAll(tree.getCommands());
+ result.addAll(globalCommands.stream().
+ filter(it -> (it.getCommandType() == Command.Type.USER || it.getCommandType() == Command.Type.MESSAGE))
+ .map(GenericCommandDefinition::toCommandData)
+ .collect(Collectors.toSet())
+ );
+ log.debug("Registering commands: " + result.stream().map(CommandData::getName).collect(Collectors.toSet()));
+ jdaContext.performTask(jda -> jda.updateCommands().addCommands(result).queue());
+ log.debug("Done!");
+ }
}
diff --git a/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/ContextMenu.java b/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/ContextCommand.java
similarity index 98%
rename from src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/ContextMenu.java
rename to src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/ContextCommand.java
index 2845288f..e8e9c3c2 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/ContextMenu.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/annotations/interactions/ContextCommand.java
@@ -26,7 +26,7 @@
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
-public @interface ContextMenu {
+public @interface ContextCommand {
/**
* Returns the name of the command.
diff --git a/src/main/java/com/github/kaktushose/jda/commands/data/CommandTree.java b/src/main/java/com/github/kaktushose/jda/commands/data/CommandTree.java
index f1c99197..aaff70ab 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/data/CommandTree.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/data/CommandTree.java
@@ -1,6 +1,6 @@
package com.github.kaktushose.jda.commands.data;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import java.util.Collection;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/data/TreeNode.java b/src/main/java/com/github/kaktushose/jda/commands/data/TreeNode.java
index 2cbb4628..a0324f4d 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/data/TreeNode.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/data/TreeNode.java
@@ -1,7 +1,7 @@
package com.github.kaktushose.jda.commands.data;
import com.github.kaktushose.jda.commands.SlashCommandUpdater;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/adapter/TypeAdapterRegistry.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/adapter/TypeAdapterRegistry.java
index e8978cf0..40c799a9 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/adapter/TypeAdapterRegistry.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/adapter/TypeAdapterRegistry.java
@@ -5,7 +5,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import com.github.kaktushose.jda.commands.embeds.ErrorMessageFactory;
import com.github.kaktushose.jda.commands.reflect.ParameterDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.User;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/ConstraintFilter.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/ConstraintFilter.java
index fb4c6bbf..4b5611df 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/ConstraintFilter.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/ConstraintFilter.java
@@ -5,7 +5,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
import com.github.kaktushose.jda.commands.reflect.ConstraintDefinition;
import com.github.kaktushose.jda.commands.reflect.ParameterDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/CooldownFilter.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/CooldownFilter.java
index 9604ad92..6f188745 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/CooldownFilter.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/CooldownFilter.java
@@ -5,7 +5,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
import com.github.kaktushose.jda.commands.reflect.CooldownDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/DirectMessageFilter.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/DirectMessageFilter.java
index 7bc5c9c9..b1a89db6 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/DirectMessageFilter.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/filter/impl/DirectMessageFilter.java
@@ -4,7 +4,7 @@
import com.github.kaktushose.jda.commands.dispatching.filter.Filter;
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import org.jetbrains.annotations.NotNull;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandContext.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandContext.java
index 97c4d53a..799332c3 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandContext.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandContext.java
@@ -2,7 +2,7 @@
import com.github.kaktushose.jda.commands.JDACommands;
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericContext;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java
index 697b074f..2b84d792 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandDispatcher.java
@@ -8,7 +8,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericDispatcher;
import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
import com.github.kaktushose.jda.commands.embeds.ErrorMessageFactory;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -59,7 +59,7 @@ public void onEvent(CommandContext context) {
}
}
- Optional optional = interactionRegistry.getCommands().stream()
+ Optional optional = interactionRegistry.getSlashCommands().stream()
.filter(it -> it.getName().equals(context.getEvent().getFullCommandName()))
.findFirst();
if (optional.isEmpty()) {
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandEvent.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandEvent.java
index b3a448ea..3830e5ef 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandEvent.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/commands/CommandEvent.java
@@ -4,7 +4,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericEvent;
import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
import com.github.kaktushose.jda.commands.dispatching.reply.Replyable;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import org.jetbrains.annotations.NotNull;
/**
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java
index bb7b26a5..ce0e2df4 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuDispatcher.java
@@ -6,7 +6,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandDispatcher;
import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
import com.github.kaktushose.jda.commands.embeds.ErrorMessageFactory;
-import com.github.kaktushose.jda.commands.reflect.interactions.ContextCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.ContextCommandDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -33,7 +33,7 @@ public ContextMenuDispatcher(DispatcherSupervisor supervisor, RuntimeSupervisor
public void onEvent(ContextMenuContext context) {
ErrorMessageFactory messageFactory = implementationRegistry.getErrorMessageFactory();
- Optional optional = interactionRegistry.getContextMenus().stream()
+ Optional optional = interactionRegistry.getContextCommands().stream()
.filter(it -> it.getName().equals(context.getEvent().getFullCommandName()))
.findFirst();
diff --git a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuEvent.java b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuEvent.java
index 717a1311..34551a12 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuEvent.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/dispatching/interactions/contextmenu/ContextMenuEvent.java
@@ -4,7 +4,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.GenericEvent;
import com.github.kaktushose.jda.commands.dispatching.reply.ReplyContext;
import com.github.kaktushose.jda.commands.dispatching.reply.Replyable;
-import com.github.kaktushose.jda.commands.reflect.interactions.ContextCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.ContextCommandDefinition;
import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
import org.jetbrains.annotations.NotNull;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/embeds/DefaultErrorMessageFactory.java b/src/main/java/com/github/kaktushose/jda/commands/embeds/DefaultErrorMessageFactory.java
index 3b12ef80..f64588cd 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/embeds/DefaultErrorMessageFactory.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/embeds/DefaultErrorMessageFactory.java
@@ -4,7 +4,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import com.github.kaktushose.jda.commands.reflect.ConstraintDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/embeds/JsonErrorMessageFactory.java b/src/main/java/com/github/kaktushose/jda/commands/embeds/JsonErrorMessageFactory.java
index 9c1064c8..304bf4aa 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/embeds/JsonErrorMessageFactory.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/embeds/JsonErrorMessageFactory.java
@@ -5,7 +5,7 @@
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandContext;
import com.github.kaktushose.jda.commands.dispatching.interactions.commands.CommandEvent;
import com.github.kaktushose.jda.commands.reflect.ConstraintDefinition;
-import com.github.kaktushose.jda.commands.reflect.interactions.SlashCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import org.jetbrains.annotations.NotNull;
diff --git a/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionDefinition.java b/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionDefinition.java
index c399a42b..1f1eeedd 100644
--- a/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionDefinition.java
+++ b/src/main/java/com/github/kaktushose/jda/commands/reflect/InteractionDefinition.java
@@ -4,10 +4,16 @@
import com.github.kaktushose.jda.commands.annotations.interactions.*;
import com.github.kaktushose.jda.commands.dependency.DependencyInjector;
import com.github.kaktushose.jda.commands.dispatching.validation.ValidatorRegistry;
-import com.github.kaktushose.jda.commands.reflect.interactions.*;
+import com.github.kaktushose.jda.commands.reflect.interactions.AutoCompleteDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.ButtonDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.ModalDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.ContextCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.GenericCommandDefinition;
+import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import com.github.kaktushose.jda.commands.reflect.interactions.menus.EntitySelectMenuDefinition;
import com.github.kaktushose.jda.commands.reflect.interactions.menus.GenericSelectMenuDefinition;
import com.github.kaktushose.jda.commands.reflect.interactions.menus.StringSelectMenuDefinition;
+import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.localization.LocalizationFunction;
import net.dv8tion.jda.api.interactions.components.selections.SelectMenu;
import org.jetbrains.annotations.NotNull;
@@ -30,24 +36,21 @@
public class InteractionDefinition {
private static final Logger log = LoggerFactory.getLogger(InteractionDefinition.class);
- private final List commands;
+ private final List commands;
private final List buttons;
private final List> selectMenus;
private final List autoCompletes;
- private final Collection contextMenus;
private final List modals;
- private InteractionDefinition(List commands,
+ private InteractionDefinition(List