From a0fb71ebd545f8435cd9fbb285a0ce3fe11b4ea0 Mon Sep 17 00:00:00 2001 From: Tais993 Date: Sat, 3 Sep 2022 15:25:04 +0200 Subject: [PATCH 01/32] Copy paste of old command rework from #382 --- .../tjbot/commands/BotCommand.java | 71 +++++++++ .../tjbot/commands/BotCommandAdapter.java | 140 ++++++++++++++++++ ...Visibility.java => CommandVisibility.java} | 4 +- .../togetherjava/tjbot/commands/Features.java | 6 +- .../tjbot/commands/MessageContextCommand.java | 63 ++++++++ .../tjbot/commands/SlashCommand.java | 70 +++++---- .../tjbot/commands/SlashCommandAdapter.java | 79 ++-------- .../tjbot/commands/UserContextCommand.java | 64 ++++++++ .../tjbot/commands/UserInteractor.java | 33 ++++- .../tjbot/commands/basic/PingCommand.java | 6 +- .../commands/basic/RoleSelectCommand.java | 10 +- .../commands/basic/VcActivityCommand.java | 6 +- .../tjbot/commands/help/AskCommand.java | 6 +- .../help/ChangeHelpCategoryCommand.java | 6 +- .../commands/help/ChangeHelpTitleCommand.java | 6 +- .../tjbot/commands/help/CloseCommand.java | 6 +- .../commands/mathcommands/TeXCommand.java | 6 +- .../wolframalpha/WolframAlphaCommand.java | 6 +- .../commands/moderation/AuditCommand.java | 6 +- .../tjbot/commands/moderation/BanCommand.java | 6 +- .../commands/moderation/KickCommand.java | 6 +- .../commands/moderation/MuteCommand.java | 6 +- .../commands/moderation/NoteCommand.java | 6 +- .../moderation/QuarantineCommand.java | 6 +- .../commands/moderation/UnbanCommand.java | 6 +- .../commands/moderation/UnmuteCommand.java | 6 +- .../moderation/UnquarantineCommand.java | 6 +- .../commands/moderation/WarnCommand.java | 6 +- .../commands/moderation/WhoIsCommand.java | 6 +- .../commands/reminder/RemindCommand.java | 6 +- .../commands/system/LogLevelCommand.java | 6 +- .../tjbot/commands/system/ReloadCommand.java | 10 +- .../tjbot/commands/tags/TagCommand.java | 6 +- .../tjbot/commands/tags/TagManageCommand.java | 6 +- .../tjbot/commands/tags/TagsCommand.java | 6 +- .../commands/tophelper/TopHelpersCommand.java | 6 +- .../commands/SlashCommandAdapterTest.java | 4 +- 37 files changed, 506 insertions(+), 198 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java rename application/src/main/java/org/togetherjava/tjbot/commands/{SlashCommandVisibility.java => CommandVisibility.java} (88%) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.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 new file mode 100644 index 0000000000..eec94d7fba --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java @@ -0,0 +1,71 @@ +package org.togetherjava.tjbot.commands; + +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 org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.componentids.ComponentId; +import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; +import org.togetherjava.tjbot.commands.componentids.Lifespan; + +import java.util.List; + +/** + * Represents a Discord command. + *

+ * All commands have to implement this interface. For convenience, there is a + * {@link BotCommandAdapter} available that implemented most methods already. A new command can then + * be registered by adding it to {@link Features}. + *

+ *

+ * Commands can either be visible globally in Discord or just to specific guilds. Some + * configurations can be made via {@link CommandData}, which is then to be returned by + * {@link #getData()} where the system will then pick it up from. + *

+ * After registration, the system will notify a command whenever one of its corresponding command + * method, buttons ({@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 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 + */ + @NotNull + Command.Type getType(); + + + /** + * Gets the visibility of this command. + *

+ * After registration of the command, the visibility must not change anymore. + * + * @return the visibility of the command + */ + @NotNull + CommandVisibility getVisibility(); + + /** + * Gets the command data belonging to this command. + *

+ * See {@link net.dv8tion.jda.api.interactions.commands.build.Commands Commands} for details on how to + * create and configure instances of it. + *

+ *

+ * This method may be called multiple times, implementations must not create new data each time + * but instead configure it once beforehand. The core system will automatically call this method + * to register the command to Discord. + * + * @return the command data of this command + */ + @NotNull + CommandData getData(); +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java new file mode 100644 index 0000000000..1f5fea88c1 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java @@ -0,0 +1,140 @@ +package org.togetherjava.tjbot.commands; + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +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 org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.componentids.ComponentId; +import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; +import org.togetherjava.tjbot.commands.componentids.Lifespan; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +/** + * Adapter implementation of a {@link BotCommand}. The minimal setup only requires implementation of + * their respective command method. A new command can then be registered by adding it to + * {@link Features}. + *

+ * Further, {@link #onButtonClick(ButtonInteractionEvent, List)} and + * {@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 CommandVisibility visibility; + private final CommandData data; + private ComponentIdGenerator componentIdGenerator; + + /** + * Creates a new adapter with the given data. + * + * @param data the data for this command + * @param visibility the visibility of the command + */ + protected BotCommandAdapter(@NotNull CommandData data, @NotNull CommandVisibility visibility) { + this.data = Objects.requireNonNull(data, "The data shouldn't be null"); + this.visibility = Objects.requireNonNull(visibility, "The visibility shouldn't be null"); + + this.name = data.getName(); + this.type = data.getType(); + } + + @Override + public final @NotNull String getName() { + return name; + } + + @NotNull + @Override + public Command.Type getType() { + return type; + } + + @Override + public final @NotNull CommandVisibility getVisibility() { + return visibility; + } + + @Override + public @NotNull CommandData getData() { + return data; + } + + @Override + @Contract(mutates = "this") + public final void acceptComponentIdGenerator(@NotNull ComponentIdGenerator generator) { + componentIdGenerator = + Objects.requireNonNull(generator, "The given generator cannot be null"); + } + + @SuppressWarnings("NoopMethodInAbstractClass") + @Override + public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args) { + // Adapter does not react by default, subclasses may change this behavior + } + + @SuppressWarnings("NoopMethodInAbstractClass") + @Override + public void onSelectionMenu(@NotNull SelectMenuInteractionEvent event, + @NotNull List args) { + // Adapter does not react by default, subclasses may change this behavior + } + + /** + * Helper method to generate component IDs that are considered valid per + * {@link #acceptComponentIdGenerator(ComponentIdGenerator)}. + *

+ * 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 that 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") + protected final @NotNull String generateComponentId(@NotNull String... args) { + return generateComponentId(Lifespan.REGULAR, args); + } + + /** + * Helper method to generate component IDs that are considered valid per + * {@link #acceptComponentIdGenerator(ComponentIdGenerator)}. + *

+ * 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", "WeakerAccess"}) + protected final @NotNull String generateComponentId(@NotNull Lifespan lifespan, + @NotNull String... args) { + return componentIdGenerator.generate(new ComponentId(getName(), Arrays.asList(args)), + lifespan); + } +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandVisibility.java b/application/src/main/java/org/togetherjava/tjbot/commands/CommandVisibility.java similarity index 88% rename from application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandVisibility.java rename to application/src/main/java/org/togetherjava/tjbot/commands/CommandVisibility.java index e0d184c764..86e8ca7870 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandVisibility.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/CommandVisibility.java @@ -3,7 +3,7 @@ /** * Visibility of a slash command, i.e. in which context it can be used by users. */ -public enum SlashCommandVisibility { +public enum CommandVisibility { /** * The command can be used within the context of a guild. */ @@ -12,4 +12,4 @@ public enum SlashCommandVisibility { * The command can be used globally, outside a guild context. */ GLOBAL -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index c348b607f8..e41e57c468 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -97,6 +97,10 @@ public static Collection createFeatures(JDA jda, Database database, Con features.add(new RejoinModerationRoleListener(actionsStore, config)); features.add(new OnGuildLeaveCloseThreadListener(database)); + // Message context commands + + // User context commands + // Slash commands features.add(new LogLevelCommand()); features.add(new PingCommand()); @@ -130,4 +134,4 @@ public static Collection createFeatures(JDA jda, Database database, Con return features; } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java new file mode 100644 index 0000000000..4015f3f0eb --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java @@ -0,0 +1,63 @@ +package org.togetherjava.tjbot.commands; + +import net.dv8tion.jda.api.entities.Emoji; +import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent; +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.build.CommandData; +import net.dv8tion.jda.api.interactions.components.ComponentInteraction; +import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; + +import java.util.List; + +/** + * A message context-command is a command, accessible when right-clicking on a message. + * + *

+ * Represents a Discord message context-command. Mostly decorating + * {@link net.dv8tion.jda.api.interactions.commands.Command}. + *

+ * All message context-commands have to implement this interface. For convenience, there is a + * {@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. + *

+ * After registration, the system will notify a command whenever one of its corresponding message + * context-commands ({@link #onMessageContext(MessageContextInteractionEvent)}), buttons + * ({@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. + *

+ * This method may be called multithreaded. In particular, there are no guarantees that it will + * be executed on the same thread repeatedly or on the same thread that other event methods have + * been called on. + *

+ * Details are available in the given event and the event also enables implementations to + * respond to it. + *

+ * Buttons or menus have to be created with a component ID (see + * {@link ComponentInteraction#getComponentId()}, + * {@link net.dv8tion.jda.api.interactions.components.buttons.Button#of(ButtonStyle, String, Emoji)}) + * in a very specific format, otherwise the core system will fail to identify the command that + * corresponded to the button or menu click event and is unable to route it back. + *

+ * See {@link #acceptComponentIdGenerator(ComponentIdGenerator)} for more info. + * + * @param event the event that triggered this + */ + void onMessageContext(@NotNull MessageContextInteractionEvent event); +} \ No newline at end of file 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 9618c7412f..a0793b4881 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java @@ -1,14 +1,17 @@ package org.togetherjava.tjbot.commands; import net.dv8tion.jda.api.entities.Emoji; +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; 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.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.Commands; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; +import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; import org.togetherjava.tjbot.commands.componentids.Lifespan; @@ -16,6 +19,12 @@ import java.util.List; /** + * A slash-command is a command in Discord, with slash (/) as the prefix. These commands offer + * enhanced functionality and superior UX over text-commands. An example slash-command is the + * `/thread` command, this allows you to create threads using your keyboard. Try it out yourself! + * + *

+ *

* Represents a Discord slash-command. Mostly decorating * {@link net.dv8tion.jda.api.interactions.commands.Command}. *

@@ -36,7 +45,7 @@ *

* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}. */ -public interface SlashCommand extends UserInteractor { +public interface SlashCommand extends BotCommand { /** * Gets the description of the command. @@ -50,15 +59,6 @@ public interface SlashCommand extends UserInteractor { */ String getDescription(); - /** - * Gets the visibility of this command. - *

- * After registration of the command, the visibility must not change anymore. - * - * @return the visibility of the command - */ - SlashCommandVisibility getVisibility(); - /** * Gets the command data belonging to this command. *

@@ -74,13 +74,13 @@ public interface SlashCommand extends UserInteractor { * * @return the command data of this command */ - SlashCommandData getData(); + @NotNull SlashCommandData getData(); /** * Triggered by the core system when a slash command corresponding to this implementation (based * on {@link #getData()}) has been triggered. *

- * This method may be called multi-threaded. In particular, there are no guarantees that it will + * This method may be called multithreaded. In particular, there are no guarantees that it will * be executed on the same thread repeatedly or on the same thread that other event methods have * been called on. *

@@ -93,26 +93,34 @@ public interface SlashCommand extends UserInteractor { * in a very specific format, otherwise the core system will fail to identify the command that * corresponded to the button or menu click event and is unable to route it back. *

- * The component ID has to be a UUID-string (see {@link java.util.UUID}), which is associated to - * a specific database entry, containing meta information about the command being executed. Such - * a database entry can be created and a UUID be obtained by using - * {@link ComponentIdGenerator#generate(ComponentId, Lifespan)}, as provided by the instance - * given to {@link #acceptComponentIdGenerator(ComponentIdGenerator)} during system setup. The - * required {@link ComponentId} instance accepts optional extra arguments, which, if provided, - * can be picked up during the corresponding event (see - * {@link #onButtonClick(ButtonInteractionEvent, List)}, - * {@link #onSelectionMenu(SelectMenuInteractionEvent, List)}). - *

- * Alternatively, if {@link SlashCommandAdapter} has been extended, it also offers a handy - * {@link SlashCommandAdapter#generateComponentId(String...)} method to ease the flow. - *

- * See Component-IDs on - * our Wiki for more details and examples of how to use component IDs. - *

- * This method will be called in a multi-threaded context and the event may not be hold valid - * forever. + * See {@link #acceptComponentIdGenerator(ComponentIdGenerator)} for more info on the ID's. * * @param event the event that triggered this */ void onSlashCommand(SlashCommandInteractionEvent event); -} + + /** + * Autocompletion is comparable, but not the same as slash-command choices. Choices allow you to + * set a static list of {@value OptionData#MAX_CHOICES} possible "choices" to the commmand. + * Autocomplete allows you to dynamically give the user a list of + * {@value OptionData#MAX_CHOICES} possible choices. These choices can be generated based on the + * input of the user, the functionality is comparable to Google's autocompletion when searching + * for something. The given choices are **not** enforced by Discord, the user can **ignore** + * auto-completion and send whatever they want. + *

+ * Triggered by the core system when a slash command's autocomplete corresponding to this + * implementation (based on {@link #getData()}) has been triggered. Don't forget to enable + * autocomplete using {@link OptionData#setAutoComplete(boolean)}! + *

+ * This method may be called multithreaded. In particular, there are no guarantees that it will + * be executed on the same thread repeatedly or on the same thread that other event methods have + * been called on. + *

+ * Details are available in the given event and the event also enables implementations to + * respond to it.
+ * See {@link #acceptComponentIdGenerator(ComponentIdGenerator)} for more info on the ID's. + * + * @param event the event that triggered this + */ + void onAutoComplete(@NotNull CommandAutoCompleteInteractionEvent event); +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java index cc071fd767..83bde647ea 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java @@ -1,5 +1,6 @@ package org.togetherjava.tjbot.commands; +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent; @@ -9,6 +10,7 @@ import net.dv8tion.jda.api.interactions.commands.build.Commands; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Unmodifiable; import org.togetherjava.tjbot.commands.componentids.ComponentId; @@ -32,7 +34,7 @@ *

*

* The adapter manages all command related data itself, which can be provided during construction - * (see {@link #SlashCommandAdapter(String, String, SlashCommandVisibility)}). In order to add + * (see {@link #SlashCommandAdapter(String, String, CommandVisibility)}). In order to add * options, subcommands or similar command configurations, use {@link #getData()} and mutate the * returned data object (see {@link CommandData} for details on how to work with this class). *

@@ -63,10 +65,8 @@ *

* and registration of an instance of that class in {@link Features}. */ -public abstract class SlashCommandAdapter implements SlashCommand { - private final String name; +public abstract class SlashCommandAdapter extends BotCommandAdapter implements SlashCommand { private final String description; - private final SlashCommandVisibility visibility; private final SlashCommandData data; private ComponentIdGenerator componentIdGenerator; @@ -80,17 +80,11 @@ public abstract class SlashCommandAdapter implements SlashCommand { * @param visibility the visibility of the command */ protected SlashCommandAdapter(String name, String description, - SlashCommandVisibility visibility) { - this.name = name; + CommandVisibility visibility) { + super(Commands.slash(name, description), visibility); this.description = description; - this.visibility = visibility; - data = Commands.slash(name, description); - } - - @Override - public final String getName() { - return name; + this.data = getData(); } @Override @@ -99,69 +93,16 @@ public final String getDescription() { } @Override - public final SlashCommandVisibility getVisibility() { - return visibility; - } - - @Override - public final SlashCommandData getData() { + public final @NotNull SlashCommandData getData() { return data; } - @Override - public final void acceptComponentIdGenerator(ComponentIdGenerator generator) { - componentIdGenerator = generator; - } - - @SuppressWarnings("NoopMethodInAbstractClass") - @Override - public void onButtonClick(ButtonInteractionEvent event, List args) { - // Adapter does not react by default, subclasses may change this behavior - } - @SuppressWarnings("NoopMethodInAbstractClass") @Override - public void onSelectionMenu(SelectMenuInteractionEvent event, List args) { + public void onAutoComplete(@NotNull CommandAutoCompleteInteractionEvent event) { // Adapter does not react by default, subclasses may change this behavior } - /** - * Helper method to generate 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 that 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") - protected final String generateComponentId(String... args) { - return generateComponentId(Lifespan.REGULAR, args); - } - - /** - * Helper method to generate 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", "WeakerAccess"}) - protected final String generateComponentId(Lifespan lifespan, String... args) { - return Objects.requireNonNull(componentIdGenerator) - .generate(new ComponentId(getName(), Arrays.asList(args)), lifespan); - } - /** * Copies the given option multiple times. *

@@ -215,4 +156,4 @@ protected static List getMultipleOptionsByNamePrefix( .filter(option -> option.getName().startsWith(namePrefix)) .toList(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java new file mode 100644 index 0000000000..44bb8c1f81 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java @@ -0,0 +1,64 @@ +package org.togetherjava.tjbot.commands; + +import net.dv8tion.jda.api.entities.Emoji; +import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent; +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.build.CommandData; +import net.dv8tion.jda.api.interactions.components.ComponentInteraction; +import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; + +import java.util.List; + +/** + * A user context-command is a command, accessible when right-clicking on a member in a guild. These + * commands aren't accessible within DM's. + * + *

+ * Represents a Discord user context-command. Mostly decorating + * {@link net.dv8tion.jda.api.interactions.commands.Command}. + *

+ * All user context-commands have to implement this interface. For convenience, there is a + * {@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. + *

+ * After registration, the system will notify a command whenever one of its corresponding user + * context-commands ({@link #onUserContext(UserContextInteractionEvent)}), buttons + * ({@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. + *

+ * This method may be called multithreaded. In particular, there are no guarantees that it will + * be executed on the same thread repeatedly or on the same thread that other event methods have + * been called on. + *

+ * Details are available in the given event and the event also enables implementations to + * respond to it. + *

+ * Buttons or menus have to be created with a component ID (see + * {@link ComponentInteraction#getComponentId()}, + * {@link net.dv8tion.jda.api.interactions.components.buttons.Button#of(ButtonStyle, String, Emoji)}) + * in a very specific format, otherwise the core system will fail to identify the command that + * corresponded to the button or menu click event and is unable to route it back. + *

+ * See {@link #acceptComponentIdGenerator(ComponentIdGenerator)} for more info. + * + * @param event the event that triggered this + */ + void onUserContext(@NotNull UserContextInteractionEvent event); +} \ No newline at end of file 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 b46cb8069b..19d64b0955 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -3,7 +3,9 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent; +import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; +import org.togetherjava.tjbot.commands.componentids.Lifespan; import java.util.List; @@ -53,14 +55,14 @@ public interface UserInteractor extends Feature { * Triggered by the core system when a selection menu corresponding to this implementation * (based on {@link #getName()}) has been clicked. *

- * This method may be called multi-threaded. In particular, there are no guarantees that it will + * This method may be called multithreaded. In particular, there are no guarantees that it will * be executed on the same thread repeatedly or on the same thread that other event methods have * been called on. *

* Details are available in the given event and the event also enables implementations to * respond to it. *

- * This method will be called in a multi-threaded context and the event may not be hold valid + * This method will be called in a multithreaded context and the event may not be hold valid * forever. * * @param event the event that triggered this @@ -71,13 +73,28 @@ public interface UserInteractor extends Feature { void onSelectionMenu(SelectMenuInteractionEvent event, List args); /** - * Triggered by the core system during its setup phase. 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. + * Triggered by the core system during its setup phase. It will provide the command a component + * id generator through this method, which can be used to generate component ids, as used for + * button or selection menus. + * + *

+ * The component ID has to be a UUID-string (see {@link java.util.UUID}), which is associated to + * a specific database entry, containing meta information about the command being executed. Such + * a database entry can be created and a UUID be obtained by using + * {@link ComponentIdGenerator#generate(ComponentId, Lifespan)}, as provided by the instance + * given to this method during system setup. The required {@link ComponentId} instance accepts + * optional extra arguments, which, if provided, can be picked up during the corresponding event + * (see {@link #onButtonClick(ButtonInteractionEvent, List)}, + * {@link #onSelectionMenu(SelectMenuInteractionEvent, List)}). + *

+ * Alternatively, if {@link BotCommandAdapter} has been extended, it also offers a handy + * {@link BotCommandAdapter#generateComponentId(String...)} method to ease the flow. + *

+ * See Component-IDs on + * our Wiki for more details and examples of how to use component IDs. + * * * @param generator the provided component id generator */ void acceptComponentIdGenerator(ComponentIdGenerator generator); -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java index fdaba47f5b..8096c811eb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java @@ -2,7 +2,7 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; /** * Implementation of an example command to illustrate how to respond to a user. @@ -14,7 +14,7 @@ public final class PingCommand extends SlashCommandAdapter { * Creates an instance of the ping pong command. */ public PingCommand() { - super("ping", "Bot responds with 'Pong!'", SlashCommandVisibility.GUILD); + super("ping", "Bot responds with 'Pong!'", CommandVisibility.GUILD); } /** @@ -26,4 +26,4 @@ public PingCommand() { public void onSlashCommand(SlashCommandInteractionEvent event) { event.reply("Pong!").queue(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java index 6b0e1376c2..241df04bde 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java @@ -16,7 +16,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.componentids.Lifespan; import java.awt.Color; @@ -29,7 +29,7 @@ *

* The command works in two stages. First, a user sets up a role selection dialog by using the * command: - * + * *

  * {@code
  * /role-select
@@ -40,7 +40,7 @@
  *   selectable-role2: @Droid
  * }
  * 
- * + * * Afterwards, users can pick their roles in a menu, upon which the command adjusts their roles. */ public final class RoleSelectCommand extends SlashCommandAdapter { @@ -61,7 +61,7 @@ public final class RoleSelectCommand extends SlashCommandAdapter { public RoleSelectCommand() { super("role-select", "Creates a dialog that lets users pick roles, system roles are ignored when selected.", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); OptionData roleOption = new OptionData(OptionType.ROLE, ROLE_OPTION, "pick roles that users will then be able to select", true); @@ -266,4 +266,4 @@ private static MessageEmbed createEmbed(String title, CharSequence description) .setColor(AMBIENT_COLOR) .build(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java index 33f97035b4..e6a0f2332a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java @@ -16,7 +16,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import javax.annotation.Nullable; import java.util.List; @@ -121,7 +121,7 @@ public final class VcActivityCommand extends SlashCommandAdapter { public VcActivityCommand() { super("vc-activity", "Starts a VC activity (you need to be in an voice channel to run this command)", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); SubcommandData applicationSubCommand = new SubcommandData(APPLICATION_SUBCOMMAND, "Choose an application from our list") @@ -237,4 +237,4 @@ private static Integer requireIntOptionIfPresent(@Nullable OptionMapping option) return option == null ? null : Math.toIntExact(option.getAsLong()); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java index 451ba9d4bf..3efe69662f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java @@ -12,7 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.config.Config; import java.util.Optional; @@ -55,7 +55,7 @@ public final class AskCommand extends SlashCommandAdapter { */ public AskCommand(Config config, HelpSystemHelper helper) { super("ask", "Ask a question - use this in the staging channel", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); OptionData title = new OptionData(OptionType.STRING, TITLE_OPTION, "short and to the point", true); @@ -166,4 +166,4 @@ private static void handleFailure(Throwable exception, InteractionHook eventHook logger.error("Attempted to create a help thread, but failed", exception); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java index fd884591f8..ddabcd1005 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java @@ -12,7 +12,7 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.requests.RestAction; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.config.Config; import java.time.Instant; @@ -48,7 +48,7 @@ public final class ChangeHelpCategoryCommand extends SlashCommandAdapter { */ public ChangeHelpCategoryCommand(Config config, HelpSystemHelper helper) { super("change-help-category", "changes the category of a help thread", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); OptionData category = new OptionData(OptionType.STRING, CATEGORY_OPTION, "select what describes the question the best", true); @@ -125,4 +125,4 @@ private boolean isHelpThreadOnCooldown(ThreadChannel helpThread) { .filter(Instant.now()::isBefore) .isPresent(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java index 8ea5288f25..a53e7fddce 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java @@ -7,7 +7,7 @@ import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.interactions.commands.OptionType; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -41,7 +41,7 @@ public final class ChangeHelpTitleCommand extends SlashCommandAdapter { */ public ChangeHelpTitleCommand(HelpSystemHelper helper) { super("change-help-title", "changes the title of a help thread", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); getData().addOption(OptionType.STRING, TITLE_OPTION, "short and to the point", true); @@ -105,4 +105,4 @@ private boolean handleIsValidTitle(CharSequence title, IReplyCallback event) { return false; } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java index 3b0e348e7e..9a2a1621f9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java @@ -7,7 +7,7 @@ import net.dv8tion.jda.api.entities.ThreadChannel; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -31,7 +31,7 @@ public final class CloseCommand extends SlashCommandAdapter { * Creates a new instance. */ public CloseCommand() { - super("close", "Close this question thread", SlashCommandVisibility.GUILD); + super("close", "Close this question thread", CommandVisibility.GUILD); helpThreadIdToLastClose = Caffeine.newBuilder() .maximumSize(1_000) @@ -72,4 +72,4 @@ private boolean isHelpThreadOnCooldown(ThreadChannel helpThread) { .filter(Instant.now()::isBefore) .isPresent(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java index c81e1f7be6..7ca74ca1c7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java @@ -12,7 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import javax.imageio.ImageIO; import java.awt.Color; @@ -55,7 +55,7 @@ public final class TeXCommand extends SlashCommandAdapter { */ public TeXCommand() { super("tex", "Renders LaTeX, also supports inline $-regions like 'see this $\frac{x}{2}$'.", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); getData().addOption(OptionType.STRING, LATEX_OPTION, "The latex which is rendered as an image", true); } @@ -168,4 +168,4 @@ public void onButtonClick(final ButtonInteractionEvent event, final List } event.getMessage().delete().queue(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java index 8c650b60dc..04e4aa94fc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java @@ -7,7 +7,7 @@ import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.requests.restaction.WebhookMessageUpdateAction; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.config.Config; import java.net.http.HttpClient; @@ -40,7 +40,7 @@ public final class WolframAlphaCommand extends SlashCommandAdapter { */ public WolframAlphaCommand(Config config) { super("wolfram-alpha", "Renders mathematical queries using WolframAlpha", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); getData().addOption(OptionType.STRING, QUERY_OPTION, "the query to send to WolframAlpha", true); appId = config.getWolframAlphaAppId(); @@ -83,4 +83,4 @@ private static void sendResponse(WolframAlphaHandler.HandlerResponse response, action.queue(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 983ff7aa24..fd8fb90c81 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -15,7 +15,7 @@ import net.dv8tion.jda.api.utils.TimeUtil; import net.dv8tion.jda.internal.requests.CompletedRestAction; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import javax.annotation.Nullable; import java.time.Instant; @@ -46,7 +46,7 @@ public final class AuditCommand extends SlashCommandAdapter { */ public AuditCommand(ModerationActionsStore actionsStore) { super(COMMAND_NAME, "Lists all moderation actions that have been taken against a user", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); getData().addOption(OptionType.USER, TARGET_OPTION, "The user who to retrieve actions for", true); @@ -260,4 +260,4 @@ public void onButtonClick(ButtonInteractionEvent event, List args) { .flatMap(event::editMessage) .queue(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 52ee553f9b..8dc5001851 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -17,7 +17,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import javax.annotation.Nullable; import java.time.Instant; @@ -52,7 +52,7 @@ public final class BanCommand extends SlashCommandAdapter { * @param actionsStore used to store actions issued by this command */ public BanCommand(ModerationActionsStore actionsStore) { - super(COMMAND_NAME, "Bans the given user from the server", SlashCommandVisibility.GUILD); + super(COMMAND_NAME, "Bans the given user from the server", CommandVisibility.GUILD); OptionData durationData = new OptionData(OptionType.STRING, DURATION_OPTION, "the duration of the ban, permanent or temporary", true); @@ -215,4 +215,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { reason, deleteHistoryDays, guild, event)); }).queue(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index cd83762c6d..4dbb69df4f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -15,7 +15,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import javax.annotation.Nullable; import java.util.Objects; @@ -41,7 +41,7 @@ public final class KickCommand extends SlashCommandAdapter { * @param actionsStore used to store actions issued by this command */ public KickCommand(ModerationActionsStore actionsStore) { - super(COMMAND_NAME, "Kicks the given user from the server", SlashCommandVisibility.GUILD); + super(COMMAND_NAME, "Kicks the given user from the server", CommandVisibility.GUILD); getData().addOption(OptionType.USER, TARGET_OPTION, "The user who you want to kick", true) .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be kicked", true); @@ -139,4 +139,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } kickUserFlow(Objects.requireNonNull(target), author, reason, guild, event); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java index 0b4e4628b8..e7478a6497 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java @@ -12,7 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.config.Config; import javax.annotation.Nullable; @@ -49,7 +49,7 @@ public final class MuteCommand extends SlashCommandAdapter { */ public MuteCommand(ModerationActionsStore actionsStore, Config config) { super(COMMAND_NAME, "Mutes the given user so that they can not send messages anymore", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); OptionData durationData = new OptionData(OptionType.STRING, DURATION_OPTION, "the duration of the mute, permanent or temporary", true); @@ -168,4 +168,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { muteUserFlow(Objects.requireNonNull(target), author, temporaryData.orElse(null), reason, guild, event); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java index 3d0cc9cf2e..20be0f49c9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java @@ -8,7 +8,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import javax.annotation.Nullable; import java.util.Objects; @@ -34,7 +34,7 @@ public final class NoteCommand extends SlashCommandAdapter { * @param actionsStore used to store actions issued by this command */ public NoteCommand(ModerationActionsStore actionsStore) { - super("note", "Writes a note about the given user", SlashCommandVisibility.GUILD); + super("note", "Writes a note about the given user", CommandVisibility.GUILD); getData() .addOption(OptionType.USER, USER_OPTION, "The user who you want to write a note about", @@ -92,4 +92,4 @@ private static void sendFeedback(User target, Member author, String noteContent, event.replyEmbeds(feedback).queue(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java index c54fab71c7..a1554989e3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java @@ -11,7 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.config.Config; import javax.annotation.Nullable; @@ -43,7 +43,7 @@ public final class QuarantineCommand extends SlashCommandAdapter { public QuarantineCommand(ModerationActionsStore actionsStore, Config config) { super(COMMAND_NAME, "Puts the given user under quarantine. They can not interact with anyone anymore then.", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); getData() .addOption(OptionType.USER, TARGET_OPTION, "The user who you want to quarantine", true) @@ -146,4 +146,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { quarantineUserFlow(Objects.requireNonNull(target), author, reason, guild, event); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index e9a6ad2bd6..dc0078b3c1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -10,7 +10,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import java.util.Objects; @@ -32,7 +32,7 @@ public final class UnbanCommand extends SlashCommandAdapter { * @param actionsStore used to store actions issued by this command */ public UnbanCommand(ModerationActionsStore actionsStore) { - super(COMMAND_NAME, "Unbans the given user from the server", SlashCommandVisibility.GUILD); + super(COMMAND_NAME, "Unbans the given user from the server", CommandVisibility.GUILD); getData() .addOption(OptionType.USER, TARGET_OPTION, "The banned user who you want to unban", @@ -110,4 +110,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } unban(target, author, reason, guild, event); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java index 72b691c8a2..a68b39ec42 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java @@ -11,7 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.config.Config; import javax.annotation.Nullable; @@ -42,7 +42,7 @@ public final class UnmuteCommand extends SlashCommandAdapter { public UnmuteCommand(ModerationActionsStore actionsStore, Config config) { super(COMMAND_NAME, "Unmutes the given already muted user so that they can send messages again", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); getData().addOption(OptionType.USER, TARGET_OPTION, "The user who you want to unmute", true) .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be unmuted", true); @@ -137,4 +137,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } unmuteUserFlow(Objects.requireNonNull(target), author, reason, guild, event); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java index 87bad18f96..82aec3e2c5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java @@ -11,7 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.config.Config; import javax.annotation.Nullable; @@ -43,7 +43,7 @@ public final class UnquarantineCommand extends SlashCommandAdapter { public UnquarantineCommand(ModerationActionsStore actionsStore, Config config) { super(COMMAND_NAME, "Unquarantines the given already quarantined user so that they can interact again", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); getData() .addOption(OptionType.USER, TARGET_OPTION, "The user who you want to unquarantine", @@ -143,4 +143,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } unquarantineUserFlow(Objects.requireNonNull(target), author, reason, guild, event); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java index 05a6ae4fa1..39945da914 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java @@ -10,7 +10,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import javax.annotation.Nullable; import java.util.Objects; @@ -35,7 +35,7 @@ public final class WarnCommand extends SlashCommandAdapter { * @param actionsStore used to store actions issued by this command */ public WarnCommand(ModerationActionsStore actionsStore) { - super("warn", "Warns the given user", SlashCommandVisibility.GUILD); + super("warn", "Warns the given user", CommandVisibility.GUILD); getData().addOption(OptionType.USER, USER_OPTION, "The user who you want to warn", true) .addOption(OptionType.STRING, REASON_OPTION, "Why you want to warn the user", true); @@ -112,4 +112,4 @@ private boolean handleChecks(Member bot, Member author, @Nullable Member target, } return ModerationUtils.handleReason(reason, event); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java index f255ab08ef..5dab37b64f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java @@ -9,7 +9,7 @@ import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.utils.DiscordClientAction; import javax.annotation.CheckReturnValue; @@ -39,7 +39,7 @@ public final class WhoIsCommand extends SlashCommandAdapter { * Creates an instance. */ public WhoIsCommand() { - super("whois", "Provides info about the given user", SlashCommandVisibility.GUILD); + super("whois", "Provides info about the given user", CommandVisibility.GUILD); getData().addOption(OptionType.USER, USER_OPTION, "the user to look up", true) .addOption(OptionType.BOOLEAN, SHOW_SERVER_INFO_OPTION, @@ -241,4 +241,4 @@ private static String formatUserFlags(final Collection flags) { .filter(name -> (name.contains("Hypesquad"))) .collect(Collectors.joining(", ")); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java index 931b26e075..d3e513b11e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java @@ -8,7 +8,7 @@ import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.db.Database; import java.time.*; @@ -53,7 +53,7 @@ public final class RemindCommand extends SlashCommandAdapter { */ public RemindCommand(Database database) { super(COMMAND_NAME, "Reminds you after a given time period has passed (e.g. in 5 weeks)", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); // TODO As soon as JDA offers date/time selector input, this should also offer // "/remind at" next to "/remind in" and use subcommands then @@ -150,4 +150,4 @@ private boolean handleIsUserBelowMaxPendingReminders(ISnowflake author, ISnowfla return false; } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java index 2780f3f8ed..8ad18a6129 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java @@ -7,7 +7,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.config.Configurator; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import java.util.stream.Stream; @@ -31,7 +31,7 @@ public final class LogLevelCommand extends SlashCommandAdapter { */ public LogLevelCommand() { super("set-log-level", "Changes the log level of the bot while it is running.", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); OptionData option = new OptionData(OptionType.STRING, LOG_LEVEL_OPTION, "the log level to set", true); @@ -57,4 +57,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { Configurator.setAllLevels(LogManager.getRootLogger().getName(), level); event.reply("Set the log level to '%s'.".formatted(levelText)).queue(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java index f061b98695..54b8a0d7d2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java @@ -14,7 +14,7 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommand; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.utils.MessageUtils; import java.util.ArrayList; @@ -48,7 +48,7 @@ public final class ReloadCommand extends SlashCommandAdapter { public ReloadCommand(SlashCommandProvider commandProvider) { super("reload", "Uploads all existing slash-commands to Discord so they are fully up-to-date.", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); this.commandProvider = commandProvider; } @@ -96,7 +96,7 @@ public void onButtonClick(ButtonInteractionEvent event, List args) { // Reload global commands actions.add(updateCommandsIf( - command -> command.getVisibility() == SlashCommandVisibility.GLOBAL, + command -> command.getVisibility() == CommandVisibility.GLOBAL, getGlobalUpdateAction(event.getJDA()))); // Reload guild commands (potentially many guilds) @@ -105,7 +105,7 @@ public void onButtonClick(ButtonInteractionEvent event, List args) { // list. However, correctly reducing RestActions in a stream is not trivial. getGuildUpdateActions(event.getJDA()) .map(updateAction -> updateCommandsIf( - command -> command.getVisibility() == SlashCommandVisibility.GUILD, + command -> command.getVisibility() == CommandVisibility.GUILD, updateAction)) .forEach(actions::add); logger.debug("Reloading commands over {} action-upstreams", actions.size()); @@ -147,4 +147,4 @@ private static CommandListUpdateAction getGlobalUpdateAction(JDA jda) { private static Stream getGuildUpdateActions(JDA jda) { return jda.getGuildCache().stream().map(Guild::updateCommands); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java index 615ee799ae..7d10e8ba09 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java @@ -6,7 +6,7 @@ import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import java.time.Instant; import java.util.Objects; @@ -30,7 +30,7 @@ public final class TagCommand extends SlashCommandAdapter { * @param tagSystem the system providing the actual tag data */ public TagCommand(TagSystem tagSystem) { - super("tag", "Display a tags content", SlashCommandVisibility.GUILD); + super("tag", "Display a tags content", CommandVisibility.GUILD); this.tagSystem = tagSystem; @@ -62,4 +62,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } message.queue(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index c7b2602da2..ca0b3a190f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -13,7 +13,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.moderation.ModAuditLogWriter; import javax.annotation.Nullable; @@ -68,7 +68,7 @@ public final class TagManageCommand extends SlashCommandAdapter { * @param modAuditLogWriter to log tag changes for audition */ public TagManageCommand(TagSystem tagSystem, ModAuditLogWriter modAuditLogWriter) { - super("tag-manage", "Provides commands to manage all tags", SlashCommandVisibility.GUILD); + super("tag-manage", "Provides commands to manage all tags", CommandVisibility.GUILD); this.tagSystem = tagSystem; @@ -419,4 +419,4 @@ String getActionVerb() { return actionVerb; } } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java index 3432720cb9..e4abab24ae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java @@ -7,7 +7,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import java.time.Instant; import java.util.Collection; @@ -42,7 +42,7 @@ public final class TagsCommand extends SlashCommandAdapter { * @param tagSystem the system providing the actual tag data */ public TagsCommand(TagSystem tagSystem) { - super("tags", "Displays all available tags", SlashCommandVisibility.GUILD); + super("tags", "Displays all available tags", CommandVisibility.GUILD); this.tagSystem = tagSystem; } @@ -85,4 +85,4 @@ public void onButtonClick(ButtonInteractionEvent event, List args) { } event.getMessage().delete().queue(); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java index dd609826ea..0bea0716dd 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java @@ -15,7 +15,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.db.Database; import javax.annotation.Nullable; @@ -51,7 +51,7 @@ public final class TopHelpersCommand extends SlashCommandAdapter { */ public TopHelpersCommand(Database database) { super(COMMAND_NAME, "Lists top helpers for the last month, or a given month", - SlashCommandVisibility.GUILD); + CommandVisibility.GUILD); OptionData monthData = new OptionData(OptionType.STRING, MONTH_OPTION, "the month to compute for, by default the last month", false); @@ -190,4 +190,4 @@ private record TopHelperResult(long authorId, BigDecimal messageLengths) { private record ColumnSetting(String headerName, HorizontalAlign alignment) { } -} +} \ No newline at end of file diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java index 4007feba5c..8878b6abcd 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java @@ -11,7 +11,7 @@ final class SlashCommandAdapterTest { private static final String NAME = "foo"; private static final String DESCRIPTION = "Foo command"; - private static final SlashCommandVisibility VISIBILITY = SlashCommandVisibility.GUILD; + private static final CommandVisibility VISIBILITY = CommandVisibility.GUILD; private static final int UNIQUE_ID_ITERATIONS = 20; static SlashCommandAdapter createAdapter() { @@ -84,4 +84,4 @@ void generateComponentId() { assertEquals(lifespan.toString(), componentIdText[2]); } } -} +} \ No newline at end of file From e3aa80b89e5a3cbcd61f8e07932e3e344d277859 Mon Sep 17 00:00:00 2001 From: Tais993 Date: Sat, 3 Sep 2022 16:07:44 +0200 Subject: [PATCH 02/32] Removed ReloadCommand.java and added automatic reloading on bot launch --- .../org/togetherjava/tjbot/Application.java | 13 +- .../togetherjava/tjbot/CommandReloadUtil.java | 73 +++++++++ .../tjbot/commands/system/BotCore.java | 143 +++++++++-------- .../commands/system/CommandProvider.java | 27 ++++ .../tjbot/commands/system/ReloadCommand.java | 150 ------------------ .../commands/system/SlashCommandProvider.java | 26 --- .../commands/system/ReloadCommandTest.java | 34 ---- 7 files changed, 192 insertions(+), 274 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/system/SlashCommandProvider.java delete mode 100644 application/src/test/java/org/togetherjava/tjbot/commands/system/ReloadCommandTest.java diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index fed19cc192..fe39e4dc0d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -2,11 +2,16 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.requests.GatewayIntent; +import net.dv8tion.jda.api.requests.RestAction; +import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.Features; +import org.togetherjava.tjbot.commands.SlashCommand; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.system.BotCore; import org.togetherjava.tjbot.config.Config; @@ -17,6 +22,11 @@ import java.nio.file.Files; import java.nio.file.Path; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Stream; /** * Main class of the application. Use {@link #main(String[])} to start an instance of it. @@ -84,6 +94,7 @@ public static void runBot(Config config) { BotCore core = new BotCore(jda, database, config); jda.addEventListener(core); + CommandReloadUtil.reloadCommands(jda, core); jda.awaitReady(); // We fire the event manually, since the core might be added too late to receive the @@ -115,4 +126,4 @@ private static void onUncaughtException(Thread failingThread, Throwable failure) logger.error("Unknown error in thread {}.", failingThread.getName(), failure); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java new file mode 100644 index 0000000000..606d996610 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java @@ -0,0 +1,73 @@ +package org.togetherjava.tjbot; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.requests.RestAction; +import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.commands.BotCommand; +import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.system.CommandProvider; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Stream; + +public class CommandReloadUtil { + private static final Logger logger = LoggerFactory.getLogger(CommandReloadUtil.class); + + public static void reloadCommands(JDA jda, CommandProvider commandProvider) { + logger.info("Reloading commands..."); + List actions = + Collections.synchronizedList(new ArrayList<>()); + + // Reload global commands + actions.add(updateCommandsIf( + command -> command.getVisibility() == CommandVisibility.GLOBAL, + getGlobalUpdateAction(jda), commandProvider)); + + // Reload guild commands (potentially many guilds) + // NOTE Storing the guild actions in a list is potentially dangerous since the + // bot might theoretically be part of so many guilds that it exceeds the max size of + // list. However, correctly reducing RestActions in a stream is not trivial. + getGuildUpdateActions(jda) + .map(updateAction -> updateCommandsIf( + command -> command.getVisibility() == CommandVisibility.GUILD, + updateAction, commandProvider)) + .forEach(actions::add); + logger.debug("Reloading commands over {} action-upstreams", actions.size()); + + // Send message when all are done + RestAction.allOf(actions) + .queue(a -> logger.debug("Commands successfully reloaded!")); + } + + /** + * Updates all commands given by the command provider which pass the given filter by pushing + * through the given action upstream. + * + * @param commandFilter filter that matches commands that should be uploaded + * @param updateAction the upstream to update commands + * @return the given upstream for chaining + */ + private static CommandListUpdateAction updateCommandsIf(Predicate commandFilter, + CommandListUpdateAction updateAction, CommandProvider commandProvider) { + return commandProvider.getBotCommands() + .stream() + .filter(commandFilter) + .map(BotCommand::getData) + .reduce(updateAction, CommandListUpdateAction::addCommands, (x, y) -> x); + } + + private static CommandListUpdateAction getGlobalUpdateAction(JDA jda) { + return jda.updateCommands(); + } + + private static Stream getGuildUpdateActions(JDA jda) { + return jda.getGuildCache().stream().map(Guild::updateCommands); + } + +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 51e9434eed..d72170ff93 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -15,6 +15,7 @@ import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.requests.ErrorResponse; +import org.checkerframework.checker.units.qual.C; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.*; @@ -47,7 +48,7 @@ * event listener, using {@link net.dv8tion.jda.api.JDA#addEventListener(Object...)}. Afterwards, * the system is ready and will correctly forward events to all commands. */ -public final class BotCore extends ListenerAdapter implements SlashCommandProvider { +public final class BotCore extends ListenerAdapter implements CommandProvider { private static final Logger logger = LoggerFactory.getLogger(BotCore.class); private static final String RELOAD_COMMAND = "reload"; private static final ExecutorService COMMAND_SERVICE = Executors.newCachedThreadPool(); @@ -66,9 +67,9 @@ public final class BotCore extends ListenerAdapter implements SlashCommandProvid *

* Commands are fetched from {@link Features}. * - * @param jda the JDA instance that this command system will be used with + * @param jda the JDA instance that this command system will be used with * @param database the database that commands may use to persist data - * @param config the configuration to use for this system + * @param config the configuration to use for this system */ public BotCore(JDA jda, Database database, Config config) { this.config = config; @@ -76,28 +77,28 @@ public BotCore(JDA jda, Database database, Config config) { // Message receivers features.stream() - .filter(MessageReceiver.class::isInstance) - .map(MessageReceiver.class::cast) - .forEach(messageReceiver -> channelNameToMessageReceiver - .put(messageReceiver.getChannelNamePattern(), messageReceiver)); + .filter(MessageReceiver.class::isInstance) + .map(MessageReceiver.class::cast) + .forEach(messageReceiver -> channelNameToMessageReceiver + .put(messageReceiver.getChannelNamePattern(), messageReceiver)); // Event receivers features.stream() - .filter(EventReceiver.class::isInstance) - .map(EventReceiver.class::cast) - .forEach(jda::addEventListener); + .filter(EventReceiver.class::isInstance) + .map(EventReceiver.class::cast) + .forEach(jda::addEventListener); // Routines (are scheduled once the core is ready) routines = features.stream() - .filter(Routine.class::isInstance) - .map(Routine.class::cast) - .toList(); + .filter(Routine.class::isInstance) + .map(Routine.class::cast) + .toList(); // User Interactors (e.g. slash commands) nameToInteractor = features.stream() - .filter(UserInteractor.class::isInstance) - .map(UserInteractor.class::cast) - .collect(Collectors.toMap(UserInteractor::getName, Function.identity())); + .filter(UserInteractor.class::isInstance) + .map(UserInteractor.class::cast) + .collect(Collectors.toMap(UserInteractor::getName, Function.identity())); // Reload Command if (nameToInteractor.containsKey(RELOAD_COMMAND)) { @@ -111,12 +112,12 @@ public BotCore(JDA jda, Database database, Config config) { componentIdStore.addComponentIdRemovedListener(BotCore::onComponentIdRemoved); componentIdParser = uuid -> componentIdStore.get(UUID.fromString(uuid)); nameToInteractor.values() - .forEach(slashCommand -> slashCommand - .acceptComponentIdGenerator(((componentId, lifespan) -> { - UUID uuid = UUID.randomUUID(); - componentIdStore.putOrThrow(uuid, componentId, lifespan); - return uuid.toString(); - }))); + .forEach(slashCommand -> slashCommand + .acceptComponentIdGenerator(((componentId, lifespan) -> { + UUID uuid = UUID.randomUUID(); + componentIdStore.putOrThrow(uuid, componentId, lifespan); + return uuid.toString(); + }))); if (logger.isInfoEnabled()) { logger.info("Available user interactors: {}", nameToInteractor.keySet()); @@ -124,24 +125,40 @@ public BotCore(JDA jda, Database database, Config config) { } @Override - public Collection getSlashCommands() { + public Collection getBotCommands() { return nameToInteractor.values() - .stream() - .filter(SlashCommand.class::isInstance) - .map(SlashCommand.class::cast) - .toList(); + .stream() + .filter(BotCommand.class::isInstance) + .map(BotCommand.class::cast) + .toList(); } @Override - public Optional getSlashCommand(String name) { + public > Optional getCommand(String name, C type) { + return Optional.ofNullable(nameToInteractor.get(name)) - .filter(SlashCommand.class::isInstance) - .map(SlashCommand.class::cast); + .filter(type::isInstance) + .map(type::cast); + } + + private static Class commandTypeToClass(Command.Type type) { + if (type == null) return BotCommand.class; + + return switch (type) { + case MESSAGE -> MessageContextCommand.class; + case USER -> UserContextCommand.class; + case SLASH -> SlashCommand.class; + case UNKNOWN -> BotCommand.class; + /* + preparing for the future be like: + case null -> BotCommand.class; + */ + }; } /** * Trigger once JDA is ready. Subsequent calls are ignored. - * + * * @param jda the JDA instance to work with */ public void onReady(JDA jda) { @@ -153,7 +170,7 @@ public void onReady(JDA jda) { // Register reload on all guilds logger.debug("JDA is ready, registering reload command"); jda.getGuildCache() - .forEach(guild -> COMMAND_SERVICE.execute(() -> registerReloadCommand(guild))); + .forEach(guild -> COMMAND_SERVICE.execute(() -> registerReloadCommand(guild))); // NOTE We do not have to wait for reload to complete for the command system to be ready // itself logger.debug("Bot core is now ready"); @@ -189,7 +206,7 @@ private void scheduleRoutines(JDA jda) { public void onMessageReceived(final MessageReceivedEvent event) { if (event.isFromGuild()) { getMessageReceiversSubscribedTo(event.getChannel()) - .forEach(messageReceiver -> messageReceiver.onMessageReceived(event)); + .forEach(messageReceiver -> messageReceiver.onMessageReceived(event)); } } @@ -197,18 +214,18 @@ public void onMessageReceived(final MessageReceivedEvent event) { public void onMessageUpdate(final MessageUpdateEvent event) { if (event.isFromGuild()) { getMessageReceiversSubscribedTo(event.getChannel()) - .forEach(messageReceiver -> messageReceiver.onMessageUpdated(event)); + .forEach(messageReceiver -> messageReceiver.onMessageUpdated(event)); } } private Stream getMessageReceiversSubscribedTo(Channel channel) { String channelName = channel.getName(); return channelNameToMessageReceiver.entrySet() - .stream() - .filter(patternAndReceiver -> patternAndReceiver.getKey() - .matcher(channelName) - .matches()) - .map(Map.Entry::getValue); + .stream() + .filter(patternAndReceiver -> patternAndReceiver.getKey() + .matcher(channelName) + .matches()) + .map(Map.Entry::getValue); } @Override @@ -223,7 +240,7 @@ public void onButtonInteraction(ButtonInteractionEvent event) { logger.debug("Received button click '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); COMMAND_SERVICE - .execute(() -> forwardComponentCommand(event, UserInteractor::onButtonClick)); + .execute(() -> forwardComponentCommand(event, UserInteractor::onButtonClick)); } @Override @@ -231,7 +248,7 @@ public void onSelectMenuInteraction(SelectMenuInteractionEvent event) { logger.debug("Received selection menu event '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); COMMAND_SERVICE - .execute(() -> forwardComponentCommand(event, UserInteractor::onSelectionMenu)); + .execute(() -> forwardComponentCommand(event, UserInteractor::onSelectionMenu)); } private void registerReloadCommand(Guild guild) { @@ -246,8 +263,8 @@ private void registerReloadCommand(Guild guild) { logger.debug("Register '{}' for guild '{}'", RELOAD_COMMAND, guild.getName()); SlashCommand reloadCommand = requireSlashCommand(RELOAD_COMMAND); guild.upsertCommand(reloadCommand.getData()) - .queue(command -> logger.debug("Registered '{}' for guild '{}'", RELOAD_COMMAND, - guild.getName())); + .queue(command -> logger.debug("Registered '{}' for guild '{}'", RELOAD_COMMAND, + guild.getName())); }, ex -> handleRegisterErrors(ex, guild)); } @@ -262,22 +279,22 @@ private void registerReloadCommand(Guild guild) { * } * * - * @param event the component event that should be forwarded + * @param event the component event that should be forwarded * @param interactorArgumentConsumer the action to trigger on the associated user interactor, - * providing the event and list of arguments for consumption - * @param the type of the component interaction that should be forwarded + * providing the event and list of arguments for consumption + * @param the type of the component interaction that should be forwarded */ private void forwardComponentCommand(T event, - TriConsumer> interactorArgumentConsumer) { + TriConsumer> interactorArgumentConsumer) { Optional componentIdOpt; try { componentIdOpt = componentIdParser.parse(event.getComponentId()); } catch (InvalidComponentIdFormatException e) { logger - .error("Unable to route event (#{}) back to its corresponding user interactor. The component ID was in an unexpected format." - + " All button and menu events have to use a component ID created in a specific format" - + " (refer to the documentation of UserInteractor). Component ID was: {}", - event.getId(), event.getComponentId(), e); + .error("Unable to route event (#{}) back to its corresponding user interactor. The component ID was in an unexpected format." + + " All button and menu events have to use a component ID created in a specific format" + + " (refer to the documentation of UserInteractor). Component ID was: {}", + event.getId(), event.getComponentId(), e); // Unable to forward, simply fade out the event return; } @@ -285,8 +302,8 @@ private void forwardComponentCommand(T event, logger.warn("The event (#{}) has an expired component ID, which was: {}.", event.getId(), event.getComponentId()); event.reply("Sorry, but this event has expired. You can not use it anymore.") - .setEphemeral(true) - .queue(); + .setEphemeral(true) + .queue(); return; } ComponentId componentId = componentIdOpt.orElseThrow(); @@ -326,17 +343,17 @@ private void handleRegisterErrors(Throwable ex, Guild guild) { // NOTE Unfortunately, there is no better accurate way to find a proper channel // where we can report the setup problems other than simply iterating all of them. Optional channelToReportTo = guild.getTextChannelCache() - .stream() - .filter(channel -> guild.getPublicRole() - .hasPermission(channel, Permission.MESSAGE_SEND)) - .findAny(); + .stream() + .filter(channel -> guild.getPublicRole() + .hasPermission(channel, Permission.MESSAGE_SEND)) + .findAny(); // Report the problem to the guild channelToReportTo.ifPresent(textChannel -> textChannel - .sendMessage("I need the commands scope, please invite me correctly." - + " You can join '%s' or visit '%s' for more info, I will leave your guild now." + .sendMessage("I need the commands scope, please invite me correctly." + + " You can join '%s' or visit '%s' for more info, I will leave your guild now." .formatted(config.getDiscordGuildInvite(), config.getProjectWebsite())) - .queue()); + .queue()); guild.leave().queue(); @@ -370,10 +387,10 @@ private interface TriConsumer { /** * Performs this operation on the given arguments. * - * @param first the first input argument + * @param first the first input argument * @param second the second input argument - * @param third the third input argument + * @param third the third input argument */ void accept(A first, B second, C third); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java new file mode 100644 index 0000000000..9bac61a8b4 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -0,0 +1,27 @@ +package org.togetherjava.tjbot.commands.system; + +import org.togetherjava.tjbot.commands.BotCommand; + +import java.util.Collection; +import java.util.Optional; + +/** + * Provides all registered commands. + */ +public interface CommandProvider { + /** + * Gets a list of all currently available and registered commands. + * + * @return all commands + */ + Collection getBotCommands(); + + /** + * Gets the slash command registered under the given name, if any. + * + * @param name the name of the command + * @param type the type of the command + * @return the command registered under this name, if any + */ + public > Optional getCommand(String name, C type); +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java deleted file mode 100644 index 54b8a0d7d2..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java +++ /dev/null @@ -1,150 +0,0 @@ -package org.togetherjava.tjbot.commands.system; - -import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; -import net.dv8tion.jda.api.interactions.components.buttons.Button; -import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; -import net.dv8tion.jda.api.requests.RestAction; -import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommand; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; -import org.togetherjava.tjbot.commands.CommandVisibility; -import org.togetherjava.tjbot.commands.utils.MessageUtils; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import java.util.function.Predicate; -import java.util.stream.Stream; - -/** - * The reload command is a meta command that updates all commands to Discord by using - * {@link CommandListUpdateAction} to push any changes through JDA. - *

- * Whenever a command has been added, removed or changed, the reload command has to be called for - * the changes to actually take effect in Discord. - *

- * This command is rate limited by Discord and may not be used too often. Be aware that it will - * reload all commands registered with this application, on all guilds. - */ -public final class ReloadCommand extends SlashCommandAdapter { - private static final Logger logger = LoggerFactory.getLogger(ReloadCommand.class); - private final SlashCommandProvider commandProvider; - - /** - * Creates the reload command, using the given provider as source of truth for the commands to - * reload - * - * @param commandProvider the provider of slash commands to reload when this command is - * triggered - */ - public ReloadCommand(SlashCommandProvider commandProvider) { - super("reload", - "Uploads all existing slash-commands to Discord so they are fully up-to-date.", - CommandVisibility.GUILD); - this.commandProvider = commandProvider; - } - - @Override - public void onSlashCommand(SlashCommandInteractionEvent event) { - Member member = Objects.requireNonNull(event.getMember()); - - if (!member.hasPermission(Permission.MANAGE_SERVER)) { - logger.debug( - "Attempted reload but is missing permissions, triggered by user '{}' in guild '{}'", - member.getId(), event.getGuild()); - event.reply("You need the 'MANAGE_SERVER' permission to use this command.") - .setEphemeral(true) - .queue(); - return; - } - - event.reply( - "Are you sure? You can only reload commands a few times each day, so do not overdo this.") - .addActionRow(Button.success(generateComponentId(member.getId()), "Yes"), - Button.danger(generateComponentId(member.getId()), "No")) - .queue(); - } - - @Override - public void onButtonClick(ButtonInteractionEvent event, List args) { - // Ignore if another user clicked the button - String userId = args.get(0); - if (!userId.equals(Objects.requireNonNull(event.getMember()).getId())) { - event.reply("Sorry, but only the user who triggered the command can use these buttons.") - .setEphemeral(true) - .queue(); - return; - } - - ButtonStyle buttonStyle = Objects.requireNonNull(event.getButton()).getStyle(); - switch (buttonStyle) { - case DANGER -> event.reply("Okay, will not reload.").queue(); - case SUCCESS -> { - logger.info("Reloading commands, triggered by user '{}' in guild '{}'", userId, - event.getGuild()); - event.deferReply().queue(); - List actions = - Collections.synchronizedList(new ArrayList<>()); - - // Reload global commands - actions.add(updateCommandsIf( - command -> command.getVisibility() == CommandVisibility.GLOBAL, - getGlobalUpdateAction(event.getJDA()))); - - // Reload guild commands (potentially many guilds) - // NOTE Storing the guild actions in a list is potentially dangerous since the - // bot might theoretically be part of so many guilds that it exceeds the max size of - // list. However, correctly reducing RestActions in a stream is not trivial. - getGuildUpdateActions(event.getJDA()) - .map(updateAction -> updateCommandsIf( - command -> command.getVisibility() == CommandVisibility.GUILD, - updateAction)) - .forEach(actions::add); - logger.debug("Reloading commands over {} action-upstreams", actions.size()); - - // Send message when all are done - RestAction.allOf(actions) - .queue(updatedCommands -> event.getHook() - .editOriginal( - "Commands successfully reloaded! (global commands can take up to one hour to load)") - .queue()); - } - default -> throw new AssertionError("Unexpected button action clicked: " + buttonStyle); - } - - MessageUtils.disableButtons(event.getMessage()); - } - - /** - * Updates all commands given by the command provider which pass the given filter by pushing - * through the given action upstream. - * - * @param commandFilter filter that matches commands that should be uploaded - * @param updateAction the upstream to update commands - * @return the given upstream for chaining - */ - private CommandListUpdateAction updateCommandsIf(Predicate commandFilter, - CommandListUpdateAction updateAction) { - return commandProvider.getSlashCommands() - .stream() - .filter(commandFilter) - .map(SlashCommand::getData) - .reduce(updateAction, CommandListUpdateAction::addCommands, (x, y) -> x); - } - - private static CommandListUpdateAction getGlobalUpdateAction(JDA jda) { - return jda.updateCommands(); - } - - private static Stream getGuildUpdateActions(JDA jda) { - return jda.getGuildCache().stream().map(Guild::updateCommands); - } -} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/SlashCommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/SlashCommandProvider.java deleted file mode 100644 index 64808d67f4..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/SlashCommandProvider.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.togetherjava.tjbot.commands.system; - -import org.togetherjava.tjbot.commands.SlashCommand; - -import java.util.Collection; -import java.util.Optional; - -/** - * Provides all registered slash commands. - */ -public interface SlashCommandProvider { - /** - * Gets a list of all currently available and registered slash commands. - * - * @return all slash commands - */ - Collection getSlashCommands(); - - /** - * Gets the slash command registered under the given name, if any. - * - * @param name the name of the command - * @return the command registered under this name, if any - */ - Optional getSlashCommand(String name); -} diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/system/ReloadCommandTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/system/ReloadCommandTest.java deleted file mode 100644 index 69ca4c9518..0000000000 --- a/application/src/test/java/org/togetherjava/tjbot/commands/system/ReloadCommandTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.togetherjava.tjbot.commands.system; - -import org.junit.jupiter.api.Test; -import org.togetherjava.tjbot.commands.SlashCommand; - -import java.util.Collection; -import java.util.List; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -final class ReloadCommandTest { - @Test - void ReloadCommand() { - @SuppressWarnings({"AnonymousInnerClassWithTooManyMethods", "AnonymousInnerClass", - "AnonymousInnerClassMayBeStatic"}) - SlashCommandProvider slashCommandProvider = new SlashCommandProvider() { - @Override - public Collection getSlashCommands() { - return List.of(); - } - - @Override - public Optional getSlashCommand(String name) { - return Optional.empty(); - } - }; - - // Adjust this test if the command is supposed to be renamed; make sure to update it in all - // places. - assertEquals("reload", new ReloadCommand(slashCommandProvider).getName(), - "expected name of reload command to be 'reload', a name change can have severe consequences if not all sites that rely on this are updated."); - } -} From 4332c74ae381f3823a3c80f87b8c36345e3900f4 Mon Sep 17 00:00:00 2001 From: Tijs Date: Wed, 7 Sep 2022 15:29:03 +0200 Subject: [PATCH 03/32] Applied Spotless, and reworked CommandProvider After these changes, there's support for multiple interactors with the same name, they just need to be of a different type (slash-command, message context-command, user context-command, or just a basic interactor) --- .../org/togetherjava/tjbot/Application.java | 10 - .../togetherjava/tjbot/CommandReloadUtil.java | 24 +- .../tjbot/commands/BotCommand.java | 7 +- .../tjbot/commands/BotCommandAdapter.java | 6 +- .../tjbot/commands/CommandVisibility.java | 2 +- .../togetherjava/tjbot/commands/Features.java | 2 +- .../tjbot/commands/MessageContextCommand.java | 2 +- .../tjbot/commands/SlashCommand.java | 13 +- .../tjbot/commands/SlashCommandAdapter.java | 13 +- .../tjbot/commands/UserContextCommand.java | 2 +- .../tjbot/commands/UserInteractor.java | 2 +- .../tjbot/commands/basic/PingCommand.java | 2 +- .../commands/basic/RoleSelectCommand.java | 5 +- .../commands/basic/VcActivityCommand.java | 2 +- .../tjbot/commands/help/AskCommand.java | 5 +- .../help/ChangeHelpCategoryCommand.java | 2 +- .../commands/help/ChangeHelpTitleCommand.java | 5 +- .../tjbot/commands/help/CloseCommand.java | 2 +- .../commands/mathcommands/TeXCommand.java | 5 +- .../wolframalpha/WolframAlphaCommand.java | 2 +- .../commands/moderation/AuditCommand.java | 2 +- .../tjbot/commands/moderation/BanCommand.java | 2 +- .../commands/moderation/KickCommand.java | 2 +- .../commands/moderation/MuteCommand.java | 2 +- .../commands/moderation/NoteCommand.java | 2 +- .../moderation/QuarantineCommand.java | 2 +- .../commands/moderation/UnbanCommand.java | 2 +- .../commands/moderation/UnmuteCommand.java | 2 +- .../moderation/UnquarantineCommand.java | 2 +- .../commands/moderation/WarnCommand.java | 2 +- .../commands/moderation/WhoIsCommand.java | 4 +- .../commands/reminder/RemindCommand.java | 2 +- .../tjbot/commands/system/BotCore.java | 222 +++++++++++------- .../commands/system/CommandProvider.java | 20 +- .../commands/system/LogLevelCommand.java | 2 +- .../tjbot/commands/tags/TagCommand.java | 2 +- .../tjbot/commands/tags/TagManageCommand.java | 2 +- .../tjbot/commands/tags/TagsCommand.java | 2 +- .../commands/tophelper/TopHelpersCommand.java | 2 +- .../commands/SlashCommandAdapterTest.java | 2 +- 40 files changed, 211 insertions(+), 182 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index fe39e4dc0d..320538f626 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -2,16 +2,11 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; -import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.requests.GatewayIntent; -import net.dv8tion.jda.api.requests.RestAction; -import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.CommandVisibility; import org.togetherjava.tjbot.commands.Features; -import org.togetherjava.tjbot.commands.SlashCommand; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.system.BotCore; import org.togetherjava.tjbot.config.Config; @@ -22,11 +17,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.function.Predicate; -import java.util.stream.Stream; /** * Main class of the application. Use {@link #main(String[])} to start an instance of it. diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java index 606d996610..4e6f1e2bf1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java @@ -21,12 +21,10 @@ public class CommandReloadUtil { public static void reloadCommands(JDA jda, CommandProvider commandProvider) { logger.info("Reloading commands..."); - List actions = - Collections.synchronizedList(new ArrayList<>()); + List actions = Collections.synchronizedList(new ArrayList<>()); // Reload global commands - actions.add(updateCommandsIf( - command -> command.getVisibility() == CommandVisibility.GLOBAL, + actions.add(updateCommandsIf(command -> command.getVisibility() == CommandVisibility.GLOBAL, getGlobalUpdateAction(jda), commandProvider)); // Reload guild commands (potentially many guilds) @@ -35,14 +33,13 @@ public static void reloadCommands(JDA jda, CommandProvider commandProvider) { // list. However, correctly reducing RestActions in a stream is not trivial. getGuildUpdateActions(jda) .map(updateAction -> updateCommandsIf( - command -> command.getVisibility() == CommandVisibility.GUILD, - updateAction, commandProvider)) + command -> command.getVisibility() == CommandVisibility.GUILD, updateAction, + commandProvider)) .forEach(actions::add); logger.debug("Reloading commands over {} action-upstreams", actions.size()); // Send message when all are done - RestAction.allOf(actions) - .queue(a -> logger.debug("Commands successfully reloaded!")); + RestAction.allOf(actions).queue(a -> logger.debug("Commands successfully reloaded!")); } /** @@ -50,13 +47,16 @@ public static void reloadCommands(JDA jda, CommandProvider commandProvider) { * through the given action upstream. * * @param commandFilter filter that matches commands that should be uploaded - * @param updateAction the upstream to update commands + * @param updateAction the upstream to update commands * @return the given upstream for chaining */ - private static CommandListUpdateAction updateCommandsIf(Predicate commandFilter, - CommandListUpdateAction updateAction, CommandProvider commandProvider) { - return commandProvider.getBotCommands() + private static CommandListUpdateAction updateCommandsIf( + Predicate commandFilter, CommandListUpdateAction updateAction, + CommandProvider commandProvider) { + return commandProvider.getInteractors() .stream() + .filter(BotCommand.class::isInstance) + .map(BotCommand.class::cast) .filter(commandFilter) .map(BotCommand::getData) .reduce(updateAction, CommandListUpdateAction::addCommands, (x, y) -> x); 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 eec94d7fba..348ad4359b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java @@ -5,9 +5,6 @@ import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import org.jetbrains.annotations.NotNull; -import org.togetherjava.tjbot.commands.componentids.ComponentId; -import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; -import org.togetherjava.tjbot.commands.componentids.Lifespan; import java.util.List; @@ -56,8 +53,8 @@ public interface BotCommand extends UserInteractor { /** * Gets the command data belonging to this command. *

- * See {@link net.dv8tion.jda.api.interactions.commands.build.Commands Commands} for details on how to - * create and configure instances of it. + * See {@link net.dv8tion.jda.api.interactions.commands.build.Commands Commands} for details on + * how to create and configure instances of it. *

*

* This method may be called multiple times, implementations must not create new data each time 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 1f5fea88c1..5d7d7751ef 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java @@ -96,7 +96,7 @@ public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args) { + @NotNull List args) { // Adapter does not react by default, subclasses may change this behavior } @@ -133,8 +133,8 @@ public void onSelectionMenu(@NotNull SelectMenuInteractionEvent event, */ @SuppressWarnings({"OverloadedVarargsMethod", "WeakerAccess"}) protected final @NotNull String generateComponentId(@NotNull Lifespan lifespan, - @NotNull String... args) { + @NotNull String... args) { return componentIdGenerator.generate(new ComponentId(getName(), Arrays.asList(args)), lifespan); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/CommandVisibility.java b/application/src/main/java/org/togetherjava/tjbot/commands/CommandVisibility.java index 86e8ca7870..f8a2284590 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/CommandVisibility.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/CommandVisibility.java @@ -12,4 +12,4 @@ public enum CommandVisibility { * The command can be used globally, outside a guild context. */ GLOBAL -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index e41e57c468..3a23925122 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -134,4 +134,4 @@ public static Collection createFeatures(JDA jda, Database database, Con return features; } -} \ No newline at end of file +} 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 4015f3f0eb..d53eb56b08 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java @@ -60,4 +60,4 @@ public interface MessageContextCommand extends BotCommand { * @param event the event that triggered this */ void onMessageContext(@NotNull MessageContextInteractionEvent event); -} \ No newline at end of file +} 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 a0793b4881..aff3d7e3a8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java @@ -12,9 +12,7 @@ import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; import org.jetbrains.annotations.NotNull; -import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; -import org.togetherjava.tjbot.commands.componentids.Lifespan; import java.util.List; @@ -74,7 +72,8 @@ public interface SlashCommand extends BotCommand { * * @return the command data of this command */ - @NotNull SlashCommandData getData(); + @NotNull + SlashCommandData getData(); /** * Triggered by the core system when a slash command corresponding to this implementation (based @@ -105,17 +104,17 @@ public interface SlashCommand extends BotCommand { * Autocomplete allows you to dynamically give the user a list of * {@value OptionData#MAX_CHOICES} possible choices. These choices can be generated based on the * input of the user, the functionality is comparable to Google's autocompletion when searching - * for something. The given choices are **not** enforced by Discord, the user can **ignore** + * for something. The given choices are not enforced by Discord, the user can ignore * auto-completion and send whatever they want. - *

+ *

* Triggered by the core system when a slash command's autocomplete corresponding to this * implementation (based on {@link #getData()}) has been triggered. Don't forget to enable * autocomplete using {@link OptionData#setAutoComplete(boolean)}! - *

+ *

* This method may be called multithreaded. In particular, there are no guarantees that it will * be executed on the same thread repeatedly or on the same thread that other event methods have * been called on. - *

+ *

* Details are available in the given event and the event also enables implementations to * respond to it.
* See {@link #acceptComponentIdGenerator(ComponentIdGenerator)} for more info on the ID's. diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java index 83bde647ea..cdf996ceaf 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java @@ -13,13 +13,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Unmodifiable; -import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; -import org.togetherjava.tjbot.commands.componentids.Lifespan; -import java.util.Arrays; import java.util.List; -import java.util.Objects; import java.util.function.Function; import java.util.stream.IntStream; @@ -34,9 +30,9 @@ *

*

* The adapter manages all command related data itself, which can be provided during construction - * (see {@link #SlashCommandAdapter(String, String, CommandVisibility)}). In order to add - * options, subcommands or similar command configurations, use {@link #getData()} and mutate the - * returned data object (see {@link CommandData} for details on how to work with this class). + * (see {@link #SlashCommandAdapter(String, String, CommandVisibility)}). In order to add options, + * subcommands or similar command configurations, use {@link #getData()} and mutate the returned + * data object (see {@link CommandData} for details on how to work with this class). *

*

* If implementations want to add buttons or selection menus, it is highly advised to use component @@ -79,8 +75,7 @@ public abstract class SlashCommandAdapter extends BotCommandAdapter implements S * {@link SlashCommandData#setDescription(String)} * @param visibility the visibility of the command */ - protected SlashCommandAdapter(String name, String description, - CommandVisibility visibility) { + protected SlashCommandAdapter(String name, String description, CommandVisibility visibility) { super(Commands.slash(name, description), visibility); this.description = description; 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 44bb8c1f81..ee6026279b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java @@ -61,4 +61,4 @@ public interface UserContextCommand extends BotCommand { * @param event the event that triggered this */ void onUserContext(@NotNull UserContextInteractionEvent event); -} \ No newline at end of file +} 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 19d64b0955..2bf4a8d314 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -97,4 +97,4 @@ public interface UserInteractor extends Feature { * @param generator the provided component id generator */ void acceptComponentIdGenerator(ComponentIdGenerator generator); -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java index 8096c811eb..27e165d827 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java @@ -1,8 +1,8 @@ package org.togetherjava.tjbot.commands.basic; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; /** * Implementation of an example command to illustrate how to respond to a user. diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java index 241df04bde..7cc62c5e0f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java @@ -15,11 +15,12 @@ import org.jetbrains.annotations.Contract; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.componentids.Lifespan; -import java.awt.Color; +import java.awt.*; +import java.util.List; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java index e6a0f2332a..1402a6ac0c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java @@ -15,8 +15,8 @@ import org.jetbrains.annotations.Contract; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import javax.annotation.Nullable; import java.util.List; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java index 3efe69662f..98cd906652 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java @@ -11,8 +11,8 @@ import net.dv8tion.jda.api.requests.RestAction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; import java.util.Optional; @@ -54,8 +54,7 @@ public final class AskCommand extends SlashCommandAdapter { * @param helper the helper to use */ public AskCommand(Config config, HelpSystemHelper helper) { - super("ask", "Ask a question - use this in the staging channel", - CommandVisibility.GUILD); + super("ask", "Ask a question - use this in the staging channel", CommandVisibility.GUILD); OptionData title = new OptionData(OptionType.STRING, TITLE_OPTION, "short and to the point", true); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java index ddabcd1005..e1f6da14b0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java @@ -11,8 +11,8 @@ import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.requests.RestAction; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; import java.time.Instant; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java index a53e7fddce..5e37b5cea0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java @@ -6,8 +6,8 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.interactions.commands.OptionType; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -40,8 +40,7 @@ public final class ChangeHelpTitleCommand extends SlashCommandAdapter { * @param helper the helper to use */ public ChangeHelpTitleCommand(HelpSystemHelper helper) { - super("change-help-title", "changes the title of a help thread", - CommandVisibility.GUILD); + super("change-help-title", "changes the title of a help thread", CommandVisibility.GUILD); getData().addOption(OptionType.STRING, TITLE_OPTION, "short and to the point", true); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java index 9a2a1621f9..d0f9aa7def 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java @@ -6,8 +6,8 @@ import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.ThreadChannel; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import java.time.Instant; import java.time.temporal.ChronoUnit; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java index 7ca74ca1c7..749a6e8b21 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java @@ -11,12 +11,11 @@ import org.scilab.forge.jlatexmath.TeXFormula; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import javax.imageio.ImageIO; -import java.awt.Color; -import java.awt.Image; +import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java index 04e4aa94fc..88eed4f95e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java @@ -6,8 +6,8 @@ import net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.requests.restaction.WebhookMessageUpdateAction; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; import java.net.http.HttpClient; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index fd8fb90c81..fa76fe6872 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -14,8 +14,8 @@ import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.utils.TimeUtil; import net.dv8tion.jda.internal.requests.CompletedRestAction; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import javax.annotation.Nullable; import java.time.Instant; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 8dc5001851..0373dfe648 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -16,8 +16,8 @@ import net.dv8tion.jda.api.utils.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import javax.annotation.Nullable; import java.time.Instant; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 4dbb69df4f..20ce1dfb13 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -14,8 +14,8 @@ import net.dv8tion.jda.api.utils.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import javax.annotation.Nullable; import java.util.Objects; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java index e7478a6497..8d805211cb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java @@ -11,8 +11,8 @@ import net.dv8tion.jda.api.utils.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; import javax.annotation.Nullable; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java index 20be0f49c9..429d32fc9e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java @@ -7,8 +7,8 @@ import net.dv8tion.jda.api.interactions.commands.OptionType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import javax.annotation.Nullable; import java.util.Objects; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java index a1554989e3..cf91af9a34 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java @@ -10,8 +10,8 @@ import net.dv8tion.jda.api.utils.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; import javax.annotation.Nullable; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index dc0078b3c1..e9e41d0abe 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -9,8 +9,8 @@ import net.dv8tion.jda.api.requests.ErrorResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import java.util.Objects; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java index a68b39ec42..4857b30f44 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java @@ -10,8 +10,8 @@ import net.dv8tion.jda.api.utils.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; import javax.annotation.Nullable; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java index 82aec3e2c5..8ce28d491f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java @@ -10,8 +10,8 @@ import net.dv8tion.jda.api.utils.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.config.Config; import javax.annotation.Nullable; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java index 39945da914..98958a8280 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java @@ -9,8 +9,8 @@ import net.dv8tion.jda.api.utils.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import javax.annotation.Nullable; import java.util.Objects; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java index 5dab37b64f..5f57d71024 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java @@ -8,12 +8,12 @@ import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.utils.DiscordClientAction; import javax.annotation.CheckReturnValue; -import java.awt.Color; +import java.awt.*; import java.time.Instant; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java index d3e513b11e..402324ad80 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java @@ -7,8 +7,8 @@ import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.OptionData; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.db.Database; import java.time.*; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index d72170ff93..162ec1defd 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -15,7 +15,7 @@ import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.requests.ErrorResponse; -import org.checkerframework.checker.units.qual.C; +import org.jetbrains.annotations.Unmodifiable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.*; @@ -26,6 +26,7 @@ import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; +import javax.annotation.Nullable; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -55,6 +56,7 @@ public final class BotCore extends ListenerAdapter implements CommandProvider { private static final ScheduledExecutorService ROUTINE_SERVICE = Executors.newScheduledThreadPool(5); private final Config config; + private final List interactors; private final Map nameToInteractor; private final List routines; private final ComponentIdParser componentIdParser; @@ -67,9 +69,9 @@ public final class BotCore extends ListenerAdapter implements CommandProvider { *

* Commands are fetched from {@link Features}. * - * @param jda the JDA instance that this command system will be used with + * @param jda the JDA instance that this command system will be used with * @param database the database that commands may use to persist data - * @param config the configuration to use for this system + * @param config the configuration to use for this system */ public BotCore(JDA jda, Database database, Config config) { this.config = config; @@ -77,85 +79,109 @@ public BotCore(JDA jda, Database database, Config config) { // Message receivers features.stream() - .filter(MessageReceiver.class::isInstance) - .map(MessageReceiver.class::cast) - .forEach(messageReceiver -> channelNameToMessageReceiver - .put(messageReceiver.getChannelNamePattern(), messageReceiver)); + .filter(MessageReceiver.class::isInstance) + .map(MessageReceiver.class::cast) + .forEach(messageReceiver -> channelNameToMessageReceiver + .put(messageReceiver.getChannelNamePattern(), messageReceiver)); // Event receivers features.stream() - .filter(EventReceiver.class::isInstance) - .map(EventReceiver.class::cast) - .forEach(jda::addEventListener); + .filter(EventReceiver.class::isInstance) + .map(EventReceiver.class::cast) + .forEach(jda::addEventListener); // Routines (are scheduled once the core is ready) routines = features.stream() - .filter(Routine.class::isInstance) - .map(Routine.class::cast) - .toList(); + .filter(Routine.class::isInstance) + .map(Routine.class::cast) + .toList(); // User Interactors (e.g. slash commands) - nameToInteractor = features.stream() - .filter(UserInteractor.class::isInstance) - .map(UserInteractor.class::cast) - .collect(Collectors.toMap(UserInteractor::getName, Function.identity())); - - // Reload Command - if (nameToInteractor.containsKey(RELOAD_COMMAND)) { - throw new IllegalStateException( - "The 'reload' command is a special reserved command that must not be used by other user interactors"); - } - nameToInteractor.put(RELOAD_COMMAND, new ReloadCommand(this)); + interactors = features.stream() + .filter(UserInteractor.class::isInstance) + .map(UserInteractor.class::cast) + .toList(); + + nameToInteractor = interactors.stream().collect(Collectors.toMap(interactor -> { + if (interactor instanceof SlashCommand) { + return "s-" + interactor.getName(); + } else if (interactor instanceof MessageContextCommand) { + return "mc-" + interactor.getName(); + } else if (interactor instanceof UserContextCommand) { + return "uc-" + interactor.getName(); + } + + return interactor.getName(); + }, Function.identity())); + // Component Id Store componentIdStore = new ComponentIdStore(database); componentIdStore.addComponentIdRemovedListener(BotCore::onComponentIdRemoved); componentIdParser = uuid -> componentIdStore.get(UUID.fromString(uuid)); - nameToInteractor.values() - .forEach(slashCommand -> slashCommand - .acceptComponentIdGenerator(((componentId, lifespan) -> { - UUID uuid = UUID.randomUUID(); - componentIdStore.putOrThrow(uuid, componentId, lifespan); - return uuid.toString(); - }))); + interactors.forEach(slashCommand -> slashCommand + .acceptComponentIdGenerator(((componentId, lifespan) -> { + UUID uuid = UUID.randomUUID(); + componentIdStore.putOrThrow(uuid, componentId, lifespan); + return uuid.toString(); + }))); if (logger.isInfoEnabled()) { - logger.info("Available user interactors: {}", nameToInteractor.keySet()); + logger.info("Available user interactors: {}", interactors); } } @Override - public Collection getBotCommands() { - return nameToInteractor.values() - .stream() - .filter(BotCommand.class::isInstance) - .map(BotCommand.class::cast) - .toList(); + @Unmodifiable + public Collection getInteractors() { + return interactors; } @Override - public > Optional getCommand(String name, C type) { + public Optional getInteractor(final String name) { - return Optional.ofNullable(nameToInteractor.get(name)) - .filter(type::isInstance) - .map(type::cast); + return Optional.ofNullable(nameToInteractor.get(name)); } - private static Class commandTypeToClass(Command.Type type) { - if (type == null) return BotCommand.class; - - return switch (type) { - case MESSAGE -> MessageContextCommand.class; - case USER -> UserContextCommand.class; - case SLASH -> SlashCommand.class; - case UNKNOWN -> BotCommand.class; - /* - preparing for the future be like: - case null -> BotCommand.class; - */ - }; + @Override + public > Optional getInteractor( + final String name, @Nullable final C type) { + String prefix = ""; + + if (null == type) { + List anyInteractors = getAnyInteractors(name); + if (anyInteractors.isEmpty()) { + return Optional.empty(); + } else { + return Optional.ofNullable((T) anyInteractors.get(0)); + } + } else if (SlashCommand.class.isAssignableFrom(type)) { + prefix = "s-"; + } else if (MessageContextCommand.class.isAssignableFrom(type)) { + prefix = "mc-"; + } else if (UserContextCommand.class.isAssignableFrom(type)) { + prefix = "uc-"; + } + + return Optional.ofNullable(nameToInteractor.get(prefix + name)) + .filter(type::isInstance) + .map(type::cast); + } + + public List getAnyInteractors(final String name) { + + List localInteractors = new ArrayList<>(4); + + getInteractor(name, SlashCommand.class).ifPresent(localInteractors::add); + getInteractor(name, MessageContextCommand.class).ifPresent(localInteractors::add); + getInteractor(name, UserContextCommand.class).ifPresent(localInteractors::add); + getInteractor(name, UserInteractor.class).ifPresent(localInteractors::add); + + + return localInteractors; } + /** * Trigger once JDA is ready. Subsequent calls are ignored. * @@ -170,7 +196,7 @@ public void onReady(JDA jda) { // Register reload on all guilds logger.debug("JDA is ready, registering reload command"); jda.getGuildCache() - .forEach(guild -> COMMAND_SERVICE.execute(() -> registerReloadCommand(guild))); + .forEach(guild -> COMMAND_SERVICE.execute(() -> registerReloadCommand(guild))); // NOTE We do not have to wait for reload to complete for the command system to be ready // itself logger.debug("Bot core is now ready"); @@ -206,7 +232,7 @@ private void scheduleRoutines(JDA jda) { public void onMessageReceived(final MessageReceivedEvent event) { if (event.isFromGuild()) { getMessageReceiversSubscribedTo(event.getChannel()) - .forEach(messageReceiver -> messageReceiver.onMessageReceived(event)); + .forEach(messageReceiver -> messageReceiver.onMessageReceived(event)); } } @@ -214,18 +240,18 @@ public void onMessageReceived(final MessageReceivedEvent event) { public void onMessageUpdate(final MessageUpdateEvent event) { if (event.isFromGuild()) { getMessageReceiversSubscribedTo(event.getChannel()) - .forEach(messageReceiver -> messageReceiver.onMessageUpdated(event)); + .forEach(messageReceiver -> messageReceiver.onMessageUpdated(event)); } } private Stream getMessageReceiversSubscribedTo(Channel channel) { String channelName = channel.getName(); return channelNameToMessageReceiver.entrySet() - .stream() - .filter(patternAndReceiver -> patternAndReceiver.getKey() - .matcher(channelName) - .matches()) - .map(Map.Entry::getValue); + .stream() + .filter(patternAndReceiver -> patternAndReceiver.getKey() + .matcher(channelName) + .matches()) + .map(Map.Entry::getValue); } @Override @@ -240,7 +266,7 @@ public void onButtonInteraction(ButtonInteractionEvent event) { logger.debug("Received button click '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); COMMAND_SERVICE - .execute(() -> forwardComponentCommand(event, UserInteractor::onButtonClick)); + .execute(() -> forwardComponentCommand(event, UserInteractor::onButtonClick)); } @Override @@ -248,7 +274,7 @@ public void onSelectMenuInteraction(SelectMenuInteractionEvent event) { logger.debug("Received selection menu event '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); COMMAND_SERVICE - .execute(() -> forwardComponentCommand(event, UserInteractor::onSelectionMenu)); + .execute(() -> forwardComponentCommand(event, UserInteractor::onSelectionMenu)); } private void registerReloadCommand(Guild guild) { @@ -263,8 +289,8 @@ private void registerReloadCommand(Guild guild) { logger.debug("Register '{}' for guild '{}'", RELOAD_COMMAND, guild.getName()); SlashCommand reloadCommand = requireSlashCommand(RELOAD_COMMAND); guild.upsertCommand(reloadCommand.getData()) - .queue(command -> logger.debug("Registered '{}' for guild '{}'", RELOAD_COMMAND, - guild.getName())); + .queue(command -> logger.debug("Registered '{}' for guild '{}'", RELOAD_COMMAND, + guild.getName())); }, ex -> handleRegisterErrors(ex, guild)); } @@ -279,22 +305,22 @@ private void registerReloadCommand(Guild guild) { * } * * - * @param event the component event that should be forwarded + * @param event the component event that should be forwarded * @param interactorArgumentConsumer the action to trigger on the associated user interactor, - * providing the event and list of arguments for consumption - * @param the type of the component interaction that should be forwarded + * providing the event and list of arguments for consumption + * @param the type of the component interaction that should be forwarded */ private void forwardComponentCommand(T event, - TriConsumer> interactorArgumentConsumer) { + TriConsumer> interactorArgumentConsumer) { Optional componentIdOpt; try { componentIdOpt = componentIdParser.parse(event.getComponentId()); } catch (InvalidComponentIdFormatException e) { logger - .error("Unable to route event (#{}) back to its corresponding user interactor. The component ID was in an unexpected format." - + " All button and menu events have to use a component ID created in a specific format" - + " (refer to the documentation of UserInteractor). Component ID was: {}", - event.getId(), event.getComponentId(), e); + .error("Unable to route event (#{}) back to its corresponding user interactor. The component ID was in an unexpected format." + + " All button and menu events have to use a component ID created in a specific format" + + " (refer to the documentation of UserInteractor). Component ID was: {}", + event.getId(), event.getComponentId(), e); // Unable to forward, simply fade out the event return; } @@ -302,13 +328,13 @@ private void forwardComponentCommand(T event, logger.warn("The event (#{}) has an expired component ID, which was: {}.", event.getId(), event.getComponentId()); event.reply("Sorry, but this event has expired. You can not use it anymore.") - .setEphemeral(true) - .queue(); + .setEphemeral(true) + .queue(); return; } ComponentId componentId = componentIdOpt.orElseThrow(); - UserInteractor interactor = requireUserInteractor(componentId.userInteractorName()); + UserInteractor interactor = requireUserInteractor(componentId.userInteractorName(), null); logger.trace("Routing a component event with id '{}' back to user interactor '{}'", event.getComponentId(), interactor.getName()); interactorArgumentConsumer.accept(interactor, event, componentId.elements()); @@ -322,7 +348,7 @@ private void forwardComponentCommand(T event, * @throws NullPointerException if the command with the given name was not registered */ private SlashCommand requireSlashCommand(String name) { - return getSlashCommand(name).orElseThrow( + return getInteractor(name, SlashCommand.class).orElseThrow( () -> new NullPointerException("There is no slash command with name " + name)); } @@ -331,10 +357,24 @@ private SlashCommand requireSlashCommand(String name) { * * @param name the name of the user interactor to get * @return the user interactor with the given name - * @throws NullPointerException if the user interactor with the given name was not registered + * @throws NoSuchElementException if the user interactor with the given name was not registered */ - private UserInteractor requireUserInteractor(String name) { - return Objects.requireNonNull(nameToInteractor.get(name)); + private UserInteractor requireUserInteractor( + final String name) { + return getInteractor(name).orElseThrow(); + } + + /** + * Gets the given user interactor by its name and requires that it exists. + * + * @param name the name of the user interactor to get + * @param type an {@link Class} instance of the required type + * @return the user interactor with the given name + * @throws NoSuchElementException if the user interactor with the given name was not registered + */ + private > T requireUserInteractor( + final String name, @Nullable final C type) { + return getInteractor(name, type).orElseThrow(); } private void handleRegisterErrors(Throwable ex, Guild guild) { @@ -343,17 +383,17 @@ private void handleRegisterErrors(Throwable ex, Guild guild) { // NOTE Unfortunately, there is no better accurate way to find a proper channel // where we can report the setup problems other than simply iterating all of them. Optional channelToReportTo = guild.getTextChannelCache() - .stream() - .filter(channel -> guild.getPublicRole() - .hasPermission(channel, Permission.MESSAGE_SEND)) - .findAny(); + .stream() + .filter(channel -> guild.getPublicRole() + .hasPermission(channel, Permission.MESSAGE_SEND)) + .findAny(); // Report the problem to the guild channelToReportTo.ifPresent(textChannel -> textChannel - .sendMessage("I need the commands scope, please invite me correctly." - + " You can join '%s' or visit '%s' for more info, I will leave your guild now." + .sendMessage("I need the commands scope, please invite me correctly." + + " You can join '%s' or visit '%s' for more info, I will leave your guild now." .formatted(config.getDiscordGuildInvite(), config.getProjectWebsite())) - .queue()); + .queue()); guild.leave().queue(); @@ -387,9 +427,9 @@ private interface TriConsumer { /** * Performs this operation on the given arguments. * - * @param first the first input argument + * @param first the first input argument * @param second the second input argument - * @param third the third input argument + * @param third the third input argument */ void accept(A first, B second, C third); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index 9bac61a8b4..2c28ce1961 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -1,7 +1,8 @@ package org.togetherjava.tjbot.commands.system; -import org.togetherjava.tjbot.commands.BotCommand; +import org.togetherjava.tjbot.commands.UserInteractor; +import javax.annotation.Nullable; import java.util.Collection; import java.util.Optional; @@ -10,11 +11,19 @@ */ public interface CommandProvider { /** - * Gets a list of all currently available and registered commands. + * Gets a list of all currently available and registered interactors. * - * @return all commands + * @return all interactors */ - Collection getBotCommands(); + Collection getInteractors(); + + /** + * Gets the slash command registered under the given name, if any. + * + * @param name the name of the command (including its prefix) + * @return the command registered under this name, if any + */ + public Optional getInteractor(String name); /** * Gets the slash command registered under the given name, if any. @@ -23,5 +32,6 @@ public interface CommandProvider { * @param type the type of the command * @return the command registered under this name, if any */ - public > Optional getCommand(String name, C type); + public > Optional getInteractor(String name, + @Nullable C type); } \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java index 8ad18a6129..14965a72f6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java @@ -6,8 +6,8 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.config.Configurator; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import java.util.stream.Stream; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java index 7d10e8ba09..772c5956f3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java @@ -5,8 +5,8 @@ import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import java.time.Instant; import java.util.Objects; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index ca0b3a190f..aa592d650b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -12,8 +12,8 @@ import net.dv8tion.jda.api.requests.ErrorResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.moderation.ModAuditLogWriter; import javax.annotation.Nullable; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java index e4abab24ae..c8e7b42611 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java @@ -6,8 +6,8 @@ import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import java.time.Instant; import java.util.Collection; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java index 0bea0716dd..668405d711 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java @@ -14,8 +14,8 @@ import org.jooq.impl.DSL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.CommandVisibility; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.db.Database; import javax.annotation.Nullable; diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java index 8878b6abcd..bc5b18ed7d 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java @@ -84,4 +84,4 @@ void generateComponentId() { assertEquals(lifespan.toString(), componentIdText[2]); } } -} \ No newline at end of file +} From 154a24d0a701ddc2d9de22085cc40c60edc466f6 Mon Sep 17 00:00:00 2001 From: Tijs Date: Wed, 7 Sep 2022 15:52:12 +0200 Subject: [PATCH 04/32] I like SpotlasSS!!!!!!!! --- .../org/togetherjava/tjbot/Application.java | 2 +- .../togetherjava/tjbot/CommandReloadUtil.java | 24 +++++++++---------- .../tjbot/commands/BotCommand.java | 2 +- .../tjbot/commands/SlashCommand.java | 6 ++--- .../tjbot/commands/SlashCommandAdapter.java | 2 +- .../tjbot/commands/basic/PingCommand.java | 2 +- .../commands/basic/RoleSelectCommand.java | 2 +- .../commands/basic/VcActivityCommand.java | 2 +- .../tjbot/commands/help/AskCommand.java | 2 +- .../help/ChangeHelpCategoryCommand.java | 2 +- .../commands/help/ChangeHelpTitleCommand.java | 2 +- .../tjbot/commands/help/CloseCommand.java | 2 +- .../commands/mathcommands/TeXCommand.java | 2 +- .../wolframalpha/WolframAlphaCommand.java | 2 +- .../commands/moderation/AuditCommand.java | 2 +- .../tjbot/commands/moderation/BanCommand.java | 2 +- .../commands/moderation/KickCommand.java | 2 +- .../commands/moderation/MuteCommand.java | 2 +- .../commands/moderation/NoteCommand.java | 2 +- .../moderation/QuarantineCommand.java | 2 +- .../commands/moderation/UnbanCommand.java | 2 +- .../commands/moderation/UnmuteCommand.java | 2 +- .../moderation/UnquarantineCommand.java | 2 +- .../commands/moderation/WarnCommand.java | 2 +- .../commands/moderation/WhoIsCommand.java | 2 +- .../commands/reminder/RemindCommand.java | 2 +- .../tjbot/commands/system/BotCore.java | 5 ++-- .../commands/system/CommandProvider.java | 2 +- .../commands/system/LogLevelCommand.java | 2 +- .../tjbot/commands/tags/TagCommand.java | 2 +- .../tjbot/commands/tags/TagManageCommand.java | 2 +- .../tjbot/commands/tags/TagsCommand.java | 2 +- .../commands/tophelper/TopHelpersCommand.java | 2 +- 33 files changed, 47 insertions(+), 48 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index 320538f626..87c2f43f45 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -116,4 +116,4 @@ private static void onUncaughtException(Thread failingThread, Throwable failure) logger.error("Unknown error in thread {}.", failingThread.getName(), failure); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java index 4e6f1e2bf1..4de0ffad82 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java @@ -32,10 +32,10 @@ public static void reloadCommands(JDA jda, CommandProvider commandProvider) { // bot might theoretically be part of so many guilds that it exceeds the max size of // list. However, correctly reducing RestActions in a stream is not trivial. getGuildUpdateActions(jda) - .map(updateAction -> updateCommandsIf( - command -> command.getVisibility() == CommandVisibility.GUILD, updateAction, - commandProvider)) - .forEach(actions::add); + .map(updateAction -> updateCommandsIf( + command -> command.getVisibility() == CommandVisibility.GUILD, updateAction, + commandProvider)) + .forEach(actions::add); logger.debug("Reloading commands over {} action-upstreams", actions.size()); // Send message when all are done @@ -47,19 +47,19 @@ public static void reloadCommands(JDA jda, CommandProvider commandProvider) { * through the given action upstream. * * @param commandFilter filter that matches commands that should be uploaded - * @param updateAction the upstream to update commands + * @param updateAction the upstream to update commands * @return the given upstream for chaining */ private static CommandListUpdateAction updateCommandsIf( Predicate commandFilter, CommandListUpdateAction updateAction, CommandProvider commandProvider) { return commandProvider.getInteractors() - .stream() - .filter(BotCommand.class::isInstance) - .map(BotCommand.class::cast) - .filter(commandFilter) - .map(BotCommand::getData) - .reduce(updateAction, CommandListUpdateAction::addCommands, (x, y) -> x); + .stream() + .filter(BotCommand.class::isInstance) + .map(BotCommand.class::cast) + .filter(commandFilter) + .map(BotCommand::getData) + .reduce(updateAction, CommandListUpdateAction::addCommands, (x, y) -> x); } private static CommandListUpdateAction getGlobalUpdateAction(JDA jda) { @@ -70,4 +70,4 @@ private static Stream getGuildUpdateActions(JDA jda) { return jda.getGuildCache().stream().map(Guild::updateCommands); } -} \ No newline at end of file +} 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 348ad4359b..817aa76066 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java @@ -65,4 +65,4 @@ public interface BotCommand extends UserInteractor { */ @NotNull CommandData getData(); -} \ No newline at end of file +} 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 aff3d7e3a8..9fa06221da 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java @@ -104,8 +104,8 @@ public interface SlashCommand extends BotCommand { * Autocomplete allows you to dynamically give the user a list of * {@value OptionData#MAX_CHOICES} possible choices. These choices can be generated based on the * input of the user, the functionality is comparable to Google's autocompletion when searching - * for something. The given choices are not enforced by Discord, the user can ignore - * auto-completion and send whatever they want. + * for something. The given choices are not enforced by Discord, the user can + * ignore auto-completion and send whatever they want. *

* Triggered by the core system when a slash command's autocomplete corresponding to this * implementation (based on {@link #getData()}) has been triggered. Don't forget to enable @@ -122,4 +122,4 @@ public interface SlashCommand extends BotCommand { * @param event the event that triggered this */ void onAutoComplete(@NotNull CommandAutoCompleteInteractionEvent event); -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java index cdf996ceaf..48c4a0afc0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java @@ -151,4 +151,4 @@ protected static List getMultipleOptionsByNamePrefix( .filter(option -> option.getName().startsWith(namePrefix)) .toList(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java index 27e165d827..fd5afa121a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/PingCommand.java @@ -26,4 +26,4 @@ public PingCommand() { public void onSlashCommand(SlashCommandInteractionEvent event) { event.reply("Pong!").queue(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java index 7cc62c5e0f..096bb3327a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/RoleSelectCommand.java @@ -267,4 +267,4 @@ private static MessageEmbed createEmbed(String title, CharSequence description) .setColor(AMBIENT_COLOR) .build(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java index 1402a6ac0c..1322c62591 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/VcActivityCommand.java @@ -237,4 +237,4 @@ private static Integer requireIntOptionIfPresent(@Nullable OptionMapping option) return option == null ? null : Math.toIntExact(option.getAsLong()); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java index 98cd906652..91ad35f2c0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java @@ -165,4 +165,4 @@ private static void handleFailure(Throwable exception, InteractionHook eventHook logger.error("Attempted to create a help thread, but failed", exception); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java index e1f6da14b0..9235ba053d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpCategoryCommand.java @@ -125,4 +125,4 @@ private boolean isHelpThreadOnCooldown(ThreadChannel helpThread) { .filter(Instant.now()::isBefore) .isPresent(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java index 5e37b5cea0..1510d54f2b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ChangeHelpTitleCommand.java @@ -104,4 +104,4 @@ private boolean handleIsValidTitle(CharSequence title, IReplyCallback event) { return false; } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java index d0f9aa7def..c50b14d1a7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/CloseCommand.java @@ -72,4 +72,4 @@ private boolean isHelpThreadOnCooldown(ThreadChannel helpThread) { .filter(Instant.now()::isBefore) .isPresent(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java index 749a6e8b21..cfbfe88632 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java @@ -167,4 +167,4 @@ public void onButtonClick(final ButtonInteractionEvent event, final List } event.getMessage().delete().queue(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java index 88eed4f95e..c599368bb0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java @@ -83,4 +83,4 @@ private static void sendResponse(WolframAlphaHandler.HandlerResponse response, action.queue(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index fa76fe6872..8fceb3adb6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -260,4 +260,4 @@ public void onButtonClick(ButtonInteractionEvent event, List args) { .flatMap(event::editMessage) .queue(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java index 0373dfe648..d42838dc35 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java @@ -215,4 +215,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { reason, deleteHistoryDays, guild, event)); }).queue(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java index 20ce1dfb13..04fa4d3184 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java @@ -139,4 +139,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } kickUserFlow(Objects.requireNonNull(target), author, reason, guild, event); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java index 8d805211cb..e977196cc0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java @@ -168,4 +168,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { muteUserFlow(Objects.requireNonNull(target), author, temporaryData.orElse(null), reason, guild, event); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java index 429d32fc9e..fc5017d170 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/NoteCommand.java @@ -92,4 +92,4 @@ private static void sendFeedback(User target, Member author, String noteContent, event.replyEmbeds(feedback).queue(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java index cf91af9a34..a75d73fdd2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/QuarantineCommand.java @@ -146,4 +146,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { quarantineUserFlow(Objects.requireNonNull(target), author, reason, guild, event); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java index e9e41d0abe..3aea534970 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java @@ -110,4 +110,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } unban(target, author, reason, guild, event); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java index 4857b30f44..48e61b86d1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java @@ -137,4 +137,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } unmuteUserFlow(Objects.requireNonNull(target), author, reason, guild, event); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java index 8ce28d491f..7736e9b87c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnquarantineCommand.java @@ -143,4 +143,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } unquarantineUserFlow(Objects.requireNonNull(target), author, reason, guild, event); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java index 98958a8280..e518e5c5da 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.java @@ -112,4 +112,4 @@ private boolean handleChecks(Member bot, Member author, @Nullable Member target, } return ModerationUtils.handleReason(reason, event); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java index 5f57d71024..fe7bfaad8e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/WhoIsCommand.java @@ -241,4 +241,4 @@ private static String formatUserFlags(final Collection flags) { .filter(name -> (name.contains("Hypesquad"))) .collect(Collectors.joining(", ")); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java index 402324ad80..6e2a4795ec 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindCommand.java @@ -150,4 +150,4 @@ private boolean handleIsUserBelowMaxPendingReminders(ISnowflake author, ISnowfla return false; } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 162ec1defd..2897df16c8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -359,8 +359,7 @@ private SlashCommand requireSlashCommand(String name) { * @return the user interactor with the given name * @throws NoSuchElementException if the user interactor with the given name was not registered */ - private UserInteractor requireUserInteractor( - final String name) { + private UserInteractor requireUserInteractor(final String name) { return getInteractor(name).orElseThrow(); } @@ -433,4 +432,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index 2c28ce1961..0059d8c4e1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -34,4 +34,4 @@ public interface CommandProvider { */ public > Optional getInteractor(String name, @Nullable C type); -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java index 14965a72f6..3e11f2f367 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java @@ -57,4 +57,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { Configurator.setAllLevels(LogManager.getRootLogger().getName(), level); event.reply("Set the log level to '%s'.".formatted(levelText)).queue(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java index 772c5956f3..2e00048a18 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagCommand.java @@ -62,4 +62,4 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { } message.queue(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index aa592d650b..f496ed104a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -419,4 +419,4 @@ String getActionVerb() { return actionVerb; } } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java index c8e7b42611..1d1bffd7b6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagsCommand.java @@ -85,4 +85,4 @@ public void onButtonClick(ButtonInteractionEvent event, List args) { } event.getMessage().delete().queue(); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java index 668405d711..1ce9b20b80 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java @@ -190,4 +190,4 @@ private record TopHelperResult(long authorId, BigDecimal messageLengths) { private record ColumnSetting(String headerName, HorizontalAlign alignment) { } -} \ No newline at end of file +} From 2d837b19eeefa1d7182f78c02d40c7a32a955209 Mon Sep 17 00:00:00 2001 From: Tijs Date: Wed, 7 Sep 2022 15:56:58 +0200 Subject: [PATCH 05/32] Removed unused method --- .../tjbot/commands/system/BotCore.java | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 2897df16c8..4ad732c41f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -334,7 +334,7 @@ private void forwardComponentCommand(T event, } ComponentId componentId = componentIdOpt.orElseThrow(); - UserInteractor interactor = requireUserInteractor(componentId.userInteractorName(), null); + UserInteractor interactor = requireUserInteractor(componentId.userInteractorName()); logger.trace("Routing a component event with id '{}' back to user interactor '{}'", event.getComponentId(), interactor.getName()); interactorArgumentConsumer.accept(interactor, event, componentId.elements()); @@ -363,19 +363,6 @@ private UserInteractor requireUserInteractor(final String name) { return getInteractor(name).orElseThrow(); } - /** - * Gets the given user interactor by its name and requires that it exists. - * - * @param name the name of the user interactor to get - * @param type an {@link Class} instance of the required type - * @return the user interactor with the given name - * @throws NoSuchElementException if the user interactor with the given name was not registered - */ - private > T requireUserInteractor( - final String name, @Nullable final C type) { - return getInteractor(name, type).orElseThrow(); - } - private void handleRegisterErrors(Throwable ex, Guild guild) { new ErrorHandler().handle(ErrorResponse.MISSING_ACCESS, errorResponse -> { // Find a channel that we have permissions to write to @@ -432,4 +419,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} +} \ No newline at end of file From 8ded6d34dae6ba51323785ad85c4acc91038e1e7 Mon Sep 17 00:00:00 2001 From: Tijs Date: Wed, 7 Sep 2022 16:00:58 +0200 Subject: [PATCH 06/32] homicide --- .../java/org/togetherjava/tjbot/commands/system/BotCore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 4ad732c41f..ed6cfbd1d4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -419,4 +419,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} \ No newline at end of file +} From e10afcc36d8de016f81d8c88ded690a13d9d19cb Mon Sep 17 00:00:00 2001 From: Tijs Date: Wed, 7 Sep 2022 16:05:59 +0200 Subject: [PATCH 07/32] I just commited homicide --- .../main/java/org/togetherjava/tjbot/CommandReloadUtil.java | 6 +++++- .../togetherjava/tjbot/commands/SlashCommandAdapter.java | 4 +--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java index 4de0ffad82..09be8f65c7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java @@ -19,6 +19,10 @@ public class CommandReloadUtil { private static final Logger logger = LoggerFactory.getLogger(CommandReloadUtil.class); + private CommandReloadUtil() { + throw new IllegalStateException("Utility class"); + } + public static void reloadCommands(JDA jda, CommandProvider commandProvider) { logger.info("Reloading commands..."); List actions = Collections.synchronizedList(new ArrayList<>()); @@ -70,4 +74,4 @@ private static Stream getGuildUpdateActions(JDA jda) { return jda.getGuildCache().stream().map(Guild::updateCommands); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java index 48c4a0afc0..2bfc69f8b9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java @@ -13,7 +13,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Unmodifiable; -import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; import java.util.List; import java.util.function.Function; @@ -64,7 +63,6 @@ public abstract class SlashCommandAdapter extends BotCommandAdapter implements SlashCommand { private final String description; private final SlashCommandData data; - private ComponentIdGenerator componentIdGenerator; /** * Creates a new adapter with the given data. @@ -151,4 +149,4 @@ protected static List getMultipleOptionsByNamePrefix( .filter(option -> option.getName().startsWith(namePrefix)) .toList(); } -} +} \ No newline at end of file From acfecbca448d4a3651eb0f5321ab95b2581f9660 Mon Sep 17 00:00:00 2001 From: Tijs Date: Wed, 7 Sep 2022 16:07:38 +0200 Subject: [PATCH 08/32] It's over, but not for me --- .../src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java | 2 +- .../org/togetherjava/tjbot/commands/SlashCommandAdapter.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java index 09be8f65c7..0634805d4b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java @@ -74,4 +74,4 @@ private static Stream getGuildUpdateActions(JDA jda) { return jda.getGuildCache().stream().map(Guild::updateCommands); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java index 2bfc69f8b9..54e636643d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java @@ -149,4 +149,4 @@ protected static List getMultipleOptionsByNamePrefix( .filter(option -> option.getName().startsWith(namePrefix)) .toList(); } -} \ No newline at end of file +} From d5dbaee2a08d228b0dd4650d734e50db6886c34b Mon Sep 17 00:00:00 2001 From: Tijs Date: Wed, 7 Sep 2022 16:11:59 +0200 Subject: [PATCH 09/32] okay this one was my fault ngl --- .../org/togetherjava/tjbot/commands/SlashCommandAdapter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java index 54e636643d..3d99b09aa2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java @@ -77,7 +77,7 @@ protected SlashCommandAdapter(String name, String description, CommandVisibility super(Commands.slash(name, description), visibility); this.description = description; - this.data = getData(); + this.data = (SlashCommandData) super.getData(); } @Override From fe10a3c832fd2350f8fbeec081454b124f45e80e Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 8 Sep 2022 10:25:07 +0200 Subject: [PATCH 10/32] now it should work pepepls --- .../togetherjava/tjbot/CommandReloadUtil.java | 10 +++++- .../tjbot/commands/system/BotCore.java | 7 ++-- .../commands/system/CommandProvider.java | 33 ++++++++++++++++--- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java index 0634805d4b..6cfe0d9c5d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java @@ -16,6 +16,9 @@ import java.util.function.Predicate; import java.util.stream.Stream; +/** + * Offers utility functions for reloading all commands. + */ public class CommandReloadUtil { private static final Logger logger = LoggerFactory.getLogger(CommandReloadUtil.class); @@ -23,6 +26,11 @@ private CommandReloadUtil() { throw new IllegalStateException("Utility class"); } + /** + * Reloads all commands based on the given {@link CommandProvider} + * @param jda the JDA to update commands on + * @param commandProvider the {@link CommandProvider} to grab commands from + */ public static void reloadCommands(JDA jda, CommandProvider commandProvider) { logger.info("Reloading commands..."); List actions = Collections.synchronizedList(new ArrayList<>()); @@ -74,4 +82,4 @@ private static Stream getGuildUpdateActions(JDA jda) { return jda.getGuildCache().stream().map(Guild::updateCommands); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index ed6cfbd1d4..a6b450056f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -143,9 +143,8 @@ public Optional getInteractor(final String name) { return Optional.ofNullable(nameToInteractor.get(name)); } - @Override - public > Optional getInteractor( - final String name, @Nullable final C type) { + public Optional getInteractor( + final String name, @Nullable final Class type) { String prefix = ""; if (null == type) { @@ -419,4 +418,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index 0059d8c4e1..a80831ba82 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -1,9 +1,11 @@ package org.togetherjava.tjbot.commands.system; +import org.togetherjava.tjbot.commands.BotCommand; import org.togetherjava.tjbot.commands.UserInteractor; import javax.annotation.Nullable; import java.util.Collection; +import java.util.List; import java.util.Optional; /** @@ -17,13 +19,26 @@ public interface CommandProvider { */ Collection getInteractors(); + /** + * Gets a list of all currently available and registered interactors. + * + * @return all interactors + */ + default Collection getCommands() { + return getInteractors() + .stream() + .filter(BotCommand.class::isInstance) + .map(BotCommand.class::cast) + .toList(); + } + /** * Gets the slash command registered under the given name, if any. * * @param name the name of the command (including its prefix) * @return the command registered under this name, if any */ - public Optional getInteractor(String name); + Optional getInteractor(String name); /** * Gets the slash command registered under the given name, if any. @@ -32,6 +47,16 @@ public interface CommandProvider { * @param type the type of the command * @return the command registered under this name, if any */ - public > Optional getInteractor(String name, - @Nullable C type); -} + Optional getInteractor(String name, + @Nullable Class type); + + /** + * Gets any interactors with the given name. + * The name you give should be without prefix, these methods checks all prefixes themselves. + * If your name is with prefix, use {@link #getInteractor(String)} + * + * @param name the name of the command + * @return a list of commands with that name + */ + List getAnyInteractors(final String name); +} \ No newline at end of file From 7ddf0e0f12032024ab93686ce6ab952b6c275494 Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 8 Sep 2022 10:56:32 +0200 Subject: [PATCH 11/32] i should find out how i can make spotless run automatically so i dont have to waste another 500 commits like i am right now cause its such a messy and annoying setup --- .../togetherjava/tjbot/CommandReloadUtil.java | 3 ++- .../tjbot/commands/system/BotCore.java | 6 +++--- .../commands/system/CommandProvider.java | 19 +++++++++---------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java index 6cfe0d9c5d..3bc439c56e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java @@ -28,6 +28,7 @@ private CommandReloadUtil() { /** * Reloads all commands based on the given {@link CommandProvider} + * * @param jda the JDA to update commands on * @param commandProvider the {@link CommandProvider} to grab commands from */ @@ -82,4 +83,4 @@ private static Stream getGuildUpdateActions(JDA jda) { return jda.getGuildCache().stream().map(Guild::updateCommands); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index a6b450056f..c82c4e8cdc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -143,8 +143,8 @@ public Optional getInteractor(final String name) { return Optional.ofNullable(nameToInteractor.get(name)); } - public Optional getInteractor( - final String name, @Nullable final Class type) { + public Optional getInteractor(final String name, + @Nullable final Class type) { String prefix = ""; if (null == type) { @@ -418,4 +418,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index a80831ba82..5651ee4be7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -25,11 +25,10 @@ public interface CommandProvider { * @return all interactors */ default Collection getCommands() { - return getInteractors() - .stream() - .filter(BotCommand.class::isInstance) - .map(BotCommand.class::cast) - .toList(); + return getInteractors().stream() + .filter(BotCommand.class::isInstance) + .map(BotCommand.class::cast) + .toList(); } /** @@ -48,15 +47,15 @@ default Collection getCommands() { * @return the command registered under this name, if any */ Optional getInteractor(String name, - @Nullable Class type); + @Nullable Class type); /** - * Gets any interactors with the given name. - * The name you give should be without prefix, these methods checks all prefixes themselves. - * If your name is with prefix, use {@link #getInteractor(String)} + * Gets any interactors with the given name. The name you give should be without prefix, these + * methods checks all prefixes themselves. If your name is with prefix, use + * {@link #getInteractor(String)} * * @param name the name of the command * @return a list of commands with that name */ List getAnyInteractors(final String name); -} \ No newline at end of file +} From dc6367ff389407f1c26cc391f31ec4bff4dc20f5 Mon Sep 17 00:00:00 2001 From: Tais993 Date: Thu, 15 Sep 2022 11:34:25 +0200 Subject: [PATCH 12/32] Removed `@NotNull` annotations and other minor improvements Added name variable, instead of 4 UserInteractor#getName calls Improved documentation at some places Added validation of UserInteractors And most importantly of all modifications, I applied spotless! --- .../tjbot/commands/BotCommand.java | 22 +++++++++-- .../tjbot/commands/BotCommandAdapter.java | 21 +++++----- .../tjbot/commands/MessageContextCommand.java | 2 +- .../tjbot/commands/SlashCommand.java | 3 +- .../tjbot/commands/SlashCommandAdapter.java | 4 +- .../tjbot/commands/UserContextCommand.java | 2 +- .../tjbot/commands/UserInteractor.java | 20 ++++------ .../tjbot/commands/system/BotCore.java | 38 +++++++++++++++++-- 8 files changed, 74 insertions(+), 38 deletions(-) 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 817aa76066..cce2d23a31 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java @@ -4,6 +4,7 @@ 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 net.dv8tion.jda.api.interactions.commands.build.Commands; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -29,6 +30,24 @@ */ public interface BotCommand extends UserInteractor { + /** + * Gets the name of the interactor. + *

+ * Requirements for this are documented in + * {@link net.dv8tion.jda.api.interactions.commands.build.Commands Commands}. If your command is + * a slash-command, take a look at + * {@link net.dv8tion.jda.api.interactions.commands.build.Commands#slash(String, String) + * Commands#slash(String, String)}. For context commands, use + * {@link Commands#context(Command.Type, String) Commands#context(Command.Type, String)}. + *

+ * It is advised to not start the name with {@code s-}, {@code mc-} or {@code uc-} + *

+ * After registration of the interactor, the name must not change anymore. + * + * @return the name of the interactor + */ + String getName(); + /** * Gets the type of this command. *

@@ -36,7 +55,6 @@ public interface BotCommand extends UserInteractor { * * @return the type of the command */ - @NotNull Command.Type getType(); @@ -47,7 +65,6 @@ public interface BotCommand extends UserInteractor { * * @return the visibility of the command */ - @NotNull CommandVisibility getVisibility(); /** @@ -63,6 +80,5 @@ public interface BotCommand extends UserInteractor { * * @return the command data of this command */ - @NotNull CommandData getData(); } 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 5d7d7751ef..bf4804bfef 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java @@ -51,7 +51,7 @@ public abstract class BotCommandAdapter implements BotCommand { * @param data the data for this command * @param visibility the visibility of the command */ - protected BotCommandAdapter(@NotNull CommandData data, @NotNull CommandVisibility visibility) { + protected BotCommandAdapter(CommandData data, CommandVisibility visibility) { this.data = Objects.requireNonNull(data, "The data shouldn't be null"); this.visibility = Objects.requireNonNull(visibility, "The visibility shouldn't be null"); @@ -60,43 +60,41 @@ protected BotCommandAdapter(@NotNull CommandData data, @NotNull CommandVisibilit } @Override - public final @NotNull String getName() { + public final String getName() { return name; } - @NotNull @Override public Command.Type getType() { return type; } @Override - public final @NotNull CommandVisibility getVisibility() { + public final CommandVisibility getVisibility() { return visibility; } @Override - public @NotNull CommandData getData() { + public CommandData getData() { return data; } @Override @Contract(mutates = "this") - public final void acceptComponentIdGenerator(@NotNull ComponentIdGenerator generator) { + public final void acceptComponentIdGenerator(ComponentIdGenerator generator) { componentIdGenerator = Objects.requireNonNull(generator, "The given generator cannot be null"); } @SuppressWarnings("NoopMethodInAbstractClass") @Override - public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args) { + public void onButtonClick(ButtonInteractionEvent event, List args) { // Adapter does not react by default, subclasses may change this behavior } @SuppressWarnings("NoopMethodInAbstractClass") @Override - public void onSelectionMenu(@NotNull SelectMenuInteractionEvent event, - @NotNull List args) { + public void onSelectionMenu(SelectMenuInteractionEvent event, List args) { // Adapter does not react by default, subclasses may change this behavior } @@ -115,7 +113,7 @@ public void onSelectionMenu(@NotNull SelectMenuInteractionEvent event, * @return the generated component ID */ @SuppressWarnings("OverloadedVarargsMethod") - protected final @NotNull String generateComponentId(@NotNull String... args) { + protected final String generateComponentId(String... args) { return generateComponentId(Lifespan.REGULAR, args); } @@ -132,8 +130,7 @@ public void onSelectionMenu(@NotNull SelectMenuInteractionEvent event, * @return the generated component ID */ @SuppressWarnings({"OverloadedVarargsMethod", "WeakerAccess"}) - protected final @NotNull String generateComponentId(@NotNull Lifespan lifespan, - @NotNull String... args) { + protected final String generateComponentId(Lifespan lifespan, String... args) { return componentIdGenerator.generate(new ComponentId(getName(), Arrays.asList(args)), lifespan); } 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 d53eb56b08..4bfe2b6fbe 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java @@ -59,5 +59,5 @@ public interface MessageContextCommand extends BotCommand { * * @param event the event that triggered this */ - void onMessageContext(@NotNull MessageContextInteractionEvent event); + void onMessageContext(MessageContextInteractionEvent event); } 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 9fa06221da..6b9e418492 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java @@ -72,7 +72,6 @@ public interface SlashCommand extends BotCommand { * * @return the command data of this command */ - @NotNull SlashCommandData getData(); /** @@ -121,5 +120,5 @@ public interface SlashCommand extends BotCommand { * * @param event the event that triggered this */ - void onAutoComplete(@NotNull CommandAutoCompleteInteractionEvent event); + void onAutoComplete(CommandAutoCompleteInteractionEvent event); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java index 3d99b09aa2..b262fef6f1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java @@ -86,13 +86,13 @@ public final String getDescription() { } @Override - public final @NotNull SlashCommandData getData() { + public final SlashCommandData getData() { return data; } @SuppressWarnings("NoopMethodInAbstractClass") @Override - public void onAutoComplete(@NotNull CommandAutoCompleteInteractionEvent event) { + public void onAutoComplete(CommandAutoCompleteInteractionEvent event) { // Adapter does not react by default, subclasses may change this behavior } 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 ee6026279b..82c2b60459 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java @@ -60,5 +60,5 @@ public interface UserContextCommand extends BotCommand { * * @param event the event that triggered this */ - void onUserContext(@NotNull UserContextInteractionEvent event); + void onUserContext(UserContextInteractionEvent event); } 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 2bf4a8d314..28fe1a30c4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -3,6 +3,8 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; 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.Commands; import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; import org.togetherjava.tjbot.commands.componentids.Lifespan; @@ -10,8 +12,8 @@ import java.util.List; /** - * Represents a feature that can interact with users. The most important implementation is - * {@link SlashCommand}. + * Represents a feature that can interact with users. The most used implementation is + * {@link SlashCommand}, {@link UserContextCommand} and {@link MessageContextCommand}. *

* An interactor must have a unique name and can react to button clicks and selection menu actions. */ @@ -20,9 +22,8 @@ public interface UserInteractor extends Feature { /** * Gets the name of the interactor. *

- * Requirements for this are documented in - * {@link net.dv8tion.jda.api.interactions.commands.build.Commands#slash(String, String)}. - *

+ * It is not advised to start the name with {@code s-}, {@code mc-} or {@code uc-} + * *

* After registration of the interactor, the name must not change anymore. * @@ -34,15 +35,12 @@ public interface UserInteractor extends Feature { * Triggered by the core system when a button corresponding to this implementation (based on * {@link #getName()}) has been clicked. *

- * This method may be called multi-threaded. In particular, there are no guarantees that it will + * This method may be called multithreaded. In particular, there are no guarantees that it will * be executed on the same thread repeatedly or on the same thread that other event methods have * been called on. *

* Details are available in the given event and the event also enables implementations to * respond to it. - *

- * This method will be called in a multi-threaded context and the event may not be hold valid - * forever. * * @param event the event that triggered this * @param args the arguments transported with the button, see @@ -61,9 +59,6 @@ public interface UserInteractor extends Feature { *

* Details are available in the given event and the event also enables implementations to * respond to it. - *

- * This method will be called in a multithreaded context and the event may not be hold valid - * forever. * * @param event the event that triggered this * @param args the arguments transported with the selection menu, see @@ -93,7 +88,6 @@ public interface UserInteractor extends Feature { * See Component-IDs on * our Wiki for more details and examples of how to use component IDs. * - * * @param generator the provided component id generator */ void acceptComponentIdGenerator(ComponentIdGenerator generator); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index c82c4e8cdc..a0f1a2f2c5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -15,6 +15,7 @@ import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.requests.ErrorResponse; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Unmodifiable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,6 +34,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -100,18 +102,21 @@ public BotCore(JDA jda, Database database, Config config) { interactors = features.stream() .filter(UserInteractor.class::isInstance) .map(UserInteractor.class::cast) + .filter(validateInteractor()) .toList(); nameToInteractor = interactors.stream().collect(Collectors.toMap(interactor -> { + String name = interactor.getName(); + if (interactor instanceof SlashCommand) { - return "s-" + interactor.getName(); + return "s-" + name; } else if (interactor instanceof MessageContextCommand) { - return "mc-" + interactor.getName(); + return "mc-" + name; } else if (interactor instanceof UserContextCommand) { - return "uc-" + interactor.getName(); + return "uc-" + name; } - return interactor.getName(); + return name; }, Function.identity())); @@ -131,6 +136,31 @@ public BotCore(JDA jda, Database database, Config config) { } } + /** + * Validates the given interactor + * + * @return whenever the command should be checked + */ + @NotNull + private static Predicate validateInteractor() { + return interactor -> { + String name = interactor.getName(); + + if (name == null) { + logger.error("An interactor's name shouldn't be null! {}", interactor); + return false; + } + + if (name.startsWith("s-") || name.startsWith("mc-") || name.startsWith("uc-")) { + logger.warn( + "Names that starts with s-, mc- or uc- aren't recommended and might break other commands. (name: {})", + name); + } + + return true; + }; + } + @Override @Unmodifiable public Collection getInteractors() { From a5946210631f0945be8a3884d8a3b614c94bec0e Mon Sep 17 00:00:00 2001 From: Tais993 Date: Thu, 15 Sep 2022 13:37:14 +0200 Subject: [PATCH 13/32] Optimized imports --- .../java/org/togetherjava/tjbot/commands/UserInteractor.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 28fe1a30c4..3d3443c7f1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -3,8 +3,6 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; 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.Commands; import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; import org.togetherjava.tjbot.commands.componentids.Lifespan; @@ -91,4 +89,4 @@ public interface UserInteractor extends Feature { * @param generator the provided component id generator */ void acceptComponentIdGenerator(ComponentIdGenerator generator); -} +} \ No newline at end of file From 51bbcc2cdf1117e5332397f27d9ff9e833a5135e Mon Sep 17 00:00:00 2001 From: Tais993 Date: Thu, 15 Sep 2022 13:40:12 +0200 Subject: [PATCH 14/32] homicide. --- .../java/org/togetherjava/tjbot/commands/UserInteractor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3d3443c7f1..d50c34421a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -89,4 +89,4 @@ public interface UserInteractor extends Feature { * @param generator the provided component id generator */ void acceptComponentIdGenerator(ComponentIdGenerator generator); -} \ No newline at end of file +} From 0979e537dc6414af837345fe3f5f180286a3fd9a Mon Sep 17 00:00:00 2001 From: Tijs Date: Fri, 23 Sep 2022 09:11:30 +0200 Subject: [PATCH 15/32] Implemented UserInteractorPrefix and some other review fixes UserInteractorPrefix is an enum which contains the prefixes for specific UserInteractor types (slash-commands, message-context-command) Renamed CommandReloadUtil.java to CommandReloading.java Also modified the docs of UserInteractor#getName and BotCommand#getName --- .../org/togetherjava/tjbot/Application.java | 2 +- ...dReloadUtil.java => CommandReloading.java} | 20 ++++--- .../tjbot/commands/BotCommand.java | 10 +--- .../tjbot/commands/SlashCommand.java | 10 +--- .../tjbot/commands/UserInteractor.java | 3 +- .../tjbot/commands/UserInteractorPrefix.java | 60 +++++++++++++++++++ .../tjbot/commands/system/BotCore.java | 16 +---- 7 files changed, 80 insertions(+), 41 deletions(-) rename application/src/main/java/org/togetherjava/tjbot/{CommandReloadUtil.java => CommandReloading.java} (89%) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index 87c2f43f45..265493deeb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -84,7 +84,7 @@ public static void runBot(Config config) { BotCore core = new BotCore(jda, database, config); jda.addEventListener(core); - CommandReloadUtil.reloadCommands(jda, core); + CommandReloading.reloadCommands(jda, core); jda.awaitReady(); // We fire the event manually, since the core might be added too late to receive the diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java similarity index 89% rename from application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java rename to application/src/main/java/org/togetherjava/tjbot/CommandReloading.java index 3bc439c56e..78bd5a2d05 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloadUtil.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java @@ -4,6 +4,7 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; +import org.jetbrains.annotations.Contract; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.BotCommand; @@ -19,16 +20,16 @@ /** * Offers utility functions for reloading all commands. */ -public class CommandReloadUtil { - private static final Logger logger = LoggerFactory.getLogger(CommandReloadUtil.class); +public class CommandReloading { + private static final Logger logger = LoggerFactory.getLogger(CommandReloading.class); - private CommandReloadUtil() { - throw new IllegalStateException("Utility class"); + private CommandReloading() { + throw new UnsupportedOperationException("Utility class"); } /** - * Reloads all commands based on the given {@link CommandProvider} - * + * Reloads all commands based on the given {@link CommandProvider}. + * * @param jda the JDA to update commands on * @param commandProvider the {@link CommandProvider} to grab commands from */ @@ -63,16 +64,19 @@ public static void reloadCommands(JDA jda, CommandProvider commandProvider) { * @param updateAction the upstream to update commands * @return the given upstream for chaining */ + @Contract("_, _, _ -> param2") private static CommandListUpdateAction updateCommandsIf( Predicate commandFilter, CommandListUpdateAction updateAction, CommandProvider commandProvider) { - return commandProvider.getInteractors() + commandProvider.getInteractors() .stream() .filter(BotCommand.class::isInstance) .map(BotCommand.class::cast) .filter(commandFilter) .map(BotCommand::getData) - .reduce(updateAction, CommandListUpdateAction::addCommands, (x, y) -> x); + .forEach(updateAction::addCommands); + + return updateAction; } private static CommandListUpdateAction getGlobalUpdateAction(JDA jda) { 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 cce2d23a31..fa7afe249c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java @@ -5,7 +5,6 @@ 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.Commands; -import org.jetbrains.annotations.NotNull; import java.util.List; @@ -16,7 +15,6 @@ * {@link BotCommandAdapter} available that implemented most methods already. A new command can then * be registered by adding it to {@link Features}. *

- *

* Commands can either be visible globally in Discord or just to specific guilds. Some * configurations can be made via {@link CommandData}, which is then to be returned by * {@link #getData()} where the system will then pick it up from. @@ -25,13 +23,12 @@ * method, buttons ({@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 BotCommand extends UserInteractor { /** - * Gets the name of the interactor. + * {@inheritDoc} *

* Requirements for this are documented in * {@link net.dv8tion.jda.api.interactions.commands.build.Commands Commands}. If your command is @@ -39,10 +36,6 @@ public interface BotCommand extends UserInteractor { * {@link net.dv8tion.jda.api.interactions.commands.build.Commands#slash(String, String) * Commands#slash(String, String)}. For context commands, use * {@link Commands#context(Command.Type, String) Commands#context(Command.Type, String)}. - *

- * It is advised to not start the name with {@code s-}, {@code mc-} or {@code uc-} - *

- * After registration of the interactor, the name must not change anymore. * * @return the name of the interactor */ @@ -73,7 +66,6 @@ public interface BotCommand extends UserInteractor { * See {@link net.dv8tion.jda.api.interactions.commands.build.Commands Commands} for details on * how to create and configure instances of it. *

- *

* This method may be called multiple times, implementations must not create new data each time * but instead configure it once beforehand. The core system will automatically call this method * to register the command to Discord. 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 6b9e418492..53b5456d65 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java @@ -11,7 +11,6 @@ import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; -import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; import java.util.List; @@ -19,18 +18,16 @@ /** * A slash-command is a command in Discord, with slash (/) as the prefix. These commands offer * enhanced functionality and superior UX over text-commands. An example slash-command is the - * `/thread` command, this allows you to create threads using your keyboard. Try it out yourself! + * `/thread` command, this allows you to create threads using your keyboard. * *

- *

* Represents a Discord slash-command. Mostly decorating - * {@link net.dv8tion.jda.api.interactions.commands.Command}. + * {@link net.dv8tion.jda.api.interactions.commands.Command Command}. *

* All slash commands have to implement this interface. For convenience, there is a * {@link SlashCommandAdapter} available that implemented most methods already. A new command can * then be registered by adding it to {@link Features}. *

- *

* Slash commands can either be visible globally in Discord or just to specific guilds. They can * have subcommands, options, menus and more. This can be configured via {@link CommandData}, which * is then to be returned by {@link #getData()} where the system will then pick it up from. @@ -40,7 +37,6 @@ * ({@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 SlashCommand extends BotCommand { @@ -50,7 +46,6 @@ public interface SlashCommand extends BotCommand { *

* Requirements for this are documented in {@link Commands#slash(String, String)}. *

- *

* After registration of the command, the description must not change anymore. * * @return the description of the command @@ -65,7 +60,6 @@ public interface SlashCommand extends BotCommand { *

* See {@link CommandData} for details on how to create and configure instances of it. *

- *

* This method may be called multiple times, implementations must not create new data each time * but instead configure it once beforehand. The core system will automatically call this method * to register the command to Discord. 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 d50c34421a..5f761103d3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -20,8 +20,7 @@ public interface UserInteractor extends Feature { /** * Gets the name of the interactor. *

- * It is not advised to start the name with {@code s-}, {@code mc-} or {@code uc-} - * + * You cannot start the name with any of the prefixes found in {@link UserInteractorPrefix} *

* After registration of the interactor, the name must not change anymore. * diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java new file mode 100644 index 0000000000..90eef80752 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java @@ -0,0 +1,60 @@ +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 really useful. + */ +public enum UserInteractorPrefix { + SLASH_COMMAND("s-"), + MESSAGE_CONTEXT_COMMAND("mc-"), + USER_CONTEXT_COMMAND("uc-"); + + private final String prefix; + + UserInteractorPrefix(final String prefix) { + this.prefix = prefix; + } + + public String getPrefix() { + return prefix; + } + + /** + * Checks what enum value the given instance collaborates to. + *

+ * As example, + * + * @param instance an instance to type check for a prefix + * @param the type of the instance + * @return the prefixed {@link String} + */ + public static String getPrefixFromInstance(final T instance) { + String name = instance.getName(); + + String prefix = ""; + + // If this gets bigger, add a class variable to the constructor, but for now keep it simple + // / hardcoded, if we rework this, we should also add a check that makes sure there's no + // prefixes in names + if (instance instanceof SlashCommand) { + prefix = SLASH_COMMAND.getPrefix(); + } else if (instance instanceof MessageContextCommand) { + prefix = MESSAGE_CONTEXT_COMMAND.getPrefix(); + } else if (instance instanceof UserContextCommand) { + prefix = USER_CONTEXT_COMMAND.getPrefix(); + } + + return prefix + name; + } + + @Override + @Contract(pure = true) + public String toString() { + return "UserInteractorPrefix{" + "prefix='" + prefix + '\'' + '}'; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index a0f1a2f2c5..169c3ac5d0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -105,19 +105,9 @@ public BotCore(JDA jda, Database database, Config config) { .filter(validateInteractor()) .toList(); - nameToInteractor = interactors.stream().collect(Collectors.toMap(interactor -> { - String name = interactor.getName(); - - if (interactor instanceof SlashCommand) { - return "s-" + name; - } else if (interactor instanceof MessageContextCommand) { - return "mc-" + name; - } else if (interactor instanceof UserContextCommand) { - return "uc-" + name; - } - - return name; - }, Function.identity())); + nameToInteractor = interactors.stream() + .collect(Collectors.toMap(UserInteractorPrefix::getPrefixFromInstance, + Function.identity())); // Component Id Store From 24597aab9d33f1e643164dbd270623cf06acdaed Mon Sep 17 00:00:00 2001 From: Tijs Date: Tue, 27 Sep 2022 13:27:51 +0200 Subject: [PATCH 16/32] CR changes From UserInteractorPrefix the getPrefixFromInstance method has been renamed to getPrefixedNameFromInstance. And getPrefixedNameFromClass has been added (takes a Class instead of instance) The getInteractor(String name, Class type) has been renamed Documentation changes Removed the interactors variable in the BotCore class, instead the nameToInteractor hashmap's values will be used And removed unused @NotNull imports Sonarlint gives no warnings, so please let this be true? --- .../tjbot/commands/BotCommandAdapter.java | 1 - .../tjbot/commands/MessageContextCommand.java | 1 - .../tjbot/commands/SlashCommandAdapter.java | 4 - .../tjbot/commands/UserContextCommand.java | 1 - .../tjbot/commands/UserInteractorPrefix.java | 79 +++++++++++++++---- .../tjbot/commands/system/BotCore.java | 62 +++++---------- .../commands/system/CommandProvider.java | 31 +++++--- 7 files changed, 103 insertions(+), 76 deletions(-) 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 bf4804bfef..c03d0b82ca 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java @@ -6,7 +6,6 @@ import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; import org.togetherjava.tjbot.commands.componentids.Lifespan; 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 4bfe2b6fbe..5a29c0c873 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java @@ -7,7 +7,6 @@ import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; -import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; import java.util.List; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java index b262fef6f1..9923a15ea4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java @@ -10,7 +10,6 @@ import net.dv8tion.jda.api.interactions.commands.build.Commands; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Unmodifiable; @@ -27,18 +26,15 @@ * {@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 all command related data itself, which can be provided during construction * (see {@link #SlashCommandAdapter(String, String, CommandVisibility)}). In order to add options, * subcommands or similar command configurations, use {@link #getData()} and mutate the returned * data object (see {@link CommandData} for details on how to work with this class). *

- *

* 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}. A minimal * setup would consist of a class like * 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 82c2b60459..95e278bbad 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java @@ -7,7 +7,6 @@ import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; -import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; import java.util.List; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java index 90eef80752..12ef58569b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java @@ -8,22 +8,48 @@ * 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 really useful. + *

+ * The */ public enum UserInteractorPrefix { - SLASH_COMMAND("s-"), - MESSAGE_CONTEXT_COMMAND("mc-"), - USER_CONTEXT_COMMAND("uc-"); + /* + * order CANNOT be modified This is because USER_INTERACTOR applies to every interactor, but + * still I thought it'd be useful to document there's no prefix. + */ + + SLASH_COMMAND(SlashCommand.class, "s-"), + MESSAGE_CONTEXT_COMMAND(MessageContextCommand.class, "mc-"), + USER_CONTEXT_COMMAND(UserContextCommand.class, "uc-"), + USER_INTERACTOR(UserInteractor.class, ""); + + private final Class classType; private final String prefix; - UserInteractorPrefix(final String prefix) { + UserInteractorPrefix(Class classType, final String prefix) { + this.classType = classType; this.prefix = prefix; } + /** + * The prefix for the command + * + * @return + */ public String getPrefix() { return prefix; } + + /** + * The class type that should receive the prefix + * + * @return + */ + public Class getClassType() { + return classType; + } + /** * Checks what enum value the given instance collaborates to. *

@@ -33,23 +59,42 @@ public String getPrefix() { * @param the type of the instance * @return the prefixed {@link String} */ - public static String getPrefixFromInstance(final T instance) { + public static String getPrefixedNameFromInstance(final T instance) { String name = instance.getName(); - String prefix = ""; - - // If this gets bigger, add a class variable to the constructor, but for now keep it simple - // / hardcoded, if we rework this, we should also add a check that makes sure there's no - // prefixes in names - if (instance instanceof SlashCommand) { - prefix = SLASH_COMMAND.getPrefix(); - } else if (instance instanceof MessageContextCommand) { - prefix = MESSAGE_CONTEXT_COMMAND.getPrefix(); - } else if (instance instanceof UserContextCommand) { - prefix = USER_CONTEXT_COMMAND.getPrefix(); + 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. + *

+ * As example, + * + * @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 prefix + name; + return name; } @Override diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 169c3ac5d0..be0bfa2506 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -15,7 +15,6 @@ import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.requests.ErrorResponse; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Unmodifiable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,7 +26,6 @@ import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; -import javax.annotation.Nullable; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -58,7 +56,6 @@ public final class BotCore extends ListenerAdapter implements CommandProvider { private static final ScheduledExecutorService ROUTINE_SERVICE = Executors.newScheduledThreadPool(5); private final Config config; - private final List interactors; private final Map nameToInteractor; private final List routines; private final ComponentIdParser componentIdParser; @@ -99,14 +96,11 @@ public BotCore(JDA jda, Database database, Config config) { .toList(); // User Interactors (e.g. slash commands) - interactors = features.stream() + nameToInteractor = features.stream() .filter(UserInteractor.class::isInstance) .map(UserInteractor.class::cast) - .filter(validateInteractor()) - .toList(); - - nameToInteractor = interactors.stream() - .collect(Collectors.toMap(UserInteractorPrefix::getPrefixFromInstance, + .filter(validateInteractorPredicate()) + .collect(Collectors.toMap(UserInteractorPrefix::getPrefixedNameFromInstance, Function.identity())); @@ -114,6 +108,8 @@ public BotCore(JDA jda, Database database, Config config) { componentIdStore = new ComponentIdStore(database); componentIdStore.addComponentIdRemovedListener(BotCore::onComponentIdRemoved); componentIdParser = uuid -> componentIdStore.get(UUID.fromString(uuid)); + Collection interactors = getInteractors(); + interactors.forEach(slashCommand -> slashCommand .acceptComponentIdGenerator(((componentId, lifespan) -> { UUID uuid = UUID.randomUUID(); @@ -127,12 +123,11 @@ public BotCore(JDA jda, Database database, Config config) { } /** - * Validates the given interactor + * Returns a predicate which validates the given interactor * - * @return whenever the command should be checked + * @return A predicate which validates the given interactor */ - @NotNull - private static Predicate validateInteractor() { + private static Predicate validateInteractorPredicate() { return interactor -> { String name = interactor.getName(); @@ -142,9 +137,7 @@ private static Predicate validateInteractor() { } if (name.startsWith("s-") || name.startsWith("mc-") || name.startsWith("uc-")) { - logger.warn( - "Names that starts with s-, mc- or uc- aren't recommended and might break other commands. (name: {})", - name); + throw new IllegalArgumentException("The interactor"); } return true; @@ -154,7 +147,7 @@ private static Predicate validateInteractor() { @Override @Unmodifiable public Collection getInteractors() { - return interactors; + return nameToInteractor.values(); } @Override @@ -163,39 +156,24 @@ public Optional getInteractor(final String name) { return Optional.ofNullable(nameToInteractor.get(name)); } + @Override public Optional getInteractor(final String name, - @Nullable final Class type) { - String prefix = ""; - - if (null == type) { - List anyInteractors = getAnyInteractors(name); - if (anyInteractors.isEmpty()) { - return Optional.empty(); - } else { - return Optional.ofNullable((T) anyInteractors.get(0)); - } - } else if (SlashCommand.class.isAssignableFrom(type)) { - prefix = "s-"; - } else if (MessageContextCommand.class.isAssignableFrom(type)) { - prefix = "mc-"; - } else if (UserContextCommand.class.isAssignableFrom(type)) { - prefix = "uc-"; - } + final Class type) { + Objects.requireNonNull(type, "The given type cannot be null"); + String prefix = UserInteractorPrefix.getPrefixedNameFromClass(type, name); return Optional.ofNullable(nameToInteractor.get(prefix + name)) .filter(type::isInstance) .map(type::cast); } - public List getAnyInteractors(final String name) { - + @Override + public List getInteractorsWithName(final String name) { List localInteractors = new ArrayList<>(4); - getInteractor(name, SlashCommand.class).ifPresent(localInteractors::add); - getInteractor(name, MessageContextCommand.class).ifPresent(localInteractors::add); - getInteractor(name, UserContextCommand.class).ifPresent(localInteractors::add); - getInteractor(name, UserInteractor.class).ifPresent(localInteractors::add); - + for (UserInteractorPrefix value : UserInteractorPrefix.values()) { + getInteractor(name, value.getClassType()).ifPresent(localInteractors::add); + } return localInteractors; } @@ -438,4 +416,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index 5651ee4be7..ff5d87205a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -20,9 +20,9 @@ public interface CommandProvider { Collection getInteractors(); /** - * Gets a list of all currently available and registered interactors. + * Gets a list of all currently available and registered bot commands. * - * @return all interactors + * @return all bot commands */ default Collection getCommands() { return getInteractors().stream() @@ -32,19 +32,30 @@ default Collection getCommands() { } /** - * Gets the slash command registered under the given name, if any. + * Gets the interactor registered under the given name, if any. + *

+ * This command excepts the name with the prefix of the command, if you prefer using a + * {@link Class} instance of the command/type instead, use {@link #getInteractor(String, Class)} + * instead.- * - * @param name the name of the command (including its prefix) - * @return the command registered under this name, if any + * @param name the name of the command (including its prefix, see + * {@link org.togetherjava.tjbot.commands.UserInteractorPrefix UserInteractorPrefix} + * @return the interactor registered under this name, if any */ Optional getInteractor(String name); /** - * Gets the slash command registered under the given name, if any. + * Gets the interactor registered under the given name, if any. + *

+ * Unlike {@link #getInteractor(String)}, this command expects the name, without the prefix (see + * {@link org.togetherjava.tjbot.commands.UserInteractorPrefix UserInteractorPrefix}). Instead, + * this command excepts a {@link Class} instance of the commands class. This can be + * {@link org.togetherjava.tjbot.commands.SlashCommand SlashCommand}, or even as specific as + * {@link org.togetherjava.tjbot.commands.basic.PingCommand PingCommand}. * - * @param name the name of the command + * @param name the name of the command without prefix * @param type the type of the command - * @return the command registered under this name, if any + * @return the interactor registered under this name, if any */ Optional getInteractor(String name, @Nullable Class type); @@ -55,7 +66,7 @@ Optional getInteractor(String name, * {@link #getInteractor(String)} * * @param name the name of the command - * @return a list of commands with that name + * @return a list of commands with that name, can be empty if none match */ - List getAnyInteractors(final String name); + List getInteractorsWithName(final String name); } From 63c4496111577cb39b4fda070d845be9c203fc90 Mon Sep 17 00:00:00 2001 From: Tijs Date: Tue, 27 Sep 2022 13:34:59 +0200 Subject: [PATCH 17/32] =?UTF-8?q?=20=E2=A0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/togetherjava/tjbot/commands/system/BotCore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index be0bfa2506..915fa82085 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -416,4 +416,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} \ No newline at end of file +} From 282e06b351705c952fd8228798d50625b4954908 Mon Sep 17 00:00:00 2001 From: Tijs Date: Tue, 27 Sep 2022 13:41:38 +0200 Subject: [PATCH 18/32] =?UTF-8?q?=20=E2=A0=80pleaase=20sonarlint=20get=20y?= =?UTF-8?q?ourself=20working=20ur=20actually=20the=20most=20useless=20exte?= =?UTF-8?q?ntion=20ever,=20you=20don't=20even=20work,=20you=20never=20work?= =?UTF-8?q?ed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../togetherjava/tjbot/commands/system/CommandProvider.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index ff5d87205a..939871d052 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -57,8 +57,7 @@ default Collection getCommands() { * @param type the type of the command * @return the interactor registered under this name, if any */ - Optional getInteractor(String name, - @Nullable Class type); + Optional getInteractor(String name, Class type); /** * Gets any interactors with the given name. The name you give should be without prefix, these From 04b56b158f5760023ff25517d2113945e93d64d8 Mon Sep 17 00:00:00 2001 From: Tijs Date: Tue, 27 Sep 2022 13:51:23 +0200 Subject: [PATCH 19/32] i hate this shit so fucking much --- .../org/togetherjava/tjbot/commands/system/CommandProvider.java | 1 - 1 file changed, 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index 939871d052..96315d78b8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -3,7 +3,6 @@ import org.togetherjava.tjbot.commands.BotCommand; import org.togetherjava.tjbot.commands.UserInteractor; -import javax.annotation.Nullable; import java.util.Collection; import java.util.List; import java.util.Optional; From 3af61089a735a08f3c1a3cce7021d82f6540abc3 Mon Sep 17 00:00:00 2001 From: Tijs Date: Tue, 27 Sep 2022 16:32:21 +0200 Subject: [PATCH 20/32] CR fixes --- .../tjbot/commands/UserInteractorPrefix.java | 2 -- .../togetherjava/tjbot/commands/system/BotCore.java | 13 ++++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java index 12ef58569b..45c9017415 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java @@ -8,8 +8,6 @@ * 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 really useful. - *

- * The */ public enum UserInteractorPrefix { /* diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 915fa82085..1b5d7759e1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -136,8 +136,19 @@ private static Predicate validateInteractorPredicate() { return false; } + for (UserInteractorPrefix value : UserInteractorPrefix.values()) { + String prefix = value.getPrefix(); + + if (name.startsWith(prefix)) { + throw new IllegalArgumentException( + "The interactor's name cannot start with any of the reserved prefixes. (" + + prefix + ")"); + } + } + if (name.startsWith("s-") || name.startsWith("mc-") || name.startsWith("uc-")) { - throw new IllegalArgumentException("The interactor"); + throw new IllegalArgumentException( + "The interactor's name cannot start with any of the prefixes."); } return true; From 5df1f0ac8a2352ef39224cadd0b8be2162b09ea9 Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 29 Sep 2022 09:36:02 +0200 Subject: [PATCH 21/32] CR fixes --- .../tjbot/commands/UserInteractorPrefix.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java index 45c9017415..d12ee1f222 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java @@ -11,8 +11,8 @@ */ public enum UserInteractorPrefix { /* - * order CANNOT be modified This is because USER_INTERACTOR applies to every interactor, but - * still I thought it'd be useful to document there's no prefix. + * Implementations that are none of the following have no dedicated prefix, + * the USER_INTERACTOR value stands for these implementations. */ SLASH_COMMAND(SlashCommand.class, "s-"), @@ -32,7 +32,7 @@ public enum UserInteractorPrefix { /** * The prefix for the command * - * @return + * @return the command's prefix */ public String getPrefix() { return prefix; @@ -42,7 +42,7 @@ public String getPrefix() { /** * The class type that should receive the prefix * - * @return + * @return a {@link Class} instance of the type */ public Class getClassType() { return classType; @@ -51,11 +51,12 @@ public Class getClassType() { /** * Checks what enum value the given instance collaborates to. *

- * As example, + * 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 prefixed {@link String} + * @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(); @@ -74,15 +75,16 @@ public static String getPrefixedNameFromInstance(fina /** * Checks what enum value the given instance collaborates to. *

- * As example, + * 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 + * @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) { + final String name) { for (UserInteractorPrefix value : values()) { Class valueClassType = value.getClassType(); @@ -100,4 +102,4 @@ public static String getPrefixedNameFromClass(final C public String toString() { return "UserInteractorPrefix{" + "prefix='" + prefix + '\'' + '}'; } -} +} \ No newline at end of file From aa4e098b2d7f6fc0292c16bc4fc40b37a55c92e0 Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 29 Sep 2022 09:51:59 +0200 Subject: [PATCH 22/32] Minor changes to command reloading Order has been modified, the BotCore now only gets created after JDA is ready. Also removed the registerReloadCommand method from BotCore, the reload command has been removed, so that method served no purpose. --- .../org/togetherjava/tjbot/Application.java | 6 ++-- .../tjbot/commands/system/BotCore.java | 29 +------------------ 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index 265493deeb..2f10bd2404 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -82,10 +82,12 @@ public static void runBot(Config config) { .enableIntents(GatewayIntent.GUILD_MEMBERS) .build(); + jda.awaitReady(); + + BotCore core = new BotCore(jda, database, config); jda.addEventListener(core); CommandReloading.reloadCommands(jda, core); - jda.awaitReady(); // We fire the event manually, since the core might be added too late to receive the // actual event fired from JDA @@ -116,4 +118,4 @@ private static void onUncaughtException(Thread failingThread, Throwable failure) logger.error("Unknown error in thread {}.", failingThread.getName(), failure); } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 1b5d7759e1..8996f97554 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -12,7 +12,6 @@ import net.dv8tion.jda.api.events.message.MessageUpdateEvent; import net.dv8tion.jda.api.exceptions.ErrorHandler; import net.dv8tion.jda.api.hooks.ListenerAdapter; -import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.Unmodifiable; @@ -51,7 +50,6 @@ */ public final class BotCore extends ListenerAdapter implements CommandProvider { private static final Logger logger = LoggerFactory.getLogger(BotCore.class); - private static final String RELOAD_COMMAND = "reload"; private static final ExecutorService COMMAND_SERVICE = Executors.newCachedThreadPool(); private static final ScheduledExecutorService ROUTINE_SERVICE = Executors.newScheduledThreadPool(5); @@ -201,14 +199,6 @@ public void onReady(JDA jda) { return; } - // Register reload on all guilds - logger.debug("JDA is ready, registering reload command"); - jda.getGuildCache() - .forEach(guild -> COMMAND_SERVICE.execute(() -> registerReloadCommand(guild))); - // NOTE We do not have to wait for reload to complete for the command system to be ready - // itself - logger.debug("Bot core is now ready"); - scheduleRoutines(jda); } @@ -285,23 +275,6 @@ public void onSelectMenuInteraction(SelectMenuInteractionEvent event) { .execute(() -> forwardComponentCommand(event, UserInteractor::onSelectionMenu)); } - private void registerReloadCommand(Guild guild) { - guild.retrieveCommands().queue(commands -> { - // Has it been registered already? - if (commands.stream().map(Command::getName).anyMatch(RELOAD_COMMAND::equals)) { - logger.debug("Command '{}' has already been registered for guild '{}'", - RELOAD_COMMAND, guild.getName()); - return; - } - - logger.debug("Register '{}' for guild '{}'", RELOAD_COMMAND, guild.getName()); - SlashCommand reloadCommand = requireSlashCommand(RELOAD_COMMAND); - guild.upsertCommand(reloadCommand.getData()) - .queue(command -> logger.debug("Registered '{}' for guild '{}'", RELOAD_COMMAND, - guild.getName())); - }, ex -> handleRegisterErrors(ex, guild)); - } - /** * Forwards the given component event to the associated user interactor. *

@@ -427,4 +400,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} +} \ No newline at end of file From c4749b41d34b500920e68799436e535eabfcc4da Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 29 Sep 2022 12:03:59 +0200 Subject: [PATCH 23/32] Removed onReady listener from BotCore The BotCore gets after JDA is ready, because JDA is ready now, routines can be started on construction of BotCore, and commands have access to JDA's caches So now the BotCore constructor is now responsible for reloading commands and scheduling routines --- .../org/togetherjava/tjbot/Application.java | 4 ---- .../tjbot/commands/system/BotCore.java | 21 ++++++------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index 2f10bd2404..a771c5be93 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -87,11 +87,7 @@ public static void runBot(Config config) { BotCore core = new BotCore(jda, database, config); jda.addEventListener(core); - CommandReloading.reloadCommands(jda, core); - // We fire the event manually, since the core might be added too late to receive the - // actual event fired from JDA - core.onReady(jda); logger.info("Bot is ready"); } catch (LoginException e) { logger.error("Failed to login", e); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 8996f97554..4721897a25 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -17,6 +17,7 @@ import org.jetbrains.annotations.Unmodifiable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.CommandReloading; import org.togetherjava.tjbot.commands.*; import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdParser; @@ -118,6 +119,10 @@ public BotCore(JDA jda, Database database, Config config) { if (logger.isInfoEnabled()) { logger.info("Available user interactors: {}", interactors); } + + + CommandReloading.reloadCommands(jda, this); + scheduleRoutines(jda); } /** @@ -188,21 +193,7 @@ public List getInteractorsWithName(final String name) { } - /** - * Trigger once JDA is ready. Subsequent calls are ignored. - * - * @param jda the JDA instance to work with - */ - public void onReady(JDA jda) { - if (!receivedOnReady.compareAndSet(false, true)) { - // Ensures that we only enter the event once - return; - } - - scheduleRoutines(jda); - } - - private void scheduleRoutines(JDA jda) { + public void scheduleRoutines(JDA jda) { routines.forEach(routine -> { Runnable command = () -> { String routineName = routine.getClass().getSimpleName(); From bebccc5cbf89a6e94603e423e44acc118b66dc24 Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 29 Sep 2022 13:17:18 +0200 Subject: [PATCH 24/32] Improved documentation surrounding prefixed and unprefixed names For all the methods that expect a prefix name, the variable has been renamed from name to prefixedName to clarify what it means. Documentation has also been improved across the board. --- .../togetherjava/tjbot/CommandReloading.java | 21 +++++---- .../tjbot/commands/UserInteractor.java | 6 ++- .../tjbot/commands/system/BotCore.java | 46 +++---------------- .../commands/system/CommandProvider.java | 8 ++-- 4 files changed, 26 insertions(+), 55 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java index 78bd5a2d05..866382895d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java @@ -33,12 +33,13 @@ private CommandReloading() { * @param jda the JDA to update commands on * @param commandProvider the {@link CommandProvider} to grab commands from */ - public static void reloadCommands(JDA jda, CommandProvider commandProvider) { + public static void reloadCommands(final JDA jda, final CommandProvider commandProvider) { logger.info("Reloading commands..."); - List actions = Collections.synchronizedList(new ArrayList<>()); + // 110 is based on the fact you can have 100 slash commands, and 20 context commands max. + List actions = Collections.synchronizedList(new ArrayList<>(120)); // Reload global commands - actions.add(updateCommandsIf(command -> command.getVisibility() == CommandVisibility.GLOBAL, + actions.add(updateCommandsIf(command -> CommandVisibility.GLOBAL == command.getVisibility(), getGlobalUpdateAction(jda), commandProvider)); // Reload guild commands (potentially many guilds) @@ -47,7 +48,7 @@ public static void reloadCommands(JDA jda, CommandProvider commandProvider) { // list. However, correctly reducing RestActions in a stream is not trivial. getGuildUpdateActions(jda) .map(updateAction -> updateCommandsIf( - command -> command.getVisibility() == CommandVisibility.GUILD, updateAction, + command -> CommandVisibility.GUILD == command.getVisibility(), updateAction, commandProvider)) .forEach(actions::add); logger.debug("Reloading commands over {} action-upstreams", actions.size()); @@ -56,6 +57,8 @@ public static void reloadCommands(JDA jda, CommandProvider commandProvider) { RestAction.allOf(actions).queue(a -> logger.debug("Commands successfully reloaded!")); } + + /** * Updates all commands given by the command provider which pass the given filter by pushing * through the given action upstream. @@ -66,8 +69,8 @@ public static void reloadCommands(JDA jda, CommandProvider commandProvider) { */ @Contract("_, _, _ -> param2") private static CommandListUpdateAction updateCommandsIf( - Predicate commandFilter, CommandListUpdateAction updateAction, - CommandProvider commandProvider) { + final Predicate commandFilter, final CommandListUpdateAction updateAction, + final CommandProvider commandProvider) { commandProvider.getInteractors() .stream() .filter(BotCommand.class::isInstance) @@ -79,12 +82,12 @@ private static CommandListUpdateAction updateCommandsIf( return updateAction; } - private static CommandListUpdateAction getGlobalUpdateAction(JDA jda) { + private static CommandListUpdateAction getGlobalUpdateAction(final JDA jda) { return jda.updateCommands(); } - private static Stream getGuildUpdateActions(JDA jda) { + private static Stream getGuildUpdateActions(final JDA jda) { return jda.getGuildCache().stream().map(Guild::updateCommands); } -} +} \ No newline at end of file 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 5f761103d3..8a9b16fee7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -13,7 +13,9 @@ * Represents a feature that can interact with users. The most used implementation is * {@link SlashCommand}, {@link UserContextCommand} and {@link MessageContextCommand}. *

- * An interactor must have a unique name and can react to button clicks and selection menu actions. + * 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} */ public interface UserInteractor extends Feature { @@ -88,4 +90,4 @@ public interface UserInteractor extends Feature { * @param generator the provided component id generator */ void acceptComponentIdGenerator(ComponentIdGenerator generator); -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 4721897a25..f6188aed20 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -1,19 +1,14 @@ package org.togetherjava.tjbot.commands.system; import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Channel; -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageUpdateEvent; -import net.dv8tion.jda.api.exceptions.ErrorHandler; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; -import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.Unmodifiable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,7 +25,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -55,12 +49,11 @@ public final class BotCore extends ListenerAdapter implements CommandProvider { private static final ScheduledExecutorService ROUTINE_SERVICE = Executors.newScheduledThreadPool(5); private final Config config; - private final Map nameToInteractor; + private final Map prefixedNameToInteractor; private final List routines; private final ComponentIdParser componentIdParser; private final ComponentIdStore componentIdStore; private final Map channelNameToMessageReceiver = new HashMap<>(); - private final AtomicBoolean receivedOnReady = new AtomicBoolean(false); /** * Creates a new command system which uses the given database to allow commands to persist data. @@ -95,7 +88,7 @@ public BotCore(JDA jda, Database database, Config config) { .toList(); // User Interactors (e.g. slash commands) - nameToInteractor = features.stream() + prefixedNameToInteractor = features.stream() .filter(UserInteractor.class::isInstance) .map(UserInteractor.class::cast) .filter(validateInteractorPredicate()) @@ -161,13 +154,13 @@ private static Predicate validateInteractorPredicate() { @Override @Unmodifiable public Collection getInteractors() { - return nameToInteractor.values(); + return prefixedNameToInteractor.values(); } @Override - public Optional getInteractor(final String name) { + public Optional getInteractor(final String prefixedName) { - return Optional.ofNullable(nameToInteractor.get(name)); + return Optional.ofNullable(prefixedNameToInteractor.get(prefixedName)); } @Override @@ -176,7 +169,7 @@ public Optional getInteractor(final String name, Objects.requireNonNull(type, "The given type cannot be null"); String prefix = UserInteractorPrefix.getPrefixedNameFromClass(type, name); - return Optional.ofNullable(nameToInteractor.get(prefix + name)) + return Optional.ofNullable(prefixedNameToInteractor.get(prefix + name)) .filter(type::isInstance) .map(type::cast); } @@ -335,33 +328,6 @@ private UserInteractor requireUserInteractor(final String name) { return getInteractor(name).orElseThrow(); } - private void handleRegisterErrors(Throwable ex, Guild guild) { - new ErrorHandler().handle(ErrorResponse.MISSING_ACCESS, errorResponse -> { - // Find a channel that we have permissions to write to - // NOTE Unfortunately, there is no better accurate way to find a proper channel - // where we can report the setup problems other than simply iterating all of them. - Optional channelToReportTo = guild.getTextChannelCache() - .stream() - .filter(channel -> guild.getPublicRole() - .hasPermission(channel, Permission.MESSAGE_SEND)) - .findAny(); - - // Report the problem to the guild - channelToReportTo.ifPresent(textChannel -> textChannel - .sendMessage("I need the commands scope, please invite me correctly." - + " You can join '%s' or visit '%s' for more info, I will leave your guild now." - .formatted(config.getDiscordGuildInvite(), config.getProjectWebsite())) - .queue()); - - guild.leave().queue(); - - String unableToReportText = channelToReportTo.isPresent() ? "" - : " Did not find any public text channel to report the issue to, unable to inform the guild."; - logger.warn( - "Guild '{}' does not have the required command scope, unable to register, leaving it.{}", - guild.getName(), unableToReportText, ex); - }).accept(ex); - } @SuppressWarnings("EmptyMethod") private static void onComponentIdRemoved(ComponentId componentId) { diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index 96315d78b8..35fd390eb2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -33,15 +33,15 @@ default Collection getCommands() { /** * Gets the interactor registered under the given name, if any. *

- * This command excepts the name with the prefix of the command, if you prefer using a + * This command excepts the name to include the prefix already, if you prefer using a * {@link Class} instance of the command/type instead, use {@link #getInteractor(String, Class)} * instead.- * - * @param name the name of the command (including its prefix, see + * @param prefixedName the name of the command (including its prefix, see * {@link org.togetherjava.tjbot.commands.UserInteractorPrefix UserInteractorPrefix} * @return the interactor registered under this name, if any */ - Optional getInteractor(String name); + Optional getInteractor(String prefixedName); /** * Gets the interactor registered under the given name, if any. @@ -67,4 +67,4 @@ default Collection getCommands() { * @return a list of commands with that name, can be empty if none match */ List getInteractorsWithName(final String name); -} +} \ No newline at end of file From 41a5009adf67a845a602d0a928252c60c735c7f9 Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 29 Sep 2022 13:19:05 +0200 Subject: [PATCH 25/32] spotless --- .../org/togetherjava/tjbot/Application.java | 2 +- .../togetherjava/tjbot/CommandReloading.java | 6 ++--- .../tjbot/commands/UserInteractor.java | 8 +++---- .../tjbot/commands/UserInteractorPrefix.java | 22 +++++++++---------- .../tjbot/commands/system/BotCore.java | 2 +- .../commands/system/CommandProvider.java | 2 +- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index a771c5be93..916a71568b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -114,4 +114,4 @@ private static void onUncaughtException(Thread failingThread, Throwable failure) logger.error("Unknown error in thread {}.", failingThread.getName(), failure); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java index 866382895d..b3c6adb4e8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java @@ -69,8 +69,8 @@ public static void reloadCommands(final JDA jda, final CommandProvider commandPr */ @Contract("_, _, _ -> param2") private static CommandListUpdateAction updateCommandsIf( - final Predicate commandFilter, final CommandListUpdateAction updateAction, - final CommandProvider commandProvider) { + final Predicate commandFilter, + final CommandListUpdateAction updateAction, final CommandProvider commandProvider) { commandProvider.getInteractors() .stream() .filter(BotCommand.class::isInstance) @@ -90,4 +90,4 @@ private static Stream getGuildUpdateActions(final JDA j return jda.getGuildCache().stream().map(Guild::updateCommands); } -} \ No newline at end of file +} 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 8a9b16fee7..65311be4bc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -13,9 +13,9 @@ * Represents a feature that can interact with users. The most used implementation is * {@link SlashCommand}, {@link UserContextCommand} and {@link MessageContextCommand}. *

- * 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} + * 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} */ public interface UserInteractor extends Feature { @@ -90,4 +90,4 @@ public interface UserInteractor extends Feature { * @param generator the provided component id generator */ void acceptComponentIdGenerator(ComponentIdGenerator generator); -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java index d12ee1f222..3f09e6f427 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java @@ -11,8 +11,8 @@ */ public enum UserInteractorPrefix { /* - * Implementations that are none of the following have no dedicated prefix, - * the USER_INTERACTOR value stands for these implementations. + * Implementations that are none of the following have no dedicated prefix, the USER_INTERACTOR + * value stands for these implementations. */ SLASH_COMMAND(SlashCommand.class, "s-"), @@ -51,11 +51,11 @@ public Class getClassType() { /** * 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". + * 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 + * @param the type of the instance * @return the interactor's name, with its prefix */ public static String getPrefixedNameFromInstance(final T instance) { @@ -75,16 +75,16 @@ public static String getPrefixedNameFromInstance(fina /** * 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". + * 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 + * @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) { + final String name) { for (UserInteractorPrefix value : values()) { Class valueClassType = value.getClassType(); @@ -102,4 +102,4 @@ public static String getPrefixedNameFromClass(final C public String toString() { return "UserInteractorPrefix{" + "prefix='" + prefix + '\'' + '}'; } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index f6188aed20..e6a947ae5e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -357,4 +357,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index 35fd390eb2..1e71a914f3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -67,4 +67,4 @@ default Collection getCommands() { * @return a list of commands with that name, can be empty if none match */ List getInteractorsWithName(final String name); -} \ No newline at end of file +} From 775a0b25e277addd88fb2d71292832f3ba1ca169 Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 29 Sep 2022 13:28:42 +0200 Subject: [PATCH 26/32] Added documentation to scheduleRoutines --- .../org/togetherjava/tjbot/commands/system/BotCore.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index e6a947ae5e..aebd06c2c4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -186,6 +186,13 @@ public List getInteractorsWithName(final String name) { } + /** + * Schedules the registered routines. + *

+ * This needs a ready {@link JDA} instance. + * + * @param jda a ready JDA instance + */ public void scheduleRoutines(JDA jda) { routines.forEach(routine -> { Runnable command = () -> { @@ -357,4 +364,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} +} \ No newline at end of file From c52a4adad7e562510073a2c4fc056b32a63f9a20 Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 29 Sep 2022 13:29:58 +0200 Subject: [PATCH 27/32] spotless --- .../java/org/togetherjava/tjbot/commands/system/BotCore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index aebd06c2c4..cb8a466bc0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -364,4 +364,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} \ No newline at end of file +} From edc1158ad4f0d558b0eb71c722bca165b18e9a7d Mon Sep 17 00:00:00 2001 From: Tijs Date: Thu, 29 Sep 2022 13:54:42 +0200 Subject: [PATCH 28/32] Fixed bugs System has been tested now and should not contain any bugs! --- .../togetherjava/tjbot/commands/UserInteractorPrefix.java | 8 +++----- .../org/togetherjava/tjbot/commands/system/BotCore.java | 6 +++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java index 3f09e6f427..e32018a517 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java @@ -11,14 +11,12 @@ */ public enum UserInteractorPrefix { /* - * Implementations that are none of the following have no dedicated prefix, the USER_INTERACTOR - * value stands for these implementations. + * Implementations that are none of the following have no dedicated prefix */ SLASH_COMMAND(SlashCommand.class, "s-"), MESSAGE_CONTEXT_COMMAND(MessageContextCommand.class, "mc-"), - USER_CONTEXT_COMMAND(UserContextCommand.class, "uc-"), - USER_INTERACTOR(UserInteractor.class, ""); + USER_CONTEXT_COMMAND(UserContextCommand.class, "uc-"); private final Class classType; @@ -102,4 +100,4 @@ public static String getPrefixedNameFromClass(final C public String toString() { return "UserInteractorPrefix{" + "prefix='" + prefix + '\'' + '}'; } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index cb8a466bc0..4a8d7d4cda 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -168,8 +168,8 @@ public Optional getInteractor(final String name, final Class type) { Objects.requireNonNull(type, "The given type cannot be null"); - String prefix = UserInteractorPrefix.getPrefixedNameFromClass(type, name); - return Optional.ofNullable(prefixedNameToInteractor.get(prefix + name)) + String prefixedName = UserInteractorPrefix.getPrefixedNameFromClass(type, name); + return Optional.ofNullable(prefixedNameToInteractor.get(prefixedName)) .filter(type::isInstance) .map(type::cast); } @@ -364,4 +364,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} +} \ No newline at end of file From d8a1fdbc57f141eaa74c963c3b58b1593989b84c Mon Sep 17 00:00:00 2001 From: Tais993 Date: Thu, 29 Sep 2022 21:30:12 +0200 Subject: [PATCH 29/32] spotless runs automatically yes mhmhhm --- .../org/togetherjava/tjbot/commands/UserInteractorPrefix.java | 2 +- .../java/org/togetherjava/tjbot/commands/system/BotCore.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java index e32018a517..c9285cd77a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java @@ -100,4 +100,4 @@ public static String getPrefixedNameFromClass(final C public String toString() { return "UserInteractorPrefix{" + "prefix='" + prefix + '\'' + '}'; } -} \ No newline at end of file +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 4a8d7d4cda..8c0d5cb0b4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -364,4 +364,4 @@ private interface TriConsumer { */ void accept(A first, B second, C third); } -} \ No newline at end of file +} From 8a9326531fc0ed75ef98b26d4b1356257fe1c7c9 Mon Sep 17 00:00:00 2001 From: Tijs Date: Fri, 30 Sep 2022 12:14:07 +0200 Subject: [PATCH 30/32] CR Documentation changes Added MAX_COMMAND_COUNT constant to CommandReloading And our beloved friend, spotless :p --- .../org/togetherjava/tjbot/CommandReloading.java | 14 ++++++++++++-- .../tjbot/commands/system/CommandProvider.java | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java index b3c6adb4e8..da13ab1437 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java @@ -23,6 +23,16 @@ public class CommandReloading { private static final Logger logger = LoggerFactory.getLogger(CommandReloading.class); + /** + * According to Discord + * docs, there can be a maximum of 110 commands, 100 slash, and 5 context each. + *

+ * Because this bot most of the time operates in only 1 server, chances of exceeding this limit + * is low. + */ + public static final int MAX_COMMAND_COUNT = 110; + private CommandReloading() { throw new UnsupportedOperationException("Utility class"); } @@ -35,8 +45,8 @@ private CommandReloading() { */ public static void reloadCommands(final JDA jda, final CommandProvider commandProvider) { logger.info("Reloading commands..."); - // 110 is based on the fact you can have 100 slash commands, and 20 context commands max. - List actions = Collections.synchronizedList(new ArrayList<>(120)); + List actions = + Collections.synchronizedList(new ArrayList<>(MAX_COMMAND_COUNT)); // Reload global commands actions.add(updateCommandsIf(command -> CommandVisibility.GLOBAL == command.getVisibility(), diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java index 1e71a914f3..639b7dbf6d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandProvider.java @@ -33,9 +33,9 @@ default Collection getCommands() { /** * Gets the interactor registered under the given name, if any. *

- * This command excepts the name to include the prefix already, if you prefer using a + * This command expects the name to include the prefix already, if you prefer using a * {@link Class} instance of the command/type instead, use {@link #getInteractor(String, Class)} - * instead.- + * instead. * * @param prefixedName the name of the command (including its prefix, see * {@link org.togetherjava.tjbot.commands.UserInteractorPrefix UserInteractorPrefix} @@ -48,7 +48,7 @@ default Collection getCommands() { *

* Unlike {@link #getInteractor(String)}, this command expects the name, without the prefix (see * {@link org.togetherjava.tjbot.commands.UserInteractorPrefix UserInteractorPrefix}). Instead, - * this command excepts a {@link Class} instance of the commands class. This can be + * this command expects a {@link Class} instance of the commands class. This can be * {@link org.togetherjava.tjbot.commands.SlashCommand SlashCommand}, or even as specific as * {@link org.togetherjava.tjbot.commands.basic.PingCommand PingCommand}. * From f71e4fe54f49a823d646c696b0d8d025d96956f1 Mon Sep 17 00:00:00 2001 From: Tais993 <49957334+Tais993@users.noreply.github.com> Date: Fri, 30 Sep 2022 04:53:37 -0700 Subject: [PATCH 31/32] Removed useless extra lines (CR) --- .../src/main/java/org/togetherjava/tjbot/Application.java | 2 -- .../src/main/java/org/togetherjava/tjbot/CommandReloading.java | 3 --- .../org/togetherjava/tjbot/commands/UserInteractorPrefix.java | 2 -- 3 files changed, 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index 916a71568b..97d038c5c8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -83,8 +83,6 @@ public static void runBot(Config config) { .build(); jda.awaitReady(); - - BotCore core = new BotCore(jda, database, config); jda.addEventListener(core); diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java index da13ab1437..a4941c39c7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java @@ -66,9 +66,6 @@ public static void reloadCommands(final JDA jda, final CommandProvider commandPr // Send message when all are done RestAction.allOf(actions).queue(a -> logger.debug("Commands successfully reloaded!")); } - - - /** * Updates all commands given by the command provider which pass the given filter by pushing * through the given action upstream. diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java index c9285cd77a..f489a34106 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java @@ -35,8 +35,6 @@ public enum UserInteractorPrefix { public String getPrefix() { return prefix; } - - /** * The class type that should receive the prefix * From befa3a830e05a327bdb5645dcb6428a21177ffd2 Mon Sep 17 00:00:00 2001 From: Tais993 <49957334+Tais993@users.noreply.github.com> Date: Fri, 30 Sep 2022 05:02:02 -0700 Subject: [PATCH 32/32] Added missing empty lines between methods Spotless really is useful, huh? Co-authored-by: Tanish Azad <73871477+Taz03@users.noreply.github.com> --- .../src/main/java/org/togetherjava/tjbot/CommandReloading.java | 1 + .../main/java/org/togetherjava/tjbot/commands/BotCommand.java | 1 - .../org/togetherjava/tjbot/commands/UserInteractorPrefix.java | 1 + 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java index a4941c39c7..54108595ae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java +++ b/application/src/main/java/org/togetherjava/tjbot/CommandReloading.java @@ -66,6 +66,7 @@ public static void reloadCommands(final JDA jda, final CommandProvider commandPr // Send message when all are done RestAction.allOf(actions).queue(a -> logger.debug("Commands successfully reloaded!")); } + /** * Updates all commands given by the command provider which pass the given filter by pushing * through the given action upstream. 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 fa7afe249c..0795c12d76 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java @@ -50,7 +50,6 @@ public interface BotCommand extends UserInteractor { */ Command.Type getType(); - /** * Gets the visibility of this command. *

diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java index f489a34106..e4537853e7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java @@ -35,6 +35,7 @@ public enum UserInteractorPrefix { public String getPrefix() { return prefix; } + /** * The class type that should receive the prefix *