From 5ed98e8b631411ed700a499082a7c888b28b0e4f Mon Sep 17 00:00:00 2001 From: Tais993 <49957334+Tais993@users.noreply.github.com> Date: Sun, 12 Sep 2021 12:14:17 +0200 Subject: [PATCH 1/3] Add command-system Added Command and AbstractCommand for commands to extend. Added CommandExample and SubCommandExample as examples/guides for people/animals that want to create a command. Added the CommandHandler that actually handles the command Added the ReloadCommand so you can reload commands Added the CommandHandler to the EventListeners in Application.java --- .../org/togetherjava/tjbot/Application.java | 2 + .../togetherjava/tjbot/commands/Command.java | 167 ++++++++++++++++++ .../tjbot/commands/CommandHandler.java | 101 +++++++++++ .../tjbot/commands/ReloadCommand.java | 151 ++++++++++++++++ .../commands/example/AbstractCommand.java | 40 +++++ .../commands/example/CommandExample.java | 105 +++++++++++ .../commands/example/SubCommandExample.java | 99 +++++++++++ 7 files changed, 665 insertions(+) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/Command.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/CommandHandler.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/ReloadCommand.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/example/AbstractCommand.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index 959505305c..2d6a32daac 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -4,6 +4,7 @@ import net.dv8tion.jda.api.JDABuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.commands.CommandHandler; import org.togetherjava.tjbot.db.Database; import javax.security.auth.login.LoginException; @@ -53,6 +54,7 @@ public static void runBot(String token, Path databasePath) { JDA jda = JDABuilder.createDefault(token) .addEventListeners(new PingPongListener()) .addEventListeners(new DatabaseListener(database)) + .addEventListeners(new CommandHandler()) .build(); jda.awaitReady(); logger.info("Bot is ready"); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Command.java b/application/src/main/java/org/togetherjava/tjbot/commands/Command.java new file mode 100644 index 0000000000..08371a31b7 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Command.java @@ -0,0 +1,167 @@ +package org.togetherjava.tjbot.commands; + +import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; +import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.interactions.components.Component; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.example.CommandExample; +import org.togetherjava.tjbot.commands.example.SubCommandExample; + +import java.time.Instant; +import java.util.Collection; +import java.util.List; + +/** + * The class all commands have to abstract.
+ * Look at {@link CommandExample} for an example.
+ * Or look at {@link SubCommandExample} for an example with subcommands. + * + *
+ * All commands that implement this interface need to be added command to the + * {@link CommandHandler} class! + */ +public interface Command { + + /** + *

+ * The command's name. + * + *

+ * Requirements! + *

+ * + * @return The commands name as a {@link String} + */ + @NotNull + String getCommandName(); + + /** + *

+ * The command's description. + * + *

+ * Requirements! + *

+ * + * @return The command's description as a {@link String} + */ + @NotNull + String getDescription(); + + /** + * Whenever the command is only for guilds, optional method. + * + * @return Whenever the command is only for guilds as a {@link Boolean} + */ + default boolean isGuildOnly() { + return false; + } + + /** + * This command will add all the options to the slash-command.
+ * Look at {@link CommandExample#addOptions(CommandData)} for an example. + * + * @param commandData The {@link CommandData} where the options need to be added. + * @return The changed {@link CommandData} + */ + default @NotNull CommandData addOptions(@NotNull CommandData commandData) { + return commandData; + } + + /** + * The execute method for when the command gets executed.
+ * Check JDA's + * slash-commands Wiki article for more information. + * + * @param event The relating {@link SlashCommandEvent} + */ + default void onSlashCommand(SlashCommandEvent event) {} + + /** + * The execute method for when a button related to this command gets clicked.
+ * Check JDA's + * buttons Wiki article for more information. + * + * @param event The relating {@link ButtonClickEvent} + * @param idArgs All the arguments stored in the ID ({@link Component#getId() + * Component#getId()}) have been converted to a {@link List} The commands name and + * current time in MS have been removed from this, example: + * "examplecommand-7272727272-userId", this will be converted to a {@link List} + * containing "userId", the rest has been removed. + */ + default void onButtonClick(ButtonClickEvent event, List idArgs) {} + + /** + * The execute method for when a selection menu related to this command gets clicked.
+ * JDA has no dedicated guide for selection menu's, they work the same as buttons.
+ * Check JDA's + * buttons Wiki article for more information. + * + * @param event The relating {@link ButtonClickEvent} + * @param idArgs All the arguments stored in the ID ({@link Component#getId() + * Component#getId()}) have been converted to a {@link List} The commands name and + * current time in MS have been removed from this, example: + * "examplecommand-7272727272-userId", this will be converted to a {@link List} + * containing "userId", the rest has been removed. + */ + default void onSelectionMenu(SelectionMenuEvent event, List idArgs) {} + + + /** + * Generates a {@link net.dv8tion.jda.api.interactions.components.Component Component} ID based + * on the command name, current time and given arguments,
+ * {@link net.dv8tion.jda.api.interactions.components.Component Component} examples are + * {@link net.dv8tion.jda.api.interactions.components.Button Buttons} and or + * {@link net.dv8tion.jda.api.interactions.components.selections.SelectionMenu SelectionMenu's} + * + *
+ * An ID can be maximum of + * {@link net.dv8tion.jda.api.interactions.components.Button#ID_MAX_LENGTH} chars.
+ * Between every argument an `{@code -}` is added, and by default the command name + current + * time in MS gets added. + * + * @param args A {@link String} array of the arguments + * @return A {@link String} generated ID in the format of + * `{@code commandName-timeInMs-arg1-arg2}` + */ + default @NotNull String generateComponentId(@NotNull String... args) { + StringBuilder stringBuilder = new StringBuilder(getCommandName()); + + stringBuilder.append("-").append(Instant.now().getNano()); + + for (String arg : args) { + stringBuilder.append("-").append(arg); + } + + return stringBuilder.toString(); + } + + /** + * /** Generates a {@link net.dv8tion.jda.api.interactions.components.Component Component} ID + * based on the command name, current time and given arguments,
+ * {@link net.dv8tion.jda.api.interactions.components.Component Component} examples are + * {@link net.dv8tion.jda.api.interactions.components.Button Buttons} and or + * {@link net.dv8tion.jda.api.interactions.components.selections.SelectionMenu SelectionMenu's} + * + *
+ * An ID can be maximum of + * {@link net.dv8tion.jda.api.interactions.components.Button#ID_MAX_LENGTH} chars.
+ * Between every argument an `{@code -}` is added, and by default the command name + current + * time in MS gets added. + * + * @param args A {@link Collection} of {@link String}'s of the arguments + * @return A {@link String} generated ID in the format of + * `{@code commandName-timeInMs-arg1-arg2}` + */ + default @NotNull String generateComponentId(@NotNull Collection args) { + return generateComponentId(args.toArray(new String[] {})); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/CommandHandler.java b/application/src/main/java/org/togetherjava/tjbot/commands/CommandHandler.java new file mode 100644 index 0000000000..8e1b0dc97e --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/CommandHandler.java @@ -0,0 +1,101 @@ +package org.togetherjava.tjbot.commands; + +import net.dv8tion.jda.api.events.ReadyEvent; +import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; +import net.dv8tion.jda.api.events.interaction.GenericComponentInteractionCreateEvent; +import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.stream.Collectors; + +/** + * The command handler + * + * Commands need to be added to the commandList + */ +public class CommandHandler extends ListenerAdapter { + private final static Logger logger = LoggerFactory.getLogger(CommandHandler.class); + + private final List commandList = new ArrayList<>(); + private final Map commandMap; + + public CommandHandler() { + commandList.addAll(List.of(new ReloadCommand(this) + // add your command here + )); + + commandMap = commandList.stream() + .collect(Collectors.toMap(Command::getCommandName, command -> command)); + } + + @Override + public void onReady(@NotNull ReadyEvent event) { + event.getJDA().getGuildCache().forEach(guild -> { + guild.retrieveCommands().queue(commands -> { + boolean hasReloadCommand = + commands.stream().anyMatch(command -> command.getName().equals("reload")); + + if (!hasReloadCommand) { + Command command = commandMap.get("reload"); + guild.upsertCommand(command.addOptions( + new CommandData(command.getCommandName(), command.getDescription()))) + .queue(); + } + }); + }); + } + + @Override + public void onSlashCommand(@NotNull SlashCommandEvent event) { + Command command = commandMap.get(event.getName()); + + if (command != null) { + command.onSlashCommand(event); + } else { + logger.error("Command '{}' doesn't exist?", event.getName()); + } + } + + @Override + public void onButtonClick(@NotNull ButtonClickEvent event) { + getArgumentsAndCommandByEvent(event, (command, args) -> command.onButtonClick(event, args)); + } + + @Override + public void onSelectionMenu(@NotNull SelectionMenuEvent event) { + getArgumentsAndCommandByEvent(event, + (command, args) -> command.onSelectionMenu(event, args)); + } + + /** + * Gets the arguments as a {@link List} and gets the {@link Command} by the event. + * + * @param event a {@link GenericComponentInteractionCreateEvent} + * @param onSucceed a {@link BiConsumer} that takes the command and the arguments + */ + private void getArgumentsAndCommandByEvent(GenericComponentInteractionCreateEvent event, + BiConsumer> onSucceed) { + String[] argsArray = event.getComponentId().split("-"); + Command command = commandMap.get(argsArray[0]); + + if (command != null) { + List args = new ArrayList<>(Arrays.asList(argsArray)); + args = args.subList(2, args.size()); + onSucceed.accept(command, args); + } + } + + public List getCommandList() { + return commandList; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/ReloadCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/ReloadCommand.java new file mode 100644 index 0000000000..8c7e2f9da1 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/ReloadCommand.java @@ -0,0 +1,151 @@ +package org.togetherjava.tjbot.commands; + +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.Event; +import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.interactions.components.ButtonStyle; +import net.dv8tion.jda.api.requests.RestAction; +import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; +import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public record ReloadCommand(CommandHandler commandHandler) implements Command { + + @Override + public @NotNull String getCommandName() { + return "reload"; + } + + @Override + public @NotNull String getDescription() { + return "Uploads all existing slash-commands to Discord so they're fully up-to-date."; + } + + @Override + public boolean isGuildOnly() { + return true; + } + + @Override + public void onSlashCommand(SlashCommandEvent event) { + Member member = event.getMember(); + + if (member.hasPermission(Permission.MANAGE_SERVER)) { + event.reply( + "You sure? You can only reload commands a few times each day, so don't overdo this.") + .addActionRow( + Button.of(ButtonStyle.SUCCESS, generateComponentId(member.getId()), + "Affirmative!"), + Button.of(ButtonStyle.DANGER, generateComponentId(member.getId()), + "Wrong command! Sorry")) + .queue(); + } else { + event.reply("You need the MANAGE_SERVER permission to use this command!") + .setEphemeral(true) + .queue(); + } + } + + @Override + public void onButtonClick(ButtonClickEvent event, List idArgs) { + Member member = event.getMember(); + + if (member.getId().equals(idArgs.get(0))) { + switch (event.getButton().getStyle()) { + case DANGER -> { + event.reply("Next time use the right command!").queue(); + event.getMessage().editMessageComponents().queue(); + } + + case SUCCESS -> { + event.deferReply().queue(); + List commands = commandHandler.getCommandList(); + List restActions; + + // * loads all RestActions from updating the Guild and Global commands + restActions = getGuildCommandUpdateRestActions(event, commands); + restActions.add(getGlobalCommandUpdateRestAction(event, commands)); + + // * Triggers all RestActions, when they're all finished the message gets send. + RestAction.allOf(restActions).queue(updatedCommands -> { + event.getHook() + .editOriginal( + "Commands successfully reloaded! *Global commands can take upto 1 hour to load*") + .queue(); + }); + } + } + } + } + + /** + * Adds commands to {@link net.dv8tion.jda.api.JDA} + * + * @param event A {@link Event} + * @param commands A {@link List} of {@link Command Commands} + * @return newly created {@link CommandListUpdateAction} with Commands + */ + private CommandListUpdateAction getGlobalCommandUpdateRestAction(Event event, + List commands) { + return addCommandsToUpdateAction(event.getJDA().updateCommands(), commands, false); + } + + /** + * Adds commands to all {@link Guild Guilds} + * + * @param event A {@link Event} + * @param commands A {@link List} of {@link Command Commands} + * @return newly created {@link CommandListUpdateAction} with Commands + */ + private List getGuildCommandUpdateRestActions(Event event, + List commands) { + SnowflakeCacheView guildCache = event.getJDA().getGuildCache(); + + List restActions = new ArrayList<>((int) guildCache.size()); + + event.getJDA().getGuildCache().forEach(guild -> { + restActions.add(addCommandsToUpdateAction(guild.updateCommands(), commands, true)); + }); + + return restActions; + } + + /** + * Adds all given commands to the {@link CommandListUpdateAction} if
+ * The {@link Command} AND the {@link CommandListUpdateAction} are from a {@link Guild} + *
+ * or
+ * The {@link Command} AND the {@link CommandListUpdateAction} are NOT from a + * {@link Guild}
+ * + * @param commandListUpdateAction A {@link CommandListUpdateAction} + * @param commands A {@link List} of {@link Command Commands} that need to be added + * @param isGuild Whenever the given {@link CommandListUpdateAction} is from a {@link Guild} or + * not + * @return The updated {@link CommandListUpdateAction} + */ + private CommandListUpdateAction addCommandsToUpdateAction( + CommandListUpdateAction commandListUpdateAction, List commands, + boolean isGuild) { + commands.forEach(command -> { + if (command.isGuildOnly() && isGuild) { + commandListUpdateAction.addCommands(command.addOptions( + new CommandData(command.getCommandName(), command.getDescription()))); + } else if (!command.isGuildOnly() && !isGuild) { + commandListUpdateAction.addCommands(command.addOptions( + new CommandData(command.getCommandName(), command.getDescription()))); + } + }); + + + return commandListUpdateAction; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/example/AbstractCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/example/AbstractCommand.java new file mode 100644 index 0000000000..859e53afd1 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/example/AbstractCommand.java @@ -0,0 +1,40 @@ +package org.togetherjava.tjbot.commands.example; + +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.Command; + +/** + * Makes usage of constructor to store the {@link #commandName}, {@link #description} and the + * {@link #isGuildOnly}
+ * This helps against a tiny bit of duplicated code. + */ +public class AbstractCommand implements Command { + private final String commandName; + private final String description; + private final boolean isGuildOnly; + + public AbstractCommand(String commandName, String description, boolean isGuildOnly) { + this.commandName = commandName; + this.description = description; + this.isGuildOnly = isGuildOnly; + } + + public AbstractCommand(String commandName, String description) { + this(commandName, description, false); + } + + @Override + public @NotNull String getCommandName() { + return commandName; + } + + @Override + public @NotNull String getDescription() { + return description; + } + + @Override + public boolean isGuildOnly() { + return isGuildOnly; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java b/application/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java new file mode 100644 index 0000000000..82be6032c8 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java @@ -0,0 +1,105 @@ +package org.togetherjava.tjbot.commands.example; + +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.interactions.components.ButtonStyle; +import net.dv8tion.jda.api.interactions.components.Component; +import net.dv8tion.jda.api.requests.restaction.CommandCreateAction; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.Command; + +import java.util.List; + +/** + * Example command + * + */ +public class CommandExample extends AbstractCommand { + + /** + * Parses the command name and the description to the super's constructor, both as a + * {@link String}
+ * Also includes the isGuildOnly check, so this is also parsed to the constructor, as a + * {@link Boolean} + */ + public CommandExample() { + super("example", "Example command! Allows you to spam ping someone!", true); + } + + /** + * Adds the "User" and "Amount of pings" option.
+ * This is for the GUI, so the user sees what options it has. + * + * @param commandData The thing we will add our options to! + * @return A {@link CommandCreateAction} with the required options for this command + */ + @Override + public @NotNull CommandData addOptions(@NotNull CommandData commandData) { + return commandData.addOptions( + // * add the first option + new OptionData(OptionType.USER, "user", "User that will be pinged!", true), + // * adds the second option + new OptionData(OptionType.STRING, "times-to-ping", + "Amount of times the user will be pinged, default 1") + .addChoices( + new net.dv8tion.jda.api.interactions.commands.Command.Choice( + "Once", "1"), + new net.dv8tion.jda.api.interactions.commands.Command.Choice( + "Twice", "2")) + .setRequired(true)); + } + + /** + * The execute method!
+ * Here it loads the options the users made.
+ * After that it sends a reply with a {@link Button} asking the user, or they're sure
+ * It generates a component ID using one of the {@link Command} methods, this makes sure that + * the {@link #onButtonClick(ButtonClickEvent, List)} runs. + * + * @param event A {@link SlashCommandEvent} to respond to. + */ + @Override + public void onSlashCommand(SlashCommandEvent event) { + // * gets the option as a user + User userToMention = event.getOption("user").getAsUser(); + int timesToPing = Integer.parseInt(event.getOption("times-to-ping").getAsString()); + + event.reply("You sure you want to spam them, you don't have feelings?") + .addActionRow(Button.of(ButtonStyle.DANGER, + generateComponentId(event.getUser().getId()), "Affirmative!")) + .queue(); + } + + /** + * After we created the button in the {@link #onSlashCommand(SlashCommandEvent)} method, the + * button gets pressed.
+ * This gets handled here, it checks or the right user is pressing and or the right button was + * pressed.
+ * After that it replies that spam pinging is bad! + * + * @param event The relating {@link ButtonClickEvent} + * @param idArgs All the arguments stored in the ID ({@link Component#getId() + * Component#getId()}) have been converted to a {@link List}
+ * The commands name and current time in MS have been removed from this, example: + */ + @Override + public void onButtonClick(ButtonClickEvent event, List idArgs) { + String userId = idArgs.get(0); + if (event.getUser().getId().equals(userId) + && event.getButton().getStyle() == ButtonStyle.DANGER) { + event.reply("No! I'm not going to spam ping! You should behave!") + .setEphemeral(true) + .queue(); + + event.getMessage() + .editMessageComponents(ActionRow.of(event.getButton().asDisabled())) + .queue(); + } + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java b/application/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java new file mode 100644 index 0000000000..8a05eaedaa --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java @@ -0,0 +1,99 @@ +package org.togetherjava.tjbot.commands.example; + +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; +import org.jetbrains.annotations.NotNull; + +/** + * Subcommands example! + * + *

+ * Discord enforces a strong 100 commands limit globally and a 100 commands limit in each guild. By + * making usage of subcommands, the amount of commands can be extended significantly allowing many + * more commands. + * + *

+ * A good example is the ban command, instead of having tempban, permban separate this can become 1 + * command. + * + *
+ * counts as 1 command within Discord
+ *
+ * `{@code ban temp 6d Tijs#0001}` and `{@code ban perm Tijs#0001}` + * + *
+ * instead of
+ * counts as 2 commands within Discord `{@code tempban 6s Tijs#0001}` and + * `{@code permban Tijs#0001}` + * + */ +public class SubCommandExample extends AbstractCommand { + + /** + * Parses the command name and the description to the super's constructor, both as a + * {@link String} + */ + public SubCommandExample() { + super("subcommands-example", + "Subcommands example! Allows you to ping someone again, and something else?"); + } + + /** + * Adds the "ping" and "hello" subcommand. + * + * @param commandData The thing we will add our sub commands to! + * @return A {@link CommandData} with the required options for this command + */ + @Override + public @NotNull CommandData addOptions(@NotNull CommandData commandData) { + return commandData.addSubcommands( + // * adds the subcommand here + new SubcommandData("ping", "Ping an user! Yes you're allowed to spam again!") + // * adds the User option, it's hard to ping nothing, so it's required. + .addOption(OptionType.USER, "user", "User to ping", true), + // * adds another subcommand + new SubcommandData("hello", "Send hello and optionally you can ping an user.") + // * this time it's not required, so no need for that boolean, making it + // shorter. + .addOption(OptionType.USER, "user", "User to ping!")); + } + + /** + * The execute method!
+ * Here it checks what subcommand the user ran.
+ * After that that command gets ran. + * + * @param event A {@link SlashCommandEvent} + */ + @Override + public void onSlashCommand(SlashCommandEvent event) { + + // * get the subcommand + switch (event.getSubcommandName()) { + // * if it's "ping", run the ping command. + case "ping" -> { + User userToPing = event.getOption("user").getAsUser(); + + event.reply(userToPing.getAsMention() + " pinging like this is useless ;)") + .setEphemeral(true) + .queue(); + } + + // * if it's "Hello", run the hello command. + case "hello" -> { + OptionMapping userOption = event.getOption("user"); + + if (userOption == null) { + event.reply("Hello!").queue(); + } else { + User userToPing = userOption.getAsUser(); + event.reply("Hello " + userToPing.getAsMention() + "!").queue(); + } + } + } + } +} From 103989e58a803ce1ffc32d99f919e6a4ed7f9ca2 Mon Sep 17 00:00:00 2001 From: Tais993 <49957334+Tais993@users.noreply.github.com> Date: Sun, 12 Sep 2021 12:20:10 +0200 Subject: [PATCH 2/3] Mention of AbstractClass in the Command interface --- .../src/main/java/org/togetherjava/tjbot/commands/Command.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Command.java b/application/src/main/java/org/togetherjava/tjbot/commands/Command.java index 08371a31b7..d0c0665ff1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Command.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Command.java @@ -15,6 +15,8 @@ /** * The class all commands have to abstract.
+ * Instead of implementing this interface, you can also extend {@link org.togetherjava.tjbot.commands.example.AbstractCommand}. + * * Look at {@link CommandExample} for an example.
* Or look at {@link SubCommandExample} for an example with subcommands. * From 18e2993a46234f942232a839b8b5ad44b177747d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Radosavljevi=C4=87?= Date: Sun, 12 Sep 2021 12:25:36 +0200 Subject: [PATCH 3/3] Run spotlessApply --- .../src/main/java/org/togetherjava/tjbot/commands/Command.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Command.java b/application/src/main/java/org/togetherjava/tjbot/commands/Command.java index d0c0665ff1..8beb9fcbce 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Command.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Command.java @@ -15,7 +15,8 @@ /** * The class all commands have to abstract.
- * Instead of implementing this interface, you can also extend {@link org.togetherjava.tjbot.commands.example.AbstractCommand}. + * Instead of implementing this interface, you can also extend + * {@link org.togetherjava.tjbot.commands.example.AbstractCommand}. * * Look at {@link CommandExample} for an example.
* Or look at {@link SubCommandExample} for an example with subcommands.