From c732be13dce81e0d108a15682163464e7ed12220 Mon Sep 17 00:00:00 2001
From: Tais993 <49957334+Tais993@users.noreply.github.com>
Date: Wed, 8 Sep 2021 23:46:13 +0200
Subject: [PATCH 1/6] Second try. Thought again about interface vs abstract
class. And ended up with an interface.
I'm not sure how it exactly ends like, so maybe it will become an abstract class again.
We'll find out
Added SubCommandExample, it's an example for a command with subcommands.
---
.../tjbot/commands/AbstractCommand.java | 62 ++++++++++
.../commands/example/CommandExample.java | 93 +++++++++++++++
.../commands/example/SubCommandExample.java | 107 ++++++++++++++++++
3 files changed, 262 insertions(+)
create mode 100644 src/main/java/org/togetherjava/tjbot/commands/AbstractCommand.java
create mode 100644 src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java
create mode 100644 src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java
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..f740bf6b14
--- /dev/null
+++ b/src/main/java/org/togetherjava/tjbot/commands/AbstractCommand.java
@@ -0,0 +1,62 @@
+package org.togetherjava.tjbot.commands;
+
+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 abstract class AbstractCommand {
+
+ /**
+ * The commands name.
+ *
+ * @return
+ * The commands name as a {@link String}
+ */
+ public abstract String getCommandName();
+
+ /**
+ * The command's 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 CommandExample#addOptions(CommandData)} for an example.
+ *
+ * @param commandData
+ * The {@link CommandData} where the options need to be added.
+ * @return
+ * The changed {@link CommandData}
+ */
+ public 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}
+ */
+ public abstract void execute(SlashCommandEvent 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..8d4caeef16
--- /dev/null
+++ b/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java
@@ -0,0 +1,93 @@
+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.CommandData;
+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 CommandExample extends AbstractCommand {
+
+ /**
+ * 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, "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) {
+
+ // * gets the option as a user
+ 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();
+ }
+}
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..d239990794
--- /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.AbstractCommand;
+
+/**
+ * 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 extends AbstractCommand {
+
+ /**
+ * 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 check 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();
+ }
+ }
+ }
+ }
+}
From 0cd2db5ef95269e43ca827854149ba0f69af0eda Mon Sep 17 00:00:00 2001
From: Tais993 <49957334+Tais993@users.noreply.github.com>
Date: Thu, 9 Sep 2021 00:02:30 +0200
Subject: [PATCH 2/6] Actually changed it to an interface now. Let's not talk
about that :)
---
src/main/java/org/togetherjava/tjbot/Main.java | 1 +
.../commands/{AbstractCommand.java => ICommand.java} | 12 ++++++------
.../tjbot/commands/example/CommandExample.java | 9 ++++-----
.../tjbot/commands/example/SubCommandExample.java | 4 ++--
4 files changed, 13 insertions(+), 13 deletions(-)
rename src/main/java/org/togetherjava/tjbot/commands/{AbstractCommand.java => ICommand.java} (84%)
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/AbstractCommand.java b/src/main/java/org/togetherjava/tjbot/commands/ICommand.java
similarity index 84%
rename from src/main/java/org/togetherjava/tjbot/commands/AbstractCommand.java
rename to src/main/java/org/togetherjava/tjbot/commands/ICommand.java
index f740bf6b14..b1b38bda59 100644
--- a/src/main/java/org/togetherjava/tjbot/commands/AbstractCommand.java
+++ b/src/main/java/org/togetherjava/tjbot/commands/ICommand.java
@@ -10,7 +10,7 @@
*
A second example will come for sub-commands.
*
*/
-public abstract class AbstractCommand {
+public interface ICommand {
/**
* The commands name.
@@ -18,7 +18,7 @@ public abstract class AbstractCommand {
* @return
* The commands name as a {@link String}
*/
- public abstract String getCommandName();
+ String getCommandName();
/**
* The command's description.
@@ -26,7 +26,7 @@ public abstract class AbstractCommand {
* @return
* The commands description as a {@link String}
*/
- public abstract String getDescription();
+ String getDescription();
/**
* Whenever the command is only for guilds, optional method.
@@ -34,7 +34,7 @@ public abstract class AbstractCommand {
* @return
* Whenever the command is only for guilds as a {@link Boolean}
*/
- public boolean isGuildOnly() {
+ default boolean isGuildOnly() {
return false;
}
@@ -47,7 +47,7 @@ public boolean isGuildOnly() {
* @return
* The changed {@link CommandData}
*/
- public CommandData addOptions(CommandData commandData) {
+ default CommandData addOptions(CommandData commandData) {
return commandData;
}
@@ -58,5 +58,5 @@ public CommandData addOptions(CommandData commandData) {
* @param event
* The relating {@link SlashCommandEvent}
*/
- public abstract void execute(SlashCommandEvent event);
+ void execute(SlashCommandEvent 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
index 8d4caeef16..e785c221fc 100644
--- a/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java
+++ b/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java
@@ -2,18 +2,17 @@
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.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.requests.restaction.CommandCreateAction;
-import org.togetherjava.tjbot.commands.AbstractCommand;
+import org.togetherjava.tjbot.commands.ICommand;
/**
* Example command
*
*/
-public class CommandExample extends AbstractCommand {
+public class CommandExample implements ICommand {
/**
* The command name is example, so it returns example.
@@ -65,8 +64,8 @@ public CommandData addOptions(CommandData commandData) {
new OptionData(OptionType.USER, "User", "User that will be pinged!", true),
// * adds the second option
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")
+ new net.dv8tion.jda.api.interactions.commands.Command.Choice("Once", "1"),
+ new net.dv8tion.jda.api.interactions.commands.Command.Choice("Twice", "2")
).setRequired(true)
);
}
diff --git a/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java b/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java
index d239990794..9751362006 100644
--- a/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java
+++ b/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java
@@ -6,7 +6,7 @@
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.AbstractCommand;
+import org.togetherjava.tjbot.commands.ICommand;
/**
* Subcommands example!
@@ -26,7 +26,7 @@
* `{@code tempban 6s Tijs#0001}` and `{@code permban Tijs#0001}`
*
*/
-public class SubCommandExample extends AbstractCommand {
+public class SubCommandExample implements ICommand {
/**
* The command name is subcommands-example, so it returns subcommands-example
From 8bef152fbaa5b09eef44661e73b10d452f1c1a26 Mon Sep 17 00:00:00 2001
From: Tais993 <49957334+Tais993@users.noreply.github.com>
Date: Wed, 8 Sep 2021 23:46:13 +0200
Subject: [PATCH 3/6] Second try. Thought again about interface vs abstract
class. And ended up with an interface.
I'm not sure how it exactly ends like, so maybe it will become an abstract class again.
We'll find out
Added SubCommandExample, it's an example for a command with subcommands.
---
.../java/org/togetherjava/tjbot/Main.java | 1 +
.../togetherjava/tjbot/commands/ICommand.java | 62 ++++++++++
.../commands/example/CommandExample.java | 92 +++++++++++++++
.../commands/example/SubCommandExample.java | 107 ++++++++++++++++++
4 files changed, 262 insertions(+)
create mode 100644 src/main/java/org/togetherjava/tjbot/commands/ICommand.java
create mode 100644 src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java
create mode 100644 src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java
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/ICommand.java b/src/main/java/org/togetherjava/tjbot/commands/ICommand.java
new file mode 100644
index 0000000000..b1b38bda59
--- /dev/null
+++ b/src/main/java/org/togetherjava/tjbot/commands/ICommand.java
@@ -0,0 +1,62 @@
+package org.togetherjava.tjbot.commands;
+
+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 ICommand {
+
+ /**
+ * The commands name.
+ *
+ * @return
+ * The commands name as a {@link String}
+ */
+ String getCommandName();
+
+ /**
+ * The command's description.
+ *
+ * @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}
+ */
+ void execute(SlashCommandEvent 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..e785c221fc
--- /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.ICommand;
+
+/**
+ * Example command
+ *
+ */
+public class CommandExample implements ICommand {
+
+ /**
+ * 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, "Amount of pings", "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 execute(SlashCommandEvent event) {
+
+ // * gets the option as a user
+ 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();
+ }
+}
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..9751362006
--- /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.ICommand;
+
+/**
+ * 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 ICommand {
+
+ /**
+ * 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 check 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();
+ }
+ }
+ }
+ }
+}
From 5e8d2c09c3db9b5f469a95d0418087fe67478fb7 Mon Sep 17 00:00:00 2001
From: Tais993 <49957334+Tais993@users.noreply.github.com>
Date: Thu, 9 Sep 2021 00:42:13 +0200
Subject: [PATCH 4/6] Fixed incorrect naming and added requirements section to
the ICommand#getCommandName and to the ICommand#getDescription method
---
.../org/togetherjava/tjbot/commands/ICommand.java | 14 ++++++++++++--
.../tjbot/commands/example/CommandExample.java | 8 ++++----
.../tjbot/commands/example/SubCommandExample.java | 10 +++++-----
3 files changed, 21 insertions(+), 11 deletions(-)
diff --git a/src/main/java/org/togetherjava/tjbot/commands/ICommand.java b/src/main/java/org/togetherjava/tjbot/commands/ICommand.java
index b1b38bda59..855f7dc6ca 100644
--- a/src/main/java/org/togetherjava/tjbot/commands/ICommand.java
+++ b/src/main/java/org/togetherjava/tjbot/commands/ICommand.java
@@ -13,16 +13,26 @@
public interface ICommand {
/**
- * The commands name.
+ *
The command's name. * + *
Requirements! + *
The command's description. * + *
Requirements! + *
The command's name. diff --git a/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java b/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java index 846bb7f727..ac8cc00216 100644 --- a/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java +++ b/src/main/java/org/togetherjava/tjbot/commands/example/CommandExample.java @@ -6,13 +6,13 @@ 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.ICommand; +import org.togetherjava.tjbot.commands.Command; /** * Example command * */ -public class CommandExample implements ICommand { +public class CommandExample implements Command { /** * The command name is example, so it returns example. diff --git a/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java b/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java index 166f01e0e5..488c8eb098 100644 --- a/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java +++ b/src/main/java/org/togetherjava/tjbot/commands/example/SubCommandExample.java @@ -6,7 +6,7 @@ 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.ICommand; +import org.togetherjava.tjbot.commands.Command; /** * Subcommands example! @@ -26,7 +26,7 @@ * `{@code tempban 6s Tijs#0001}` and `{@code permban Tijs#0001}` * */ -public class SubCommandExample implements ICommand { +public class SubCommandExample implements Command { /** * The command name is subcommands-example, so it returns subcommands-example