Class SlashCommand

java.lang.Object
com.jagrosh.jdautilities.command.Command
com.jagrosh.jdautilities.command.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 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:
  • 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

    • requiredRole

      @Deprecated protected String requiredRole
      Deprecated.
      This option is deprecated in favor of enabledRoles 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

      protected String[] 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 if defaultEnabled isn't false.
    • enabledUsers

      protected String[] 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 if defaultEnabled isn't false.
    • disabledRoles

      protected String[] 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 if defaultEnabled isn't true.
    • disabledUsers

      protected String[] 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 if defaultEnabled isn't true.
    • defaultEnabled

      protected boolean defaultEnabled
      Whether this command is disabled by default. If disabled, you must give yourself permission to use it.
      In order for enabledUsers and enabledRoles to work, this must be set to false.
      See Also:
    • 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.
    • guildId

      protected String guildId
      Deprecated.
      This will be removed in favor of CommandClientBuilder.forceGuildOnly(String). Please use that instead.
      The ID of the server you want guildOnly tied to. This means the slash command will only work and show up in the specified Guild. If this is null, guildOnly will still be processed, however an ephemeral message will be sent telling them to move.
      See Also:
    • 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
      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)
      The main body method of a SlashCommand.
      This is the "response" for a successful #run(CommandEvent).
      Parameters:
      event - The SlashCommandEvent that triggered this Command
    • 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(net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, CommandClient client)
      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
      client - The CommandClient for checks and stuff
    • isOwner

      public boolean isOwner(net.dv8tion.jda.api.events.interaction.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

      public CommandClient getClient()
      Gets the CommandClient.
      Returns:
      the CommandClient.
    • getGuildId

      public String getGuildId()
      Deprecated.
      This will be removed in favor of CommandClientBuilder.forceGuildOnly(String). Please use that instead.
      Gets the associated Guild ID for Guild Only command.
      Returns:
      the ID for the specific Guild
      See Also:
    • getEnabledRoles

      public String[] 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

      public String[] 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

      public String[] 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

      public String[] 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 enable roles or users 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

      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
    • 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

      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(net.dv8tion.jda.api.events.interaction.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(net.dv8tion.jda.api.events.interaction.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.