Class Command
- Direct Known Subclasses:
AboutCommand
,GuildlistCommand
,PingCommand
,RoleinfoCommand
,ServerinfoCommand
,ShutdownCommand
,SlashCommand
Commands In JDA-Utilities
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:
- Author:
- John Grosh (jagrosh)
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
To be used inCommand
s as a means of organizing commands into "Categories" as well as terminate command usage when the callingCommandEvent
doesn't meet certain requirements. -
Field Summary
Modifier and TypeFieldDescriptionprotected String[]
The aliases of the command, when calling a command these function identically to calling theCommand.name
.protected String
An arguments format String for the command, used in the default help builder.protected Command.Category
TheCategory
of the command.protected Command[]
The child commands of the command.protected String
A small help String that summarizes the function of the command, used in the default help builder, and shown in the client for Slash Commands.protected BiConsumer<CommandEvent,
Command> TheBiConsumer
for creating a help response to the format[prefix]<command name> help
.protected boolean
true
if this command should be hidden from the help.protected String
The name of the command, allows the command to be called the formats:
Normal Command:[prefix]<command name>
.protected String
A String name of a role required to use this command.protected boolean
true
if this command checks a channel topic for topic-tags.Fields inherited from class com.jagrosh.jdautilities.command.Interaction
botMissingPermMessage, botPermissions, cooldown, cooldownScope, guildOnly, nsfwOnly, ownerCommand, userMissingPermMessage, userPermissions
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected abstract void
execute
(CommandEvent event) The main body method of aCommand
.String[]
Gets theCommand.aliases
for the Command.Gets theCommand.arguments
for the Command.Gets theCommand.category
for the Command.Command[]
Gets theCommand.children
for the Command.getCooldownError
(CommandEvent event, int remaining) Gets an error message for this Command under the providedCommanEvent
.getCooldownKey
(CommandEvent event) Gets the proper cooldown key for this Command under the providedCommandEvent
.getHelp()
Gets theCommand.help
for the Command.getName()
Gets theCommand.name
for the Command.Gets theCommand.requiredRole
for the Command.boolean
isAllowed
(net.dv8tion.jda.api.entities.channel.concrete.TextChannel channel) Checks whether a command is allowed in aTextChannel
by searching the channel topic for topic tags relating to the command.boolean
isCommandFor
(String input) Checks if the given input represents this Commandboolean
Checks if this Command can only be used in aGuild
.boolean
isHidden()
Checks whether or not this command should be hidden from the help.final void
run
(CommandEvent event) Runs checks for theCommand
with the givenCommandEvent
that called it.Methods inherited from class com.jagrosh.jdautilities.command.Interaction
getBotPermissions, getCooldown, getCooldownScope, getUserPermissions, isOwnerCommand
-
Field Details
-
name
The name of the command, allows the command to be called the formats:
Normal Command:[prefix]<command name>
.
Slash Command:/<command name>
-
help
A small help String that summarizes the function of the command, used in the default help builder, and shown in the client for Slash Commands. -
category
TheCategory
of the command.
This can perform any other checks not completed by the default conditional fields. -
arguments
An arguments format String for the command, used in the default help builder. Not supported for SlashCommands.- See Also:
-
requiredRole
A String name of a role required to use this command. -
aliases
The aliases of the command, when calling a command these function identically to calling theCommand.name
. This options only works for normal commands, not slash commands. -
children
The child commands of the command. These are used in the format[prefix]<parent name> <child name>
. -
helpBiConsumer
TheBiConsumer
for creating a help response to the format[prefix]<command name> help
. -
usesTopicTags
protected boolean usesTopicTagstrue
if this command checks a channel topic for topic-tags.
This means that putting{-commandname}
,{-command category}
,{-all}
in a channel topic will cause this command to terminate.
Defaulttrue
.
-
-
Constructor Details
-
Command
public Command()
-
-
Method Details
-
execute
- Parameters:
event
- TheCommandEvent
that triggered this Command
-
run
Runs checks for theCommand
with the givenCommandEvent
that called it.
Will terminate, and possibly respond with a failure message, if any checks fail.- Parameters:
event
- The CommandEvent that triggered this Command
-
isCommandFor
Checks if the given input represents this Command- Parameters:
input
- The input to check- Returns:
true
if the input is the name or an alias of the Command
-
isAllowed
public boolean isAllowed(net.dv8tion.jda.api.entities.channel.concrete.TextChannel channel) Checks whether a command is allowed in aTextChannel
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 returnfalse
if placed in a channel topic.NOTE:Topic tags are case sensitive and proper usage must be in lower case!
Also note that settingusesTopicTags
tofalse
will cause this method to always returntrue
, as the feature would not be applicable in the first place.- Parameters:
channel
- The TextChannel to test.- Returns:
true
if the channel topic doesn't specify any topic-tags that would cause this command to be cancelled, or ifusesTopicTags
has been set tofalse
.
-
getName
Gets theCommand.name
for the Command.- Returns:
- The name for the Command
-
getHelp
Gets theCommand.help
for the Command.- Returns:
- The help for the Command
-
getCategory
Gets theCommand.category
for the Command.- Returns:
- The category for the Command
-
getArguments
Gets theCommand.arguments
for the Command.- Returns:
- The arguments for the Command
-
isGuildOnly
public boolean isGuildOnly()Checks if this Command can only be used in aGuild
.- Returns:
true
if this Command can only be used in a Guild, elsefalse
if it can be used outside of one
-
getRequiredRole
Gets theCommand.requiredRole
for the Command.- Returns:
- The requiredRole for the Command
-
getAliases
Gets theCommand.aliases
for the Command.- Returns:
- The aliases for the Command
-
getChildren
Gets theCommand.children
for the Command.- Returns:
- The children for the Command
-
isHidden
public boolean isHidden()Checks whether or not this command should be hidden from the help.- Returns:
true
if the command should be hidden, otherwisefalse
-
getCooldownKey
Gets the proper cooldown key for this Command under the providedCommandEvent
.- Parameters:
event
- The CommandEvent to generate the cooldown for.- Returns:
- A String key to use when applying a cooldown.
-
getCooldownError
Gets an error message for this Command under the providedCommanEvent
.- Parameters:
event
- The CommandEvent to generate the error message for.remaining
- The remaining number of seconds a command is on cooldown for.- Returns:
- A String error message for this command if
remaining > 0
, elsenull
.
-