From 3ba31db469d4be88ed2b7e8d31dabd85f6c7d234 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Thu, 29 Sep 2022 09:24:52 +0200 Subject: [PATCH 1/6] Added ComponentIdInteractor as delegate --- .../componentids/ComponentIdInteractor.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdInteractor.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdInteractor.java b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdInteractor.java new file mode 100644 index 0000000000..d92b05224d --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdInteractor.java @@ -0,0 +1,92 @@ +package org.togetherjava.tjbot.commands.componentids; + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import org.togetherjava.tjbot.commands.SlashCommand; + +import java.util.Arrays; + +/** + * Delegate class for interacting with component IDs. Provides methods to easily generate valid + * component IDs as accepted by the system. + *

+ * 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 ComponentIdGenerator generator; + + /** + * Creates a new instance. + * + * @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(String name) { + this.name = name; + } + + /** + * Gets the name of the interactor. + * + * @return the name of the interactor + */ + public String getName() { + return name; + } + + /** + * 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 SlashCommand#onSlashCommand(SlashCommandInteractionEvent)} for details on how to use + * this. + * + * @param generator the provided component ID generator + */ + public void acceptComponentIdGenerator(ComponentIdGenerator generator) { + this.generator = 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(getName(), Arrays.asList(args)), lifespan); + } +} From 0cfe2755e0bb1fd47c34f2421d3f203ddbbe44c2 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 30 Sep 2022 15:16:46 +0200 Subject: [PATCH 2/6] use delegate in adapter --- .../tjbot/commands/BotCommand.java | 11 -- .../tjbot/commands/BotCommandAdapter.java | 39 +++--- .../tjbot/commands/MessageContextCommand.java | 3 - .../tjbot/commands/SlashCommand.java | 1 - .../tjbot/commands/UserContextCommand.java | 3 - .../tjbot/commands/UserInteractionType.java | 61 +++++++++ .../tjbot/commands/UserInteractor.java | 14 ++- .../tjbot/commands/UserInteractorPrefix.java | 118 ------------------ .../componentids/ComponentIdInteractor.java | 12 +- .../commands/moderation/scam/ScamBlocker.java | 24 ++-- .../tjbot/commands/system/BotCore.java | 89 ++++++------- .../commands/system/CommandProvider.java | 40 ------ .../commands/SlashCommandAdapterTest.java | 2 +- 13 files changed, 158 insertions(+), 259 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/UserInteractionType.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java 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..97de040ff6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java @@ -6,12 +6,10 @@ import net.dv8tion.jda.api.interactions.commands.Command; 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 +22,22 @@ * {@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 type; private final CommandVisibility visibility; private final CommandData data; - private ComponentIdGenerator componentIdGenerator; + private final ComponentIdInteractor componentIdInteractor; /** * Creates a new adapter with the given data. @@ -55,17 +49,27 @@ 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(); + type = commandTypeToInteractionType(data.getType()); + componentIdInteractor = new ComponentIdInteractor(type, data.getName()); + } + + 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 public final String getName() { - return name; + return componentIdInteractor.getName(); } @Override - public Command.Type getType() { + public final UserInteractionType getType() { return type; } @@ -82,8 +86,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 +117,7 @@ public void onSelectionMenu(SelectMenuInteractionEvent event, List args) */ @SuppressWarnings("OverloadedVarargsMethod") protected final String generateComponentId(String... args) { - return generateComponentId(Lifespan.REGULAR, args); + return componentIdInteractor.generateComponentId(args); } /** @@ -131,8 +134,6 @@ protected final String generateComponentId(String... args) { */ @SuppressWarnings({"OverloadedVarargsMethod", "WeakerAccess"}) protected final String generateComponentId(Lifespan lifespan, String... args) { - return componentIdGenerator - .generate(new ComponentId(UserInteractorPrefix.getPrefixedNameFromInstance(this), - Arrays.asList(args)), lifespan); + return componentIdInteractor.generateComponentId(lifespan, args); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java index 9825e79d5a..3983535f1d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java @@ -22,7 +22,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. @@ -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 classType; + private final String prefix; + + UserInteractionType(Class 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 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..8a6e1f07c9 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 getType(); + /** * 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 classType; - private final String prefix; - - UserInteractorPrefix(Class 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 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 the type of the instance - * @return the interactor's name, with its prefix - */ - public static String getPrefixedNameFromInstance(final T instance) { - String name = instance.getName(); - - for (UserInteractorPrefix value : values()) { - Class valueClassType = value.getClassType(); - - if (valueClassType.isInstance(instance)) { - return value.getPrefix() + name; - } - } - - return name; - } - - /** - * Checks what enum value the given instance collaborates to. - *

- * 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 type of the instance - * @return the prefixed {@link String} - */ - public static String getPrefixedNameFromClass(final Class clazz, - final String name) { - - for (UserInteractorPrefix value : values()) { - Class valueClassType = value.getClassType(); - - if (valueClassType.isAssignableFrom(clazz)) { - return value.getPrefix() + name; - } - } - - return name; - } - - @Override - @Contract(pure = true) - public String toString() { - return "UserInteractorPrefix{" + "prefix='" + prefix + '\'' + '}'; - } -} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdInteractor.java b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdInteractor.java index d92b05224d..a23ae503d9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdInteractor.java @@ -2,8 +2,10 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import org.togetherjava.tjbot.commands.SlashCommand; +import org.togetherjava.tjbot.commands.UserInteractionType; import java.util.Arrays; +import java.util.Objects; /** * Delegate class for interacting with component IDs. Provides methods to easily generate valid @@ -17,17 +19,20 @@ */ 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(String name) { - this.name = name; + public ComponentIdInteractor(UserInteractionType userInteractionType, String name) { + this.name = Objects.requireNonNull(name); + this.userInteractionType = Objects.requireNonNull(userInteractionType); } /** @@ -87,6 +92,7 @@ public String generateComponentId(String... args) { */ @SuppressWarnings("OverloadedVarargsMethod") public String generateComponentId(Lifespan lifespan, String... args) { - return generator.generate(new ComponentId(getName(), Arrays.asList(args)), lifespan); + return generator.generate(new ComponentId(userInteractionType.getPrefixedName(getName()), + 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..7e6ed49ad9 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 hasRequiredRole; - private ComponentIdGenerator componentIdGenerator; + private final ComponentIdInteractor componentIdInteractor; /** * Creates a new listener to receive all message sent in any channel. @@ -82,6 +82,8 @@ public ScamBlocker(ModerationActionsStore actionsStore, ScamHistoryStore scamHis Pattern.compile(reportChannelPattern).asMatchPredicate(); isReportChannel = channel -> isReportChannelName.test(channel.getName()); hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); + + componentIdInteractor = new ComponentIdInteractor(getType(), getName()); } @Override @@ -89,6 +91,11 @@ public String getName() { return "scam-blocker"; } + @Override + public UserInteractionType getType() { + return UserInteractionType.OTHER; + } + @Override public void onSelectionMenu(SelectMenuInteractionEvent event, List args) { throw new UnsupportedOperationException("Not used"); @@ -96,7 +103,7 @@ public void onSelectionMenu(SelectMenuInteractionEvent event, List args) @Override public void acceptComponentIdGenerator(ComponentIdGenerator generator) { - componentIdGenerator = generator; + componentIdInteractor.acceptComponentIdGenerator(generator); } @Override @@ -266,8 +273,7 @@ private List