diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java index cc923af08f..0bd53a32b7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java @@ -2,7 +2,6 @@ import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent; -import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import java.util.List; @@ -25,16 +24,6 @@ * Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}. */ public interface BotCommand extends UserInteractor { - - /** - * Gets the type of this command. - *
- * After registration of the command, the type must not change anymore. - * - * @return the type of the command - */ - Command.Type getType(); - /** * Gets the visibility of this command. *
diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java index 0f393300fd..c9c4a52dbb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java @@ -7,11 +7,10 @@ import net.dv8tion.jda.api.interactions.commands.build.CommandData; import org.jetbrains.annotations.Contract; -import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; +import org.togetherjava.tjbot.commands.componentids.ComponentIdInteractor; import org.togetherjava.tjbot.commands.componentids.Lifespan; -import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -24,26 +23,23 @@ * {@link #onSelectionMenu(SelectMenuInteractionEvent, List)} can be overridden if desired. The * default implementation is empty, the adapter will not react to such events. *
- *
* The adapter manages some getters for you, you've to create the {@link CommandData} yourself. See * {@link #BotCommandAdapter(CommandData, CommandVisibility)}} for more info on that. Minimal * modifications can be done on the {@link CommandData} returned by {@link #getData()}. *
- *
* If implementations want to add buttons or selection menus, it is highly advised to use component * IDs generated by {@link #generateComponentId(String...)}, which will automatically create IDs * that are valid per {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)}. *
- *
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
* Registration of commands can be done in {@link Features}.
*/
public abstract class BotCommandAdapter implements BotCommand {
private final String name;
- private final Command.Type type;
+ private final UserInteractionType interactionType;
private final CommandVisibility visibility;
private final CommandData data;
- private ComponentIdGenerator componentIdGenerator;
+ private final ComponentIdInteractor componentIdInteractor;
/**
* Creates a new adapter with the given data.
@@ -55,8 +51,19 @@ protected BotCommandAdapter(CommandData data, CommandVisibility visibility) {
this.data = data.setGuildOnly(visibility == CommandVisibility.GUILD);
this.visibility = Objects.requireNonNull(visibility, "The visibility shouldn't be null");
- this.name = data.getName();
- this.type = data.getType();
+ name = data.getName();
+ interactionType = commandTypeToInteractionType(data.getType());
+ componentIdInteractor = new ComponentIdInteractor(interactionType, name);
+ }
+
+ private static UserInteractionType commandTypeToInteractionType(Command.Type type) {
+ return switch (type) {
+ case SLASH -> UserInteractionType.SLASH_COMMAND;
+ case USER -> UserInteractionType.USER_CONTEXT_COMMAND;
+ case MESSAGE -> UserInteractionType.MESSAGE_CONTEXT_COMMAND;
+ case UNKNOWN -> throw new IllegalArgumentException(
+ "Can not infer the type of user interaction for this command, it is unknown.");
+ };
}
@Override
@@ -65,8 +72,8 @@ public final String getName() {
}
@Override
- public Command.Type getType() {
- return type;
+ public final UserInteractionType getInteractionType() {
+ return interactionType;
}
@Override
@@ -82,8 +89,7 @@ public CommandData getData() {
@Override
@Contract(mutates = "this")
public final void acceptComponentIdGenerator(ComponentIdGenerator generator) {
- componentIdGenerator =
- Objects.requireNonNull(generator, "The given generator cannot be null");
+ componentIdInteractor.acceptComponentIdGenerator(generator);
}
@SuppressWarnings("NoopMethodInAbstractClass")
@@ -114,7 +120,7 @@ public void onSelectionMenu(SelectMenuInteractionEvent event, List
- *
* Context commands can either be visible globally in Discord or just to specific guilds. Minor
* adjustments can be made via {@link CommandData}, which is then to be returned by
* {@link #getData()} where the system will then pick it up from.
@@ -32,11 +31,9 @@
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
*
- *
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
public interface MessageContextCommand extends BotCommand {
-
/**
* Triggered by the core system when a message context-command corresponding to this
* implementation (based on {@link #getData()}) has been triggered.
diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java
index fb64771bf7..af7c17fa28 100644
--- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java
+++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java
@@ -40,7 +40,6 @@
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
public interface SlashCommand extends BotCommand {
-
/**
* Gets the description of the command.
*
diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java
index cc2a791338..4d25446f15 100644
--- a/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java
+++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java
@@ -23,7 +23,6 @@
* {@link BotCommandAdapter} available that implemented most methods already. A new command can then
* be registered by adding it to {@link Features}.
*
- *
* Context commands can either be visible globally in Discord or just to specific guilds. Minor
* adjustments can be made via {@link CommandData}, which is then to be returned by
* {@link #getData()} where the system will then pick it up from.
@@ -33,11 +32,9 @@
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
*
- *
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
public interface UserContextCommand extends BotCommand {
-
/**
* Triggered by the core system when a user context-command corresponding to this implementation
* (based on {@link #getData()}) has been triggered.
diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractionType.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractionType.java
new file mode 100644
index 0000000000..799852d7b2
--- /dev/null
+++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractionType.java
@@ -0,0 +1,61 @@
+package org.togetherjava.tjbot.commands;
+
+/**
+ * Different types of user interactions.
+ *
+ * Also allows user interactors of different type to have the same name.
+ */
+public enum UserInteractionType {
+ /**
+ * User types a command leaded by a slash, such as {@code /ping}.
+ */
+ SLASH_COMMAND(SlashCommand.class, "s-"),
+ /**
+ * User right-clicks a message and selects a command.
+ */
+ MESSAGE_CONTEXT_COMMAND(MessageContextCommand.class, "mc-"),
+ /**
+ * User right-clicks a user and selects a command.
+ */
+ USER_CONTEXT_COMMAND(UserContextCommand.class, "uc-"),
+ /**
+ * Other types of interactions, for example a routine that offers buttons to click on.
+ */
+ OTHER(UserInteractor.class, "");
+
+ private final Class extends UserInteractor> classType;
+ private final String prefix;
+
+ UserInteractionType(Class extends UserInteractor> classType, final String prefix) {
+ this.classType = classType;
+ this.prefix = prefix;
+ }
+
+ /**
+ * The prefix for the command
+ *
+ * @return the command's prefix
+ */
+ public String getPrefix() {
+ return prefix;
+ }
+
+ /**
+ * Returns the name, attached with the prefix in front of it.
+ *
+ * @param name the name
+ * @return the name, with the prefix in front of it.
+ */
+ public String getPrefixedName(String name) {
+ return prefix + name;
+ }
+
+ /**
+ * The class type that should receive the prefix
+ *
+ * @return a {@link Class} instance of the type
+ */
+ public Class extends UserInteractor> getClassType() {
+ return classType;
+ }
+}
diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java
index 9741571796..714a944afd 100644
--- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java
+++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java
@@ -16,14 +16,14 @@
*
* An interactor can react to button clicks and selection menu actions. This is done based on the
* given {@link #getName()}, because of this names have to be unique. But, names can be complicated
- * if their type is different, all the types can be seen in {@link UserInteractorPrefix}
+ * if their type is different, all the types can be seen in {@link UserInteractionType}
*/
public interface UserInteractor extends Feature {
/**
* Gets the name of the interactor.
*
- * You cannot start the name with any of the prefixes found in {@link UserInteractorPrefix}
+ * You cannot start the name with any of the prefixes found in {@link UserInteractionType}
*
* After registration of the interactor, the name must not change anymore.
*
@@ -31,6 +31,16 @@ public interface UserInteractor extends Feature {
*/
String getName();
+
+ /**
+ * Gets the type of interactors this interactor allows.
+ *
+ * After registration of the interactor, the type must not change anymore.
+ *
+ * @return the type of the interaction allowed by this interactor
+ */
+ UserInteractionType getInteractionType();
+
/**
* Triggered by the core system when a button corresponding to this implementation (based on
* {@link #getName()}) has been clicked.
diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java
deleted file mode 100644
index 53c76a3b5c..0000000000
--- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java
+++ /dev/null
@@ -1,118 +0,0 @@
-package org.togetherjava.tjbot.commands;
-
-import org.jetbrains.annotations.Contract;
-
-/**
- * Contains the prefixes for the UserInteractor's.
- *
- * This is used for separate interactors with the same name, by command type (and possibly more in
- * the future). Our system doesn't allow multiple interactors with the same name, while having a
- * slash-command, and a message-context-command with the same name can be useful.
- */
-public enum UserInteractorPrefix {
- // Implementations that are none of the following have no dedicated prefix.
-
- /**
- * Prefix for slash commands.
- */
- SLASH_COMMAND(SlashCommand.class, "s-"),
- /**
- * Prefix for message context commands.
- */
- MESSAGE_CONTEXT_COMMAND(MessageContextCommand.class, "mc-"),
- /**
- * Prefix for user context commands.
- */
- USER_CONTEXT_COMMAND(UserContextCommand.class, "uc-");
-
- private final Class extends UserInteractor> classType;
- private final String prefix;
-
- UserInteractorPrefix(Class extends UserInteractor> classType, final String prefix) {
- this.classType = classType;
- this.prefix = prefix;
- }
-
- /**
- * The prefix for the command
- *
- * @return the command's prefix
- */
- public String getPrefix() {
- return prefix;
- }
-
- /**
- * Returns the name, attached with the prefix in front of it.
- *
- * @param name the name
- * @return the name, with the prefix in front of it.
- */
- public String getPrefixedName(String name) {
- return prefix + name;
- }
-
- /**
- * The class type that should receive the prefix
- *
- * @return a {@link Class} instance of the type
- */
- public Class extends UserInteractor> getClassType() {
- return classType;
- }
-
- /**
- * Checks what enum value the given instance collaborates to.
- *
- * This returns the name of the interactor, and adds the designated prefix to the name. As
- * example, a slash-command with the name "help" becomes "s-help".
- *
- * @param instance an instance to type check for a prefix
- * @param
- * This combines the given name, with the interactor's prefix. As example, a slash-command with
- * the name "help" becomes "s-help".
- *
- * @param clazz the class to get the prefix from
- * @param name the name of the instance
- * @param
+ * The method {@link #acceptComponentIdGenerator(ComponentIdGenerator)} must be used before
+ * attempting to generate IDs.
+ *
+ * Mostly used by classes implementing {@link org.togetherjava.tjbot.commands.UserInteractor}, to
+ * simplify handling with the IDs.
+ */
+public final class ComponentIdInteractor {
+ private final String name;
+ private final UserInteractionType userInteractionType;
+ private ComponentIdGenerator generator;
+
+ /**
+ * Creates a new instance.
+ *
+ * @param userInteractionType The type of interaction component IDs are used by
+ * @param name The unique name of the interactor. Requirements for this are documented in
+ * {@link net.dv8tion.jda.api.interactions.commands.build.Commands#slash(String, String)}.
+ * After registration of the interactor, the name must not change anymore.
+ */
+ public ComponentIdInteractor(UserInteractionType userInteractionType, String name) {
+ this.name = Objects.requireNonNull(name);
+ this.userInteractionType = Objects.requireNonNull(userInteractionType);
+ }
+
+ /**
+ * Must be used before generating component IDs with {@link #generateComponentId(String...)} and
+ * similar.
+ *
+ * It will provide the interactor a component ID generator through this method, which can be
+ * used to generate component IDs, as used for button or selection menus. See
+ * {@link org.togetherjava.tjbot.commands.UserInteractor#acceptComponentIdGenerator(ComponentIdGenerator)}
+ * for details on how to use this.
+ *
+ * @param generator the provided component ID generator
+ */
+ public void acceptComponentIdGenerator(ComponentIdGenerator generator) {
+ this.generator = Objects.requireNonNull(generator);
+ }
+
+ /**
+ * Generates component IDs that are considered valid per
+ * {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)}.
+ *
+ * They can be used to create buttons or selection menus and transport additional data
+ * throughout the event (e.g., the user ID who created the button dialog).
+ *
+ * IDs generated by this method have a regular lifespan, meaning they might get evicted and
+ * expire after not being used for a long time. Use
+ * {@link #generateComponentId(Lifespan, String...)} to set other lifespans, if desired.
+ *
+ * @param args the extra arguments that should be part of the ID
+ * @return the generated component ID
+ */
+ @SuppressWarnings("OverloadedVarargsMethod")
+ public String generateComponentId(String... args) {
+ return generateComponentId(Lifespan.REGULAR, args);
+ }
+
+ /**
+ * Generates component IDs that are considered valid per
+ * {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)}.
+ *
+ * They can be used to create buttons or selection menus and transport additional data
+ * throughout the event (e.g., the user ID who created the button dialog).
+ *
+ * @param lifespan the lifespan of the component ID, controls when an ID that was not used for a
+ * long time might be evicted and expire
+ * @param args the extra arguments that should be part of the ID
+ * @return the generated component ID
+ */
+ @SuppressWarnings("OverloadedVarargsMethod")
+ public String generateComponentId(Lifespan lifespan, String... args) {
+ return generator.generate(
+ new ComponentId(userInteractionType.getPrefixedName(name), Arrays.asList(args)),
+ lifespan);
+ }
+}
diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java
index 0fe6d42ce9..de54d7e19a 100644
--- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java
+++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java
@@ -17,10 +17,10 @@
import org.slf4j.LoggerFactory;
import org.togetherjava.tjbot.commands.MessageReceiverAdapter;
+import org.togetherjava.tjbot.commands.UserInteractionType;
import org.togetherjava.tjbot.commands.UserInteractor;
-import org.togetherjava.tjbot.commands.componentids.ComponentId;
import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;
-import org.togetherjava.tjbot.commands.componentids.Lifespan;
+import org.togetherjava.tjbot.commands.componentids.ComponentIdInteractor;
import org.togetherjava.tjbot.commands.moderation.ModerationAction;
import org.togetherjava.tjbot.commands.moderation.ModerationActionsStore;
import org.togetherjava.tjbot.commands.moderation.ModerationUtils;
@@ -58,7 +58,7 @@ public final class ScamBlocker extends MessageReceiverAdapter implements UserInt
private final ScamHistoryStore scamHistoryStore;
private final Predicate