Skip to content
Closed
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
53 changes: 53 additions & 0 deletions src/main/java/org/togetherjava/tjbot/commands/AbstractCommand.java
Original file line number Diff line number Diff line change
@@ -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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be able to be an interface if we never need local state:

Suggested change
public abstract class AbstractCommand {
public interface Command

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this gets merged to develop, we will look back and change to an interface if no features of abstract classes will be used.
My last message


/**
* 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;
}
Comment on lines +32 to +34

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public boolean isGuildOnly() {
return false;
}
default 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 <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}
*/
public abstract void execute(SlashCommandEvent event);
}
Original file line number Diff line number Diff line change
@@ -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();
}
}