diff --git a/src/main/java/org/togetherjava/tjbot/Main.java b/src/main/java/org/togetherjava/tjbot/Main.java index 4fe7095a4d..826e131ba6 100644 --- a/src/main/java/org/togetherjava/tjbot/Main.java +++ b/src/main/java/org/togetherjava/tjbot/Main.java @@ -30,6 +30,7 @@ public static void main(final String[] args) { .addEventListeners(new PingPongListener()) .build(); jda.awaitReady(); + logger.info("Bot is ready"); } catch (LoginException e) { logger.error("Failed to login", e); diff --git a/src/main/java/org/togetherjava/tjbot/commands/Command.java b/src/main/java/org/togetherjava/tjbot/commands/Command.java new file mode 100644 index 0000000000..00bb8b1d54 --- /dev/null +++ b/src/main/java/org/togetherjava/tjbot/commands/Command.java @@ -0,0 +1,93 @@ +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 org.togetherjava.tjbot.commands.example.CommandExample; + +/** + * The class all commands have to abstract. + *
Look at {@link CommandExample} for an example. + *
A second example will come for sub-commands. + * + */ +public interface Command { + + /** + *

The command's name. + * + *

Requirements! + *

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

The command's description. + * + *

Requirements! + *

+ * @return + * The commands description as a {@link String} + */ + 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 CommandData addOptions(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 reating {@link ButtonClickEvent} + */ + default void onButtonClick(ButtonClickEvent event) {} + + /** + * 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 reating {@link ButtonClickEvent} + */ + default void onSelectionMenu(SelectionMenuEvent event) {} +} diff --git a/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java b/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java new file mode 100644 index 0000000000..ac8cc00216 --- /dev/null +++ b/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java @@ -0,0 +1,92 @@ +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.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import net.dv8tion.jda.api.requests.restaction.CommandCreateAction; +import org.togetherjava.tjbot.commands.Command; + +/** + * Example command + * + */ +public class CommandExample implements Command { + + /** + * The command name is example, so it returns example. + * + * @return + * {@link String} with the command's name + */ + @Override + public String getCommandName() { + return "example"; + } + + /** + * The description + * + * @return + * A {@link String} with the description + */ + @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 + * {@link Boolean} of {@code true} + */ + @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 has. + * + * @param commandData + * The thing we will add our options to! + * @return + * A {@link CommandCreateAction} with the required options for this command + */ + @Override + public CommandData addOptions(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 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 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("It'll only ping once, you spammer!").setEphemeral(true).queue(); + event.getChannel().sendMessage(userToMention.getAsMention().repeat(timesToPing)).queue(); + } +} diff --git a/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java b/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java new file mode 100644 index 0000000000..488c8eb098 --- /dev/null +++ b/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java @@ -0,0 +1,107 @@ +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.togetherjava.tjbot.commands.Command; + +/** + * 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 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 implements Command { + + /** + * The command name is subcommands-example, so it returns subcommands-example + * + * @return + * {@link String} with the command's name + */ + @Override + public String getCommandName() { + return "subcommands-example"; + } + + /** + * The description! + * + * @return + * A {@link String} with the description + */ + @Override + public String getDescription() { + return "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 CommandData addOptions(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 execute(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(); + } + } + } + } +}