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) |
Registering
Section titled “Registering”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).
Return your commands from Bot::commands. To change them while running, call
zaun_bot::commands::set.
fn commands(&mut self, _: &Config) -> Vec<ApplicationCommand> { vec![ SlashCommand::new("weather", "Today's weather").build(), SlashCommand::user("Show profile").build(), SlashCommand::message("Translate").build(), ]}Sub-commands and groups
Section titled “Sub-commands and groups”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().
use zaun_bot::prelude::*;
SlashCommand::new("remind", "Reminders") .sub_command( SubCommand::new("add", "Add a reminder") .option(CommandOptionBuilder::string("what", "What to remind").required()) .option(CommandOptionBuilder::integer("minutes", "In how many minutes").required().range(1.0, 1440.0)), ) .sub_command(SubCommand::new("list", "Your reminders")) .build();on_command then receives the path in CommandData.group and CommandData.sub_command:
fn on_command(&mut self, ctx: Context, cmd: CommandData) { match (cmd.name.as_str(), cmd.sub_command.as_deref()) { ("remind", Some("add")) => { /* cmd.str("what"), cmd.int("minutes") */ } ("remind", Some("list")) => { /* … */ } _ => {} }}Who can use a command
Section titled “Who can use a command”- Members need
USE_APPLICATION_COMMANDS(on@everyoneby default) andVIEW_CHANNEL. - Default member permissions additionally require those bits in the channel:
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)in TypeScript,.default_member_permissions(bits)in Rust.0makes 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.
Limits
Section titled “Limits”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.