The slash command system provides builders for creating Discord application commands with full support for options, subcommands, localization, and permissions.
Main builder for creating slash commands.
class SlashCommandBuilder {
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly options: ToAPIApplicationCommandOptions[];
readonly contexts?: InteractionContextType[];
readonly default_member_permissions?: Permissions | bigint | number | null;
readonly dm_permission?: boolean;
readonly default_permission?: boolean;
readonly nsfw?: boolean;
readonly integration_types?: ApplicationIntegrationType[];
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalization(locale: LocaleString, localizedName: string | null): this;
setNameLocalizations(localizations: LocalizationMap | null): this;
setDescriptionLocalization(locale: LocaleString, localizedDescription: string | null): this;
setDescriptionLocalizations(localizations: LocalizationMap | null): this;
setDefaultMemberPermissions(permissions: Permissions | bigint | number | null | undefined): this;
setDMPermission(enabled: boolean | null | undefined): this;
setDefaultPermission(value: boolean): this;
setNSFW(nsfw?: boolean): this;
setContexts(...contexts: InteractionContextType[]): this;
setIntegrationTypes(...integrationTypes: ApplicationIntegrationType[]): this;
// Option methods
addStringOption(input: (builder: SlashCommandStringOption) => SlashCommandStringOption): this;
addIntegerOption(input: (builder: SlashCommandIntegerOption) => SlashCommandIntegerOption): this;
addBooleanOption(input: (builder: SlashCommandBooleanOption) => SlashCommandBooleanOption): this;
addUserOption(input: (builder: SlashCommandUserOption) => SlashCommandUserOption): this;
addChannelOption(input: (builder: SlashCommandChannelOption) => SlashCommandChannelOption): this;
addRoleOption(input: (builder: SlashCommandRoleOption) => SlashCommandRoleOption): this;
addMentionableOption(input: (builder: SlashCommandMentionableOption) => SlashCommandMentionableOption): this;
addNumberOption(input: (builder: SlashCommandNumberOption) => SlashCommandNumberOption): this;
addAttachmentOption(input: (builder: SlashCommandAttachmentOption) => SlashCommandAttachmentOption): this;
// Subcommand methods
addSubcommand(input: (subcommand: SlashCommandSubcommandBuilder) => SlashCommandSubcommandBuilder): this;
addSubcommandGroup(input: (subcommandGroup: SlashCommandSubcommandGroupBuilder) => SlashCommandSubcommandGroupBuilder): this;
toJSON(): RESTPostAPIApplicationCommandsJSONBody;
}Builder for individual subcommands within a slash command.
class SlashCommandSubcommandBuilder {
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly options: ToAPIApplicationCommandOptions[];
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalization(locale: LocaleString, localizedName: string | null): this;
setNameLocalizations(localizations: LocalizationMap | null): this;
setDescriptionLocalization(locale: LocaleString, localizedDescription: string | null): this;
setDescriptionLocalizations(localizations: LocalizationMap | null): this;
addStringOption(input: (builder: SlashCommandStringOption) => SlashCommandStringOption): this;
addIntegerOption(input: (builder: SlashCommandIntegerOption) => SlashCommandIntegerOption): this;
addBooleanOption(input: (builder: SlashCommandBooleanOption) => SlashCommandBooleanOption): this;
addUserOption(input: (builder: SlashCommandUserOption) => SlashCommandUserOption): this;
addChannelOption(input: (builder: SlashCommandChannelOption) => SlashCommandChannelOption): this;
addRoleOption(input: (builder: SlashCommandRoleOption) => SlashCommandRoleOption): this;
addMentionableOption(input: (builder: SlashCommandMentionableOption) => SlashCommandMentionableOption): this;
addNumberOption(input: (builder: SlashCommandNumberOption) => SlashCommandNumberOption): this;
addAttachmentOption(input: (builder: SlashCommandAttachmentOption) => SlashCommandAttachmentOption): this;
toJSON(): APIApplicationCommandSubcommandOption;
}Builder for subcommand groups containing multiple subcommands.
class SlashCommandSubcommandGroupBuilder {
readonly name: string;
readonly description: string;
readonly name_localizations?: LocalizationMap;
readonly description_localizations?: LocalizationMap;
readonly options: SlashCommandSubcommandBuilder[];
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalization(locale: LocaleString, localizedName: string | null): this;
setNameLocalizations(localizations: LocalizationMap | null): this;
setDescriptionLocalization(locale: LocaleString, localizedDescription: string | null): this;
setDescriptionLocalizations(localizations: LocalizationMap | null): this;
addSubcommand(input: (subcommand: SlashCommandSubcommandBuilder) => SlashCommandSubcommandBuilder): this;
toJSON(): APIApplicationCommandSubcommandGroupOption;
}String input option with optional choices, autocomplete, min/max length.
class SlashCommandStringOption {
readonly type: ApplicationCommandOptionType.String;
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly required?: boolean;
readonly autocomplete?: boolean;
readonly choices?: APIApplicationCommandOptionChoice<string>[];
readonly min_length?: number;
readonly max_length?: number;
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalizations(localizations: LocalizationMap): this;
setDescriptionLocalizations(localizations: LocalizationMap): this;
setRequired(required?: boolean): this;
setAutocomplete(autocomplete: boolean): this;
addChoices(...choices: APIApplicationCommandOptionChoice<string>[]): this;
setChoices(...choices: APIApplicationCommandOptionChoice<string>[]): this;
setMinLength(minLength: number): this;
setMaxLength(maxLength: number): this;
toJSON(): APIApplicationCommandStringOption;
}Integer input option with optional choices, autocomplete, min/max values.
class SlashCommandIntegerOption {
readonly type: ApplicationCommandOptionType.Integer;
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly required?: boolean;
readonly autocomplete?: boolean;
readonly choices?: APIApplicationCommandOptionChoice<number>[];
readonly min_value?: number;
readonly max_value?: number;
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalizations(localizations: LocalizationMap): this;
setDescriptionLocalizations(localizations: LocalizationMap): this;
setRequired(required?: boolean): this;
setAutocomplete(autocomplete: boolean): this;
addChoices(...choices: APIApplicationCommandOptionChoice<number>[]): this;
setChoices(...choices: APIApplicationCommandOptionChoice<number>[]): this;
setMinValue(minValue: number): this;
setMaxValue(maxValue: number): this;
toJSON(): APIApplicationCommandIntegerOption;
}Number (decimal) input option with optional choices, autocomplete, min/max values.
class SlashCommandNumberOption {
readonly type: ApplicationCommandOptionType.Number;
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly required?: boolean;
readonly autocomplete?: boolean;
readonly choices?: APIApplicationCommandOptionChoice<number>[];
readonly min_value?: number;
readonly max_value?: number;
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalizations(localizations: LocalizationMap): this;
setDescriptionLocalizations(localizations: LocalizationMap): this;
setRequired(required?: boolean): this;
setAutocomplete(autocomplete: boolean): this;
addChoices(...choices: APIApplicationCommandOptionChoice<number>[]): this;
setChoices(...choices: APIApplicationCommandOptionChoice<number>[]): this;
setMinValue(minValue: number): this;
setMaxValue(maxValue: number): this;
toJSON(): APIApplicationCommandNumberOption;
}Boolean true/false option.
class SlashCommandBooleanOption {
readonly type: ApplicationCommandOptionType.Boolean;
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly required?: boolean;
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalizations(localizations: LocalizationMap): this;
setDescriptionLocalizations(localizations: LocalizationMap): this;
setRequired(required?: boolean): this;
toJSON(): APIApplicationCommandBooleanOption;
}User selection option.
class SlashCommandUserOption {
readonly type: ApplicationCommandOptionType.User;
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly required?: boolean;
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalizations(localizations: LocalizationMap): this;
setDescriptionLocalizations(localizations: LocalizationMap): this;
setRequired(required?: boolean): this;
toJSON(): APIApplicationCommandUserOption;
}Channel selection option with optional channel type restrictions.
class SlashCommandChannelOption {
readonly type: ApplicationCommandOptionType.Channel;
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly required?: boolean;
readonly channel_types?: ChannelType[];
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalizations(localizations: LocalizationMap): this;
setDescriptionLocalizations(localizations: LocalizationMap): this;
setRequired(required?: boolean): this;
addChannelTypes(...channelTypes: ChannelType[]): this;
setChannelTypes(...channelTypes: ChannelType[]): this;
toJSON(): APIApplicationCommandChannelOption;
}Role selection option.
class SlashCommandRoleOption {
readonly type: ApplicationCommandOptionType.Role;
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly required?: boolean;
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalizations(localizations: LocalizationMap): this;
setDescriptionLocalizations(localizations: LocalizationMap): this;
setRequired(required?: boolean): this;
toJSON(): APIApplicationCommandRoleOption;
}Mentionable (user or role) selection option.
class SlashCommandMentionableOption {
readonly type: ApplicationCommandOptionType.Mentionable;
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly required?: boolean;
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalizations(localizations: LocalizationMap): this;
setDescriptionLocalizations(localizations: LocalizationMap): this;
setRequired(required?: boolean): this;
toJSON(): APIApplicationCommandMentionableOption;
}File attachment option.
class SlashCommandAttachmentOption {
readonly type: ApplicationCommandOptionType.Attachment;
readonly name: string;
readonly name_localizations?: LocalizationMap;
readonly description: string;
readonly description_localizations?: LocalizationMap;
readonly required?: boolean;
constructor();
setName(name: string): this;
setDescription(description: string): this;
setNameLocalizations(localizations: LocalizationMap): this;
setDescriptionLocalizations(localizations: LocalizationMap): this;
setRequired(required?: boolean): this;
toJSON(): APIApplicationCommandAttachmentOption;
}import { SlashCommandBuilder } from "@discordjs/builders";
const pingCommand = new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!');const banCommand = new SlashCommandBuilder()
.setName('ban')
.setDescription('Ban a user from the server')
.addUserOption(option =>
option
.setName('target')
.setDescription('The user to ban')
.setRequired(true)
)
.addStringOption(option =>
option
.setName('reason')
.setDescription('The reason for the ban')
.setRequired(false)
);const configCommand = new SlashCommandBuilder()
.setName('config')
.setDescription('Server configuration commands')
.addSubcommandGroup(group =>
group
.setName('settings')
.setDescription('Server settings configuration')
.addSubcommand(subcommand =>
subcommand
.setName('prefix')
.setDescription('Set the bot prefix')
.addStringOption(option =>
option
.setName('value')
.setDescription('The new prefix')
.setRequired(true)
.setMaxLength(5)
)
)
);const weatherCommand = new SlashCommandBuilder()
.setName('weather')
.setDescription('Get weather information')
.addStringOption(option =>
option
.setName('location')
.setDescription('Select a location')
.setRequired(true)
.addChoices(
{ name: 'New York', value: 'ny' },
{ name: 'London', value: 'london' },
{ name: 'Tokyo', value: 'tokyo' }
)
);type ToAPIApplicationCommandOptions =
| APIApplicationCommandSubcommandOption
| APIApplicationCommandSubcommandGroupOption
| APIApplicationCommandBasicOption;
interface LocalizationMap {
[locale: string]: string;
}
interface APIApplicationCommandOptionChoice<ValueType = string | number> {
name: string;
name_localizations?: LocalizationMap;
value: ValueType;
}