Class Command
- Direct Known Subclasses:
AboutCommand,GuildlistCommand,PingCommand,RoleinfoCommand,ServerinfoCommand,ShutdownCommand,SlashCommand
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
Nested ClassesModifier and TypeClassDescriptionstatic classTo be used inCommands as a means of organizing commands into "Categories" as well as terminate command usage when the callingCommandEventdoesn't meet certain requirements.static enum -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected String[]The aliases of the command, when calling a command these function identically to calling theCommand.name.protected StringAn arguments format String for the command, used in the default help builder.protected StringThe permission message used when the bot does not have the requires permission.protected net.dv8tion.jda.api.Permission[]AnyPermissions the bot must have to use a command.protected Command.CategoryTheCategoryof the command.protected Command[]The child commands of the command.protected intAnintnumber of seconds users must wait before using this command again.protected Command.CooldownScopeTheCooldownScopeof the command.protected booleantrueif the command may only be used in aGuild,falseif it may be used in both a Guild and a DM.protected StringA 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> TheBiConsumerfor creating a help response to the format[prefix]<command name> help.protected booleantrueif this command should be hidden from the help.protected StringThe name of the command, allows the command to be called the formats:
Normal Command:[prefix]<command name>.protected booleantrueif the command may only be used in an NSFWTextChannelor DMs.protected booleantrueif the command may only be used by a User with an ID matching the Owners or any of the CoOwners.
If enabled for a Slash Command, only owners (owner + up to 9 co-owners) will be added to the SlashCommand.protected StringA String name of a role required to use this command.protected StringThe permission message used when the user does not have the requires permission.protected net.dv8tion.jda.api.Permission[]AnyPermissions a Member must have to use this command.protected booleantrueif this command checks a channel topic for topic-tags. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidexecute(CommandEvent event) The main body method of aCommand.String[]Gets theCommand.aliasesfor the Command.Gets theCommand.argumentsfor the Command.net.dv8tion.jda.api.Permission[]Gets theCommand.botPermissionsfor the Command.Gets theCommand.categoryfor the Command.Command[]Gets theCommand.childrenfor the Command.intGets theCommand.cooldownfor 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.helpfor the Command.getName()Gets theCommand.namefor the Command.Gets theCommand.requiredRolefor the Command.net.dv8tion.jda.api.Permission[]Gets theCommand.userPermissionsfor the Command.booleanisAllowed(net.dv8tion.jda.api.entities.TextChannel channel) Checks whether a command is allowed in aTextChannelby searching the channel topic for topic tags relating to the command.booleanisCommandFor(String input) Checks if the given input represents this CommandbooleanChecks if this Command can only be used in aGuild.booleanisHidden()Checks whether or not this command should be hidden from the help.booleanChecks whether or not this command is an owner only Command.final voidrun(CommandEvent event) Runs checks for theCommandwith the givenCommandEventthat called it.
-
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
TheCategoryof 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:
-
guildOnly
protected boolean guildOnlytrueif the command may only be used in aGuild,falseif it may be used in both a Guild and a DM.
Defaulttrue. -
nsfwOnly
protected boolean nsfwOnlytrueif the command may only be used in an NSFWTextChannelor DMs.falseif it may be used anywhere
Default:false -
requiredRole
A String name of a role required to use this command. -
ownerCommand
protected boolean ownerCommandtrueif the command may only be used by a User with an ID matching the Owners or any of the CoOwners.
If enabled for a Slash Command, only owners (owner + up to 9 co-owners) will be added to the SlashCommand. All other permissions will be ignored.
Defaultfalse. -
cooldown
protected int cooldownAnintnumber of seconds users must wait before using this command again. -
userPermissions
protected net.dv8tion.jda.api.Permission[] userPermissionsAnyPermissions a Member must have to use this command.
These are only checked in aGuildenvironment. -
botPermissions
protected net.dv8tion.jda.api.Permission[] botPermissionsAnyPermissions the bot must have to use a command.
These are only checked in aGuildenvironment. -
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
TheBiConsumerfor creating a help response to the format[prefix]<command name> help. -
usesTopicTags
protected boolean usesTopicTagstrueif 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. -
cooldownScope
TheCooldownScopeof the command. This defines how far of a scope cooldowns have.
DefaultCooldownScope.USER. -
botMissingPermMessage
The permission message used when the bot does not have the requires permission. Requires 3 "%s", first is user mention, second is the permission needed, third is type, e.g. Guild. -
userMissingPermMessage
The permission message used when the user does not have the requires permission. Requires 3 "%s", first is user mention, second is the permission needed, third is type, e.g. Guild.
-
-
Constructor Details
-
Command
public Command()
-
-
Method Details
-
execute
- Parameters:
event- TheCommandEventthat triggered this Command
-
run
Runs checks for theCommandwith the givenCommandEventthat 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:
trueif the input is the name or an alias of the Command
-
isAllowed
public boolean isAllowed(net.dv8tion.jda.api.entities.TextChannel channel) Checks whether a command is allowed in aTextChannelby 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 returnfalseif placed in a channel topic.NOTE:Topic tags are case sensitive and proper usage must be in lower case!
Also note that settingusesTopicTagstofalsewill cause this method to always returntrue, as the feature would not be applicable in the first place.- Parameters:
channel- The TextChannel to test.- Returns:
trueif the channel topic doesn't specify any topic-tags that would cause this command to be cancelled, or ifusesTopicTagshas been set tofalse.
-
getName
Gets theCommand.namefor the Command.- Returns:
- The name for the Command
-
getHelp
Gets theCommand.helpfor the Command.- Returns:
- The help for the Command
-
getCategory
Gets theCommand.categoryfor the Command.- Returns:
- The category for the Command
-
getArguments
Gets theCommand.argumentsfor the Command.- Returns:
- The arguments for the Command
-
isGuildOnly
public boolean isGuildOnly()Checks if this Command can only be used in aGuild.- Returns:
trueif this Command can only be used in a Guild, elsefalseif it can be used outside of one
-
getRequiredRole
Gets theCommand.requiredRolefor the Command.- Returns:
- The requiredRole for the Command
-
getCooldown
public int getCooldown()Gets theCommand.cooldownfor the Command.- Returns:
- The cooldown for the Command
-
getUserPermissions
public net.dv8tion.jda.api.Permission[] getUserPermissions()Gets theCommand.userPermissionsfor the Command.- Returns:
- The userPermissions for the Command
-
getBotPermissions
public net.dv8tion.jda.api.Permission[] getBotPermissions()Gets theCommand.botPermissionsfor the Command.- Returns:
- The botPermissions for the Command
-
getAliases
Gets theCommand.aliasesfor the Command.- Returns:
- The aliases for the Command
-
getChildren
Gets theCommand.childrenfor the Command.- Returns:
- The children for the Command
-
isOwnerCommand
public boolean isOwnerCommand()Checks whether or not this command is an owner only Command.- Returns:
trueif the command is an owner command, otherwisefalseif it is not
-
isHidden
public boolean isHidden()Checks whether or not this command should be hidden from the help.- Returns:
trueif 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.
-