Class SlashCommand


public abstract class SlashCommand extends Command

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:
  • run - The command runs through a series of conditionals, automatically terminating the command instance if one is not met, and possibly providing an error response.
  • execute - The command, now being cleared to run, executes and performs whatever lies in the abstract body method.
Author:
Olivia (Chew)
  • Field Details

    • nameLocalization

      protected Map<net.dv8tion.jda.api.interactions.DiscordLocale,String> 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

      protected Map<net.dv8tion.jda.api.interactions.DiscordLocale,String> 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 protected String 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

      protected SlashCommand[] 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 subcommandGroup
      The 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

      protected List<net.dv8tion.jda.api.interactions.commands.build.OptionData> 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

      protected CommandClient client
      Deprecated.
      This is now retrieved from SlashCommandEvent.getClient().
      The command client to be retrieved if needed.
  • Constructor Details

    • SlashCommand

      public SlashCommand()
  • Method Details

    • execute

      protected abstract void execute(SlashCommandEvent event)
      The main body method of a SlashCommand.
      This is the "response" for a successful #run(CommandEvent).
      Parameters:
      event - The SlashCommandEvent 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-complete option is set.
      Parameters:
      event - The event to handle.
      See Also:
      • OptionData.setAutoComplete(boolean)
    • execute

      protected void execute(CommandEvent event)
      The main body method of a Command.
      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 via CommandClientBuilder.addCommand(Command) for it to work properly.
      Specified by:
      execute in class Command
      Parameters:
      event - The CommandEvent that triggered this Command
    • run

      public final void run(SlashCommandEvent event)
      Runs checks for the SlashCommand with the given SlashCommandEvent 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

      public boolean isOwner(SlashCommandEvent event, CommandClient client)
      Tests whether or not the User who triggered this event is an owner of the bot.
      Parameters:
      event - the event that triggered the command
      client - the command client for checking stuff
      Returns:
      true if the User is the Owner, else false
    • getClient

      @Deprecated @ForRemoval(deadline="2.0.0") public CommandClient getClient()
      Deprecated.
      This is now retrieved from SlashCommandEvent.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

      public List<net.dv8tion.jda.api.interactions.commands.build.OptionData> 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

      public SlashCommand[] getChildren()
      Gets the Command.children for the Command.
      Overrides:
      getChildren in class Command
      Returns:
      The children for the Command
    • getCooldownKey

      public String getCooldownKey(SlashCommandEvent event)
      Gets the proper cooldown key for this Command under the provided SlashCommandEvent.
      Parameters:
      event - The CommandEvent to generate the cooldown for.
      Returns:
      A String key to use when applying a cooldown.
    • getCooldownError

      public String getCooldownError(SlashCommandEvent event, int remaining, CommandClient client)
      Gets an error message for this Command under the provided SlashCommandEvent.
      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, else null.
    • getNameLocalization

      public Map<net.dv8tion.jda.api.interactions.DiscordLocale,String> getNameLocalization()
      Gets the specified localizations of slash command names.
      Returns:
      Slash command name localizations.
    • getDescriptionLocalization

      public Map<net.dv8tion.jda.api.interactions.DiscordLocale,String> getDescriptionLocalization()
      Gets the specified localizations of slash command descriptions.
      Returns:
      Slash command description localizations.