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! + *
+ * The command's description. + * + *
+ * Requirements! + *
+ * 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?=
- * 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.