diff --git a/src/main/java/org/togetherjava/tjbot/commands/AbstractCommand.java b/src/main/java/org/togetherjava/tjbot/commands/AbstractCommand.java new file mode 100644 index 0000000000..4a8c9970ea --- /dev/null +++ b/src/main/java/org/togetherjava/tjbot/commands/AbstractCommand.java @@ -0,0 +1,53 @@ +package org.togetherjava.tjbot.commands; + +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.requests.restaction.CommandCreateAction; + +/** + * The class all commands have to abstract. + * Look at {@link org.togetherjava.tjbot.commands.example.ExampleCommand} for an example. + * A second example will come for sub-commands. + */ +public abstract class AbstractCommand { + + /** + * The commands name. + * @return + * The commands name as a {@link String} + */ + public abstract String getCommandName(); + + /** + * The commands description. + * @return + * The commands description as a {@link String} + */ + public abstract String getDescription(); + + /** + * Whenever the command is only for guilds, optional method. + * @return + * Whenever the command is only for guilds as a {@link Boolean} + */ + public boolean isGuildOnly() { + return false; + } + + /** + * This command will add all the options to the slash-command. + * Look at {@link org.togetherjava.tjbot.commands.example.ExampleCommand#addOptions(CommandCreateAction)} for an example. + * @param commandCreateAction + * The {@link CommandCreateAction} where the options need to be added. + * @return + * The changed {@link CommandCreateAction} + */ + public abstract CommandCreateAction addOptions(CommandCreateAction commandCreateAction); + + /** + * 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} + */ + public abstract void execute(SlashCommandEvent event); +} diff --git a/src/main/java/org/togetherjava/tjbot/commands/example/ExampleCommand.java b/src/main/java/org/togetherjava/tjbot/commands/example/ExampleCommand.java new file mode 100644 index 0000000000..4186dd77f0 --- /dev/null +++ b/src/main/java/org/togetherjava/tjbot/commands/example/ExampleCommand.java @@ -0,0 +1,83 @@ +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.Command; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import net.dv8tion.jda.api.requests.restaction.CommandCreateAction; +import org.togetherjava.tjbot.commands.AbstractCommand; + +/** + * Example command + */ +public class ExampleCommand extends AbstractCommand { + + /** + * The command name is example, so it returns example. + * @return + * "Example" as a {@link String} + */ + @Override + public String getCommandName() { + return "example"; + } + + /** + * The description + * @return + * "Example command! Allows you to spam ping someone!" as a {@link String} + */ + @Override + public String getDescription() { + return "Example command! Allows you to spam ping someone!"; + } + + /** + * You can make commands guild only by overriding this method. + * You can only ping people in a guild, so this command is guild only. + * @return + * "true" as a {@link Boolean} + */ + @Override + public boolean isGuildOnly() { + return true; + } + + /** + * Adds the "User" and "Amount of pings" option. + * This is for the GUI, so the user sees what options it may give. + * @param commandCreateAction + * The thing we will add our options to! + * @return + * A {@link CommandCreateAction} with the required options for this command + */ + @Override + public CommandCreateAction addOptions(CommandCreateAction commandCreateAction) { + return commandCreateAction.addOptions( + new OptionData(OptionType.USER, "User", "User that will be pinged!", true), + new OptionData(OptionType.STRING, "Amount of pings", "Amount of times the user will be pinged, default 1").addChoices( + new Command.Choice("Once", "1"), + new Command.Choice("Twice", "2") + ).setRequired(true) + ); + } + + /** + * The execute method. + * Here it loads the options the users made. + * After that it pings the user ... times. + * And it calls the person who ran the command out, spammer they are! + * @param event + * A {@link SlashCommandEvent} to respond to. + */ + @Override + public void execute(SlashCommandEvent event) { + + User userToMention = event.getOption("User").getAsUser(); + int timesToPing = Integer.parseInt(event.getOption("Amount of pings").getAsString()); + + event.reply("It'll only ping once, you spammer!").setEphemeral(true).queue(); + event.getChannel().sendMessage(userToMention.getAsMention().repeat(timesToPing)).queue(); + } +}