Skip to content

CreateSlashCommand

CreateSlashCommand creates the JSON payload needed to register a slash command with Discord. It does not send any network requests — use the ApplicationCommandManager or client.rest to register the payload.

import { CreateSlashCommand } from "lunibee";

Sets the command name (1–32 lowercase letters, numbers, underscores or hyphens; no spaces). Anything else throws a RangeError.

command.setName("ban");

Sets the command description (1–100 characters).

command.setDescription("Bans a member from the server.");

Sets the default member permissions required to see and use the command (a bigint, number or string, serialized as a string; null clears it).

import { Permission } from "lunibee";
command.setDefaultMemberPermissions(Permission.banMembers);

Where the command can be used: 0 guilds, 1 the bot’s DMs, 2 other DMs. Any other value throws a RangeError.

command.setContexts(0); // guilds only

Removed in 0.3.1, as Discord deprecated dm_permission. Use setContexts() instead: setDMPermission(false) becomes setContexts(0), and, for a guild-installed bot, setDMPermission(true) becomes setContexts(0, 1).

Where the command can be installed: 0 guild install, 1 user install.

Marks the command as age-restricted (nsfw defaults to true). setNsfw() is an alias.

Each option type has a corresponding method taking a configure callback. All return this. A command holds at most 25 options with unique names, and required options must come before optional ones (otherwise a RangeError is thrown).

Method Option Type
addStringOption(fn) Text input
addIntegerOption(fn) Whole number
addNumberOption(fn) Float
addBooleanOption(fn) True/false
addUserOption(fn) Discord user picker
addChannelOption(fn) Channel picker
addRoleOption(fn) Role picker
addMentionableOption(fn) User or role picker
addAttachmentOption(fn) File attachment
addSubcommand(fn) Subcommand
addSubcommandGroup(fn) Subcommand group
command.addStringOption(option =>
option
.setName("reason")
.setDescription("Why you're banning this user")
.setRequired(true)
.setMaxLength(500)
);

Returns the Discord API-compatible command payload object (type: 1).

Simple ping command

import { CreateSlashCommand } from "lunibee";
const ping = new CreateSlashCommand()
.setName("ping")
.setDescription("Replies with Pong!");
await client.application.commands.create(ping); // builders are accepted as they are

Ban command with options

const ban = new CreateSlashCommand()
.setName("ban")
.setDescription("Bans a member from the server")
.addUserOption(option =>
option.setName("target").setDescription("User to ban").setRequired(true)
)
.addStringOption(option =>
option.setName("reason").setDescription("Reason for the ban")
)
.addIntegerOption(option =>
option
.setName("delete_days")
.setDescription("Days of messages to delete (0-7)")
.setMinValue(0)
.setMaxValue(7)
);

Command with subcommands

const settings = new CreateSlashCommand()
.setName("settings")
.setDescription("Manage bot settings")
.addSubcommand(sub =>
sub
.setName("prefix")
.setDescription("Change the bot prefix")
.addStringOption(opt => opt.setName("value").setDescription("New prefix").setRequired(true))
)
.addSubcommand(sub =>
sub.setName("reset").setDescription("Reset all settings to defaults")
);

Each addXOption(fn) callback receives a new instance of one of these classes and must return it.

Class Option type Extra methods
CreateStringOption 3 addChoices({ name, value }), setMinLength, setMaxLength, setAutocomplete
CreateIntegerOption 4 addChoices, setMinValue, setMaxValue (integers only), setAutocomplete
CreateNumberOption 10 addChoices, setMinValue, setMaxValue, setAutocomplete
CreateBooleanOption 5 —
CreateUserOption 6 —
CreateChannelOption 7 addChannelTypes(...types)
CreateRoleOption 8 —
CreateMentionableOption 9 —
CreateAttachmentOption 11 —

All of them have setName, setDescription (1-100 characters), setRequired(required = true) and setAutocomplete(enabled = true). Choices must be strings of 1-100 characters or finite numbers (at most 25), choices cannot be added to an autocomplete option, and an option with choices refuses autocomplete, setMinLength / setMaxLength take 0-6000 / 1-6000, and setMinValue / setMaxValue throw a RangeError if the minimum would exceed the maximum.

addSubcommand(fn) adds a CreateSubcommand (type 1), which takes the same addXOption methods. addSubcommandGroup(fn) adds a CreateSubcommandGroup (type 2), whose only children are subcommands (at most 25, unique names).

const config = new CreateSlashCommand()
.setName("config")
.setDescription("Bot settings")
.addSubcommandGroup((group) =>
group
.setName("prefix")
.setDescription("Prefix settings")
.addSubcommand((sub) =>
sub
.setName("set")
.setDescription("Change the prefix")
.addStringOption((o) => o.setName("value").setDescription("New prefix").setRequired(true)),
),
);

In the handler, interaction.options.getSubcommandGroup() and getSubcommand() tell you which one ran.