Package com.jagrosh.jdautilities.command
Class SlashCommand
java.lang.Object
com.jagrosh.jdautilities.command.Command
com.jagrosh.jdautilities.command.SlashCommand
Slash Commands In JDA-Chewtils
This intends to mimic the command
with minimal breaking changes,
to make migration easy and smooth.
Breaking changes are documented here.
#execute(CommandEvent)
body:
public class ExampleCmd extends Command {
public ExampleCmd() {
this.name = "example";
this.help = "gives an example of commands do";
}
@Override
protected void execute(SlashCommandEvent event) {
event.reply("Hey look! This would be the bot's reply if this was a command!").queue();
}
}
Execution is with the provision of the SlashCommandEvent is performed in two steps:
- Author:
- Olivia (Chew)
-
Nested Class Summary
Nested classes/interfaces inherited from class com.jagrosh.jdautilities.command.Command
Command.Category, Command.CooldownScope
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected SlashCommand[]
The child commands of the command.protected CommandClient
The command client to be retrieved if needed.protected boolean
Whether this command is disabled by default.protected String[]
The list of role IDs who cannot use this Slash Command.protected String[]
The list of user IDs who cannot use this Slash Command.protected String[]
The list of role IDs who can use this Slash Command.protected String[]
The list of user IDs who can use this Slash Command.protected List<net.dv8tion.jda.api.interactions.commands.build.OptionData>
An array list of OptionData.protected String
Deprecated.protected net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData
The subcommand/child group this is associated with.Fields inherited from class com.jagrosh.jdautilities.command.Command
aliases, arguments, botMissingPermMessage, botPermissions, category, cooldown, cooldownScope, guildOnly, help, helpBiConsumer, hidden, name, nsfwOnly, ownerCommand, userMissingPermMessage, userPermissions, usesTopicTags
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionnet.dv8tion.jda.api.interactions.commands.build.CommandData
Builds CommandData for the SlashCommand upsert.List<net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege>
buildPrivileges
(@Nullable CommandClient client) Builds CommandPrivilege for the SlashCommand permissions.protected void
execute
(CommandEvent event) The main body method of aCommand
.protected abstract void
execute
(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event) The main body method of aSlashCommand
.Gets theCommand.children
for the Command.Gets the CommandClient.getCooldownError
(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, int remaining, CommandClient client) Gets an error message for this Command under the providedSlashCommandEvent
.getCooldownKey
(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event) Gets the proper cooldown key for this Command under the providedSlashCommandEvent
.String[]
Gets the disabled roles for this Slash Command.String[]
Gets the disabled users for this Slash Command.String[]
Gets the enabled roles for this Slash Command.String[]
Gets the enabled users for this Slash Command.List<net.dv8tion.jda.api.interactions.commands.build.OptionData>
Gets the options associated with this command.net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData
Gets the subcommand data associated with this subcommand.boolean
Whether or not this command is enabled by default.boolean
isOwner
(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, CommandClient client) Tests whether or not theUser
who triggered this event is an owner of the bot.final void
run
(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, CommandClient client) Runs checks for theSlashCommand
with the givenSlashCommandEvent
that called it.Methods inherited from class com.jagrosh.jdautilities.command.Command
getAliases, getArguments, getBotPermissions, getCategory, getCooldown, getCooldownError, getCooldownKey, getHelp, getName, getRequiredRole, getUserPermissions, isAllowed, isCommandFor, isGuildOnly, isHidden, isOwnerCommand, run
-
Field Details
-
requiredRole
Deprecated.This option is deprecated in favor ofenabledRoles
Please replace this with this.enabledRoles = new String[]{Roles}; While this check is still done, it's better to let Discord do the work.
This deprecation can be ignored if you intend to support normal and slash commands. -
enabledRoles
The list of role IDs who can use this Slash Command. Because command privileges are restricted to a Guild, these will not take effect for Global commands.
This is useless ifdefaultEnabled
isn't false. -
enabledUsers
The list of user IDs who can use this Slash Command. Because command privileges are restricted to a Guild, these will not take effect for Global commands.
This is useless ifdefaultEnabled
isn't false. -
disabledRoles
The list of role IDs who cannot use this Slash Command. Because command privileges are restricted to a Guild, these will not take effect for Global commands.
This is useless ifdefaultEnabled
isn't true. -
disabledUsers
The list of user IDs who cannot use this Slash Command. Because command privileges are restricted to a Guild, these will not take effect for Global commands.
This is useless ifdefaultEnabled
isn't true. -
defaultEnabled
protected boolean defaultEnabledWhether this command is disabled by default. If disabled, you must give yourself permission to use it.
In order forenabledUsers
andenabledRoles
to work, this must be set to false.- See Also:
-
CommandCreateAction.setDefaultEnabled(boolean)
enabledRoles
enabledUsers
-
children
The child commands of the command. These are used in the format/<parent name> <child name>
. This is synonymous with sub commands. Additionally, sub-commands cannot have children. -
subcommandGroup
protected net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData subcommandGroupThe subcommand/child group this is associated with. Will be in format/<parent name> <subcommandGroup name> <subcommand name>
. This only works in a child/subcommand. To instantiate:new SubcommandGroupData(name, description)
It's important the instantiations are the same across children if you intend to keep them in the same group. Can be null, and it will not be assigned to a group. -
options
An array list of OptionData. This is incompatible with children. You cannot have a child AND options. This is to specify different options for arguments and the stuff. For example, to add an argument for "input", you can do this:
OptionData data = new OptionData(OptionType.STRING, "input", "The input for the command").setRequired(true); List<OptionData> dataList = new ArrayList<>(); dataList.add(data); this.options = dataList;
-
client
The command client to be retrieved if needed.
-
-
Constructor Details
-
SlashCommand
public SlashCommand()
-
-
Method Details
-
execute
protected abstract void execute(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event) - Parameters:
event
- TheSlashCommandEvent
that triggered this Command
-
execute
The main body method of aCommand
.
This is the "response" for a successful#run(CommandEvent)
. Because this is a SlashCommand, this is called, but does nothing. You can still override this if you want to have a separate response for normal [prefix][name]. Keep in mind you must add it as a Command viaCommandClientBuilder.addCommand(Command)
for it to work properly.- Specified by:
execute
in classCommand
- Parameters:
event
- TheCommandEvent
that triggered this Command
-
run
public final void run(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, CommandClient client) Runs checks for theSlashCommand
with the givenSlashCommandEvent
that called it.
Will terminate, and possibly respond with a failure message, if any checks fail.- Parameters:
event
- The SlashCommandEvent that triggered this Commandclient
- The CommandClient for checks and stuff
-
isOwner
public boolean isOwner(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, CommandClient client) Tests whether or not theUser
who triggered this event is an owner of the bot.- Parameters:
event
- the event that triggered the commandclient
- the command client for checking stuff- Returns:
true
if the User is the Owner, elsefalse
-
getClient
Gets the CommandClient.- Returns:
- the CommandClient.
-
getEnabledRoles
Gets the enabled roles for this Slash Command. A user MUST have a role for a command to be ran.- Returns:
- a list of String role IDs
-
getEnabledUsers
Gets the enabled users for this Slash Command. A user with an ID in this list is required for the command to be ran.- Returns:
- a list of String user IDs
-
getDisabledRoles
Gets the disabled roles for this Slash Command. A user with this role may not run this command.- Returns:
- a list of String role IDs
-
getDisabledUsers
Gets the disabled users for this Slash Command. Uses in this list may not run this command.- Returns:
- a list of String user IDs
-
isDefaultEnabled
public boolean isDefaultEnabled()Whether or not this command is enabled by default. If disabled by default, you MUST enableroles
orusers
to access it. This does NOT hide it, it simply appears greyed out.- Returns:
- a list of String user IDs
-
getSubcommandGroup
public net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData getSubcommandGroup()Gets the subcommand data associated with this subcommand.- Returns:
- subcommand data
-
getOptions
Gets the options associated with this command.- Returns:
- the OptionData array for options
-
buildCommandData
public net.dv8tion.jda.api.interactions.commands.build.CommandData buildCommandData()Builds CommandData for the SlashCommand upsert. This code is executed when we need to upsert the command. Useful for manual upserting.- Returns:
- the built command data
-
buildPrivileges
public List<net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege> buildPrivileges(@Nullable @Nullable CommandClient client) Builds CommandPrivilege for the SlashCommand permissions. This code is executed after upsertion to update the permissions.
The max amount of privilege is 10, keep this in mind. Useful for manual upserting.- Parameters:
client
- the command client for owner checking. if null, owner checks won't be performed- Returns:
- the built privilege data
-
getChildren
Gets theCommand.children
for the Command.- Overrides:
getChildren
in classCommand
- Returns:
- The children for the Command
-
getCooldownKey
Gets the proper cooldown key for this Command under the providedSlashCommandEvent
.- Parameters:
event
- The CommandEvent to generate the cooldown for.- Returns:
- A String key to use when applying a cooldown.
-
getCooldownError
public String getCooldownError(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, int remaining, CommandClient client) Gets an error message for this Command under the providedSlashCommandEvent
.- Parameters:
event
- The CommandEvent to generate the error message for.remaining
- The remaining number of seconds a command is on cooldown for.client
- The CommandClient for checking stuff- Returns:
- A String error message for this command if
remaining > 0
, elsenull
.
-