public abstract class Command
extends java.lang.Object
The internal inheritance for Commands used in JDA-Utilities is that of the Command object.
Classes created inheriting this class gain the unique traits of commands operated using the Commands Extension.
Using several fields, a command can define properties that make it unique and complex while maintaining
a low level of development.
All Commands extending this class can define any number of these fields in a object constructor and then
create the command action/response in the abstract
#execute(CommandEvent) body:
public class ExampleCmd extends Command {
public ExampleCmd() {
this.name = "example";
this.aliases = new String[]{"test","demo"};
this.help = "gives an example of commands do";
}
@Override
protected void execute(CommandEvent) {
event.reply("Hey look! This would be the bot's reply if this was a command!");
}
}
Execution is with the provision of a MessageReceivedEvent-CommandClient wrapper called a
CommandEvent and is performed in two steps:
| Modifier and Type | Class and Description |
|---|---|
static class |
Command.Category
To be used in
Commands as a means of
organizing commands into "Categories" as well as terminate command usage when the calling
CommandEvent doesn't meet
certain requirements. |
static class |
Command.CooldownScope
A series of
Enums used for defining the scope size for a
Command's cooldown. |
| Modifier and Type | Field and Description |
|---|---|
protected java.lang.String[] |
aliases
The aliases of the command, when calling a command these function identically to calling the
Command.name. |
protected java.lang.String |
arguments
An arguments format String for the command, used in the default help builder.
|
protected net.dv8tion.jda.core.Permission[] |
botPermissions
Any
Permissions the bot must have to use a command. |
protected Command.Category |
category
The
Category of the command. |
protected Command[] |
children
The child commands of the command.
|
protected int |
cooldown
An
int number of seconds users must wait before using this command again. |
protected Command.CooldownScope |
cooldownScope
The
CooldownScope
of the command. |
protected boolean |
guildOnly
true if the command may only be used in a Guild,
false if it may be used in both a Guild and a DM. |
protected java.lang.String |
help
A small help String that summarizes the function of the command, used in the default help builder.
|
protected java.util.function.BiConsumer<CommandEvent,Command> |
helpBiConsumer
The
BiConsumer for creating a help response to the format
[prefix]<command name> help. |
protected boolean |
hidden
true if this command should be hidden from the help. |
protected java.lang.String |
name
The name of the command, allows the command to be called the format:
[prefix]<command name>. |
protected boolean |
ownerCommand
true if the command may only be used by a User with an ID matching the
Owners or any of the CoOwners. |
protected java.lang.String |
requiredRole
A String name of a role required to use this command.
|
protected net.dv8tion.jda.core.Permission[] |
userPermissions
Any
Permissions a Member must have to use this command. |
protected boolean |
usesTopicTags
true if this command checks a channel topic for topic-tags. |
| Constructor and Description |
|---|
Command() |
| Modifier and Type | Method and Description |
|---|---|
protected abstract void |
execute(CommandEvent event)
The main body method of a
Command. |
java.lang.String[] |
getAliases()
Gets the
Command.aliases for the Command. |
java.lang.String |
getArguments()
Gets the
Command.arguments for the Command. |
net.dv8tion.jda.core.Permission[] |
getBotPermissions()
Gets the
Command.botPermissions for the Command. |
Command.Category |
getCategory()
Gets the
Command.category for the Command. |
Command[] |
getChildren()
Gets the
Command.children for the Command. |
int |
getCooldown()
Gets the
Command.cooldown for the Command. |
java.lang.String |
getCooldownError(CommandEvent event,
int remaining)
Gets an error message for this Command under the provided
CommanEvent. |
java.lang.String |
getCooldownKey(CommandEvent event)
Gets the proper cooldown key for this Command under the provided
CommanEvent. |
java.lang.String |
getHelp()
Gets the
Command.help for the Command. |
java.lang.String |
getName()
Gets the
Command.name for the Command. |
java.lang.String |
getRequiredRole()
Gets the
Command.requiredRole for the Command. |
net.dv8tion.jda.core.Permission[] |
getUserPermissions()
Gets the
Command.userPermissions for the Command. |
boolean |
isAllowed(net.dv8tion.jda.core.entities.TextChannel channel)
Checks whether a command is allowed in a
TextChannel
by searching the channel topic for topic tags relating to the command. |
boolean |
isCommandFor(java.lang.String input)
Checks if the given input represents this Command
|
boolean |
isGuildOnly()
Checks if this Command can only be used in a
Guild. |
boolean |
isHidden()
Checks whether or not this command should be hidden from the help
|
boolean |
isOwnerCommand()
Checks whether or not this command is an owner only Command.
|
void |
run(CommandEvent event)
Runs checks for the
Command with the
given CommandEvent that called it. |
protected java.lang.String name
[prefix]<command name>.protected java.lang.String help
protected Command.Category category
Category of the command.
protected java.lang.String arguments
protected boolean guildOnly
true if the command may only be used in a Guild,
false if it may be used in both a Guild and a DM.
true.protected java.lang.String requiredRole
protected boolean ownerCommand
true if the command may only be used by a User with an ID matching the
Owners or any of the CoOwners.
false.protected int cooldown
int number of seconds users must wait before using this command again.protected net.dv8tion.jda.core.Permission[] userPermissions
Permissions a Member must have to use this command.
Guild environment.protected net.dv8tion.jda.core.Permission[] botPermissions
Permissions the bot must have to use a command.
Guild environment.protected java.lang.String[] aliases
Command.name.protected Command[] children
[prefix]<parent name>
<child name>.protected java.util.function.BiConsumer<CommandEvent,Command> helpBiConsumer
BiConsumer for creating a help response to the format
[prefix]<command name> help.protected boolean usesTopicTags
true if this command checks a channel topic for topic-tags.
{-commandname}, {-command category}, {-all} in a channel topic
will cause this command to terminate.
true.protected boolean hidden
true if this command should be hidden from the help.
falseprotected Command.CooldownScope cooldownScope
CooldownScope
of the command. This defines how far of a scope cooldowns have.
CooldownScope.USER.protected abstract void execute(CommandEvent event)
event - The CommandEvent that
triggered this Commandpublic final void run(CommandEvent event)
Command with the
given CommandEvent that called it.
event - The CommandEvent that triggered this Commandpublic boolean isCommandFor(java.lang.String input)
input - The input to checktrue if the input is the name or an alias of the Commandpublic boolean isAllowed(net.dv8tion.jda.core.entities.TextChannel channel)
TextChannel
by searching the channel topic for topic tags relating to the command.
{-name},
{-category name}, or {-all}
are valid examples of ways that this method would return false if placed in a channel topic.
NOTE:Topic tags are case sensitive and proper usage must be in lower case!
Also note that setting usesTopicTags
to false will cause this method to always return true, as the feature would not be applicable
in the first place.
channel - The TextChannel to test.true if the channel topic doesn't specify any topic-tags that would cause this command
to be cancelled, or if usesTopicTags has been set to false.public java.lang.String getName()
Command.name for the Command.public java.lang.String getHelp()
Command.help for the Command.public Command.Category getCategory()
Command.category for the Command.public java.lang.String getArguments()
Command.arguments for the Command.public boolean isGuildOnly()
Guild.true if this Command can only be used in a Guild, else false if it can
be used outside of onepublic java.lang.String getRequiredRole()
Command.requiredRole for the Command.public int getCooldown()
Command.cooldown for the Command.public net.dv8tion.jda.core.Permission[] getUserPermissions()
Command.userPermissions for the Command.public net.dv8tion.jda.core.Permission[] getBotPermissions()
Command.botPermissions for the Command.public java.lang.String[] getAliases()
Command.aliases for the Command.public Command[] getChildren()
Command.children for the Command.public boolean isOwnerCommand()
true if the command is an owner command, otherwise false if it is notpublic boolean isHidden()
true if the command should be hidden, otherwise falsepublic java.lang.String getCooldownKey(CommandEvent event)
CommanEvent.event - The CommandEvent to generate the cooldown for.public java.lang.String getCooldownError(CommandEvent event, int remaining)
CommanEvent.event - The CommandEvent to generate the error message for.remaining - The remaining number of seconds a command is on cooldown for.remaining > 0,
else null.