SlashCommand

The SlashCommand class builds a Discord application (/) command definition. It's a thin, object-based wrapper around discord.js's SlashCommandBuilder; you describe the command as plain data, and SlashCommand does the chaining for you. The result is passed as data to client.slash().

Constructor

JavaScript
const { SlashCommand } = require("syntx.js");

new SlashCommand(options);

Options

OptionTypeRequiredDescription
namestringYesThe command name shown after /.
descriptionstringYesShown to users in Discord's command picker.
name_localizationsobjectNoPer-locale names, e.g. { "es-ES": "saludo" }.
description_localizationsobjectNoPer-locale descriptions.
optionsOption[]NoTop-level options. Cannot be combined with subcommands/groups.
subcommandsSubcommand[]NoA flat list of subcommands.
groupsGroup[]NoSubcommands organized into named groups.
scope"global" | "guild"No (default "global")Where registerSlashCommands() publishes the command.
nsfwbooleanNo (default false)Restricts the command to age-restricted channels.
integrationTypes("guild" | "user")[]NoWhere the app can be installed and still use this command.
contexts("guild" | "bot_dm" | "private_channel")[]NoWhere the command can be used.
defaultMemberPermissionsPermissionResolvableNoPermissions a member needs by default to see/use the command.
JavaScript
const ping = new SlashCommand({
  name: "ping",
  description: "Replies with the bot latency",
});

Warning

You cannot pass options together with subcommands or groups on the same command; Discord does not allow mixing top-level options with subcommands. Pick one.

Adding options

Push objects into options. Every option type shares name, description, and required (default false); some types add extra fields.

typeBuilder usedExtra fields
stringaddStringOptionchoices, min_length, max_length, autocomplete
integeraddIntegerOptionchoices, min_value, max_value, autocomplete
numberaddNumberOptionchoices, min_value, max_value, autocomplete
booleanaddBooleanOption
useraddUserOption
channeladdChannelOptionchannel_types
roleaddRoleOption
mentionableaddMentionableOption
attachmentaddAttachmentOption
JavaScript
new SlashCommand({
  name: "echo",
  description: "Repeats your message",
  options: [
    {
      name: "message",
      description: "Text to repeat",
      type: "string",
      required: true,
      max_length: 200,
    },
  ],
});

Tip

choices accepts plain values (["red", "green", "blue"]) or { name, value } objects when the label shown to users should differ from the value your code receives.

Warning

Setting autocomplete: true on an option means Discord will call your command's autocomplete handler instead of using choices; don't set both on the same option.

Subcommands

JavaScript
new SlashCommand({
  name: "role",
  description: "Manage roles",
  subcommands: [
    {
      name: "add",
      description: "Add a role to a member",
      options: [
        { name: "user", description: "The member", type: "user", required: true },
        { name: "role", description: "The role to add", type: "role", required: true },
      ],
    },
    {
      name: "remove",
      description: "Remove a role from a member",
      options: [
        { name: "user", description: "The member", type: "user", required: true },
        { name: "role", description: "The role to remove", type: "role", required: true },
      ],
    },
  ],
});

Note

Inside execute, read which subcommand was used with interaction.options.getSubcommand().

Subcommand groups

For larger commands, organize subcommands into named groups (/settings welcome enable, /settings welcome channel, and so on):

JavaScript
new SlashCommand({
  name: "settings",
  description: "Configure the server",
  groups: [
    {
      name: "welcome",
      description: "Welcome message settings",
      subcommands: [
        { name: "enable", description: "Turn the welcome message on" },
        {
          name: "channel",
          description: "Set the welcome channel",
          options: [
            { name: "channel", description: "Target channel", type: "channel", required: true },
          ],
        },
      ],
    },
  ],
});

Scope: global vs guild

ScopePropagationWhere it's used
"global" (default)Up to ~1 hourEvery server (and DMs, if contexts allows it) the bot is in
"guild"InstantA single server, passed as guildId to registerSlashCommands()
JavaScript
new SlashCommand({
  name: "debug",
  description: "Developer-only debug command",
  scope: "guild",
});

Tip

Keep commands you're actively building set to scope: "guild" for instant feedback while testing, then switch to "global" once they're ready to ship.

Registering it

A SlashCommand instance only describes the command; pass it to client.slash() and then call client.registerSlashCommands() to publish it:

JavaScript
client.slash({
  data: ping,
  execute: async (interaction) => {
    await interaction.reply("Pong!");
  },
});

await client.registerSlashCommands({ showLoad: true });

toJSON()

Returns the raw payload discord.js sends to the Discord API (SlashCommandBuilder#toJSON()). registerSlashCommands() calls this internally; you'll rarely need it yourself, but it's useful for inspecting exactly what will be sent.

JavaScript
console.log(ping.toJSON());