Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/org/togetherjava/tjbot/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/org/togetherjava/tjbot/commands/Command.java
Original file line number Diff line number Diff line change
@@ -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.
* <br> Look at {@link CommandExample} for an example.
* <br> A second example will come for sub-commands.
*
*/
public interface Command {

/**
* <p> The command's name.
*
* <p> <b>Requirements!</b>
* <ul>
* <li>Lowercase</li>
* <li>Alphanumeric (with dash)</li>
* <li>1 to 32 characters long</li>
* </ul>
* @return
* The commands name as a {@link String}
*/
String getCommandName();

/**
* <p> The command's description.
*
* <p> <b>Requirements!</b>
* <ul>
* <li>1 to 100 characters long</li>
* </ul>
* @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.
* <br> 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.
* <br> Check <a href="https://github.com/DV8FromTheWorld/JDA/wiki/Interactions#slash-commands">JDA's slash-commands Wiki article</a> 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.
* <br> Check <a href="https://github.com/DV8FromTheWorld/JDA/wiki/Interactions#buttons">JDA's buttons Wiki article</a> 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.
* <br> JDA has no dedicated guide for selection menu's, they work the same as buttons.
* <br> Check <a href="https://github.com/DV8FromTheWorld/JDA/wiki/Interactions#buttons">JDA's buttons Wiki article</a> for more information.
*
* @param event
* The reating {@link ButtonClickEvent}
*/
default void onSelectionMenu(SelectionMenuEvent event) {}
}
Original file line number Diff line number Diff line change
@@ -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.
* <br> 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.
* <br> 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!
* <br> Here it loads the options the users made.
* <br> After that it pings the user ... times.
* <br> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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!
*
* <p>
* 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.
*
* <p>
* A good example is the ban command, instead of having tempban, permban separate this can become 1 command.
*
* <br> <i>counts as 1 command within Discord</i> <br>
* <br> `{@code ban temp 6d Tijs#0001}` and `{@code ban perm Tijs#0001}`
*
* <br> instead of
* <br> <i>counts as 2 commands within Discord</i>
* `{@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!
* <br> Here it checks what subcommand the user ran.
* <br> 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();
}
}
}
}
}