Skip to content

Slash commands

Three kinds of application command:

Kind type Shown as TypeScript Rust
Slash command 1 CHAT_INPUT /name options… in the composer new SlashCommandBuilder() SlashCommand::new(name, description)
User command 2 USER Apps → name, on a member new ContextMenuCommandBuilder().setType(ApplicationCommandType.User) SlashCommand::user(name)
Message command 3 MESSAGE Apps → name, on a message new ContextMenuCommandBuilder().setType(ApplicationCommandType.Message) SlashCommand::message(name)

The SDK registers your commands every time the bot starts, as a bulk overwrite: a command keeps its id as long as its name stays.

Each bot.command(builder, execute) registers one command and its handler:

import { ApplicationCommandType, Bot, ContextMenuCommandBuilder, SlashCommandBuilder } from '@zaun/bot';
const bot = new Bot();
bot.command(new SlashCommandBuilder().setName('weather').setDescription("Today's weather"), (interaction) => {
interaction.reply('☀️ 24 °C');
});
bot.command(new ContextMenuCommandBuilder().setName('Show profile').setType(ApplicationCommandType.User), (interaction) => {
if (!interaction.isUserContextMenuCommand()) return;
interaction.reply({ content: `That is ${interaction.targetMember?.displayName}`, ephemeral: true });
});
bot.command(new ContextMenuCommandBuilder().setName('Quote').setType(ApplicationCommandType.Message), (interaction) => {
if (!interaction.isMessageContextMenuCommand()) return;
interaction.reply(`> ${interaction.targetMessage?.content ?? ''}`);
});

To change commands while running, call setCommands([...]) (exported by @zaun/bot).

bot.command(
new SlashCommandBuilder().setName('remind').setDescription('Reminders')
.addSubcommand((s) => s.setName('add').setDescription('Add a reminder')
.addStringOption((o) => o.setName('what').setDescription('What to remind').setRequired(true))
.addIntegerOption((o) => o.setName('minutes').setDescription('In how many minutes').setRequired(true).setMinValue(1).setMaxValue(1440)))
.addSubcommand((s) => s.setName('list').setDescription('Your reminders')),
(interaction) => {
if (!interaction.isChatInputCommand()) return;
switch (interaction.options.getSubcommand()) {
case 'add': {
const what = interaction.options.getString('what', true);
const minutes = interaction.options.getInteger('minutes', true);
interaction.reply({ content: `I'll remind you of "${what}" in ${minutes} min.`, ephemeral: true });
break;
}
case 'list':
interaction.reply({ content: 'No reminders yet.', ephemeral: true });
break;
}
},
);

Groups use addSubcommandGroup(..); read the path with options.getSubcommandGroup() and options.getSubcommand().

  • Members need USE_APPLICATION_COMMANDS (on @everyone by default) and VIEW_CHANNEL.
  • Default member permissions additionally require those bits in the channel: .setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages) in TypeScript, .default_member_permissions(bits) in Rust. 0 makes a command owner-only.
  • Two bots may register the same name; clients show the bot’s name beside each command.
  • In a direct message, only that bot’s own commands work.

100 slash, 15 user and 15 message commands per bot; up to 25 options and 25 choices; names 1–32 characters (slash commands: lowercase letters, digits, - and _); descriptions 1–100; at most 8 000 characters of text per command.