Skip to content

Options and autocomplete

type Kind TypeScript builder / reader Rust value
3 string addStringOption / getString OptionValue::Text
4 integer (|x| < 2^53) addIntegerOption / getInteger OptionValue::Integer
5 boolean addBooleanOption / getBoolean OptionValue::Boolean
6 user addUserOption / getUser, getMember OptionValue::Id + resolved.members
7 channel addChannelOption / getChannel OptionValue::Id + resolved.channels
8 role addRoleOption / getRole OptionValue::Id + resolved.roles
9 mentionable addMentionableOption / getMentionable OptionValue::Id + a member or role in resolved
10 number addNumberOption / getNumber OptionValue::Number
11 attachment addAttachmentOption / getAttachment OptionValue::Id + resolved.attachments

Types 1 and 2 are sub-commands and groups (Slash commands).

Per option: required (required options come first), up to 25 choices (string, integer, number), minimum and maximum values, minimum and maximum lengths (0–6 000), channel types, autocomplete (not together with choices), and localized names and descriptions.

const what = interaction.options.getString('what', true); // required: never null
const minutes = interaction.options.getInteger('minutes') ?? 5;
const loud = interaction.options.getBoolean('loud') ?? false;
const who = interaction.options.getUser('who'); // the member, resolved

Mark an option as autocomplete, then return suggestions while the member types (at most 25). During autocomplete the focused option holds what was typed so far, unvalidated.

The third argument of bot.command is the autocomplete handler. Call respond before any await: the choices are taken when the handler returns.

import { Bot, EmbedBuilder, SlashCommandBuilder } from '@zaun/bot';
const bot = new Bot();
const cities = ['Berlin', 'Cairo', 'Lagos', 'Lima'];
bot.command(
new SlashCommandBuilder().setName('weather').setDescription("Today's weather")
.addStringOption((o) => o.setName('city').setDescription('Which city').setRequired(true).setAutocomplete()),
(interaction) => {
if (!interaction.isChatInputCommand()) return;
const city = interaction.options.getString('city', true);
interaction.reply({ embeds: [new EmbedBuilder().setTitle(`Weather in ${city}`).setDescription('☀️ 24 °C')] });
},
(interaction) => {
const typed = interaction.options.getFocused().toLowerCase();
interaction.respond(cities.filter((c) => c.toLowerCase().startsWith(typed)).map((c) => ({ name: c, value: c })));
},
);
export const { init, onEvent, onInteraction, onAutocomplete, onTimer, onVoiceEvent } = bot.exports();