Package com.jagrosh.jdautilities.command
Class SlashCommand
java.lang.Object
com.jagrosh.jdautilities.command.Interaction
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 SlashCommand {
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
-
Field Summary
Modifier and TypeFieldDescriptionprotected SlashCommand[]
The child commands of the command.protected CommandClient
Deprecated.Localization of slash command description.Localization of slash command name.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, category, help, helpBiConsumer, hidden, name, usesTopicTags
Fields inherited from class com.jagrosh.jdautilities.command.Interaction
botMissingPermMessage, botPermissions, cooldown, cooldownScope, guildOnly, nsfwOnly, ownerCommand, userMissingPermMessage, userPermissions
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionnet.dv8tion.jda.api.interactions.commands.build.CommandData
Builds CommandData for the SlashCommand upsert.protected void
execute
(CommandEvent event) The main body method of aCommand
.protected abstract void
execute
(SlashCommandEvent event) The main body method of aSlashCommand
.Gets theCommand.children
for the Command.Deprecated.This is now retrieved fromSlashCommandEvent.getClient()
.getCooldownError
(SlashCommandEvent event, int remaining, CommandClient client) Gets an error message for this Command under the providedSlashCommandEvent
.getCooldownKey
(SlashCommandEvent event) Gets the proper cooldown key for this Command under the providedSlashCommandEvent
.Gets the specified localizations of slash command descriptions.Gets the specified localizations of slash command names.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
isOwner
(SlashCommandEvent event, CommandClient client) Tests whether or not theUser
who triggered this event is an owner of the bot.void
onAutoComplete
(net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent event) This body is executed when an auto-complete event is received.final void
run
(SlashCommandEvent event) Runs checks for theSlashCommand
with the givenSlashCommandEvent
that called it.Methods inherited from class com.jagrosh.jdautilities.command.Command
getAliases, getArguments, getCategory, getCooldownError, getCooldownKey, getHelp, getName, getRequiredRole, isAllowed, isCommandFor, isGuildOnly, isHidden, run
Methods inherited from class com.jagrosh.jdautilities.command.Interaction
getBotPermissions, getCooldown, getCooldownScope, getUserPermissions, isOwnerCommand
-
Field Details
-
nameLocalization
Localization of slash command name. Allows discord to change the language of the name of slash commands in the client.
Example:
public Command() { this.name = "help" this.nameLocalization = Map.of(DiscordLocale.GERMAN, "hilfe", DiscordLocale.RUSSIAN, "помощь"); }
-
descriptionLocalization
Localization of slash command description. Allows discord to change the language of the description of slash commands in the client.
Example:
public Command() { this.description = "all commands" this.descriptionLocalization = Map.of(DiscordLocale.GERMAN, "alle Befehle", DiscordLocale.RUSSIAN, "все команды"); }
-
requiredRole
Deprecated.This option is deprecated in favor of using Discord's permissions
This deprecation can be ignored if you intend to support normal and slash commands. -
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
Deprecated.This is now retrieved fromSlashCommandEvent.getClient()
.The command client to be retrieved if needed.
-
-
Constructor Details
-
SlashCommand
public SlashCommand()
-
-
Method Details
-
execute
- Parameters:
event
- TheSlashCommandEvent
that triggered this Command
-
onAutoComplete
public void onAutoComplete(net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent event) This body is executed when an auto-complete event is received. This only ever gets executed if an auto-completeoption
is set.- Parameters:
event
- The event to handle.- See Also:
-
OptionData.setAutoComplete(boolean)
-
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
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 Command
-
isOwner
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
Deprecated.This is now retrieved fromSlashCommandEvent.getClient()
.Gets the CommandClient.- Returns:
- the CommandClient.
-
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
-
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
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
.
-
getNameLocalization
Gets the specified localizations of slash command names.- Returns:
- Slash command name localizations.
-
getDescriptionLocalization
Gets the specified localizations of slash command descriptions.- Returns:
- Slash command description localizations.
-
SlashCommandEvent.getClient()
.