Options and autocomplete
Option types
Section titled “Option types”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.
Reading values
Section titled “Reading values”const what = interaction.options.getString('what', true); // required: never nullconst minutes = interaction.options.getInteger('minutes') ?? 5;const loud = interaction.options.getBoolean('loud') ?? false;const who = interaction.options.getUser('who'); // the member, resolvedThe Options trait reads options by name:
let what = cmd.str("what").unwrap_or_default();let minutes = cmd.int("minutes").unwrap_or(5);let loud = cmd.bool("loud").unwrap_or(false);Autocomplete
Section titled “Autocomplete”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();Return the suggestions from on_autocomplete; the runtime answers with them.
use zaun_bot::prelude::*;use zaun_bot::types::OptionKind;
#[derive(Default)]struct Weather;
impl Bot for Weather { fn commands(&mut self, _: &Config) -> Vec<ApplicationCommand> { let city = CommandOptionBuilder::new(OptionKind::String, "city", "Which city") .required() .autocomplete(); vec![SlashCommand::new("weather", "Today's weather").option(city).build()] }
fn on_autocomplete(&mut self, _: Context, cmd: CommandData) -> Vec<Choice> { let typed = cmd.str("city").unwrap_or_default().to_lowercase(); ["Berlin", "Cairo", "Lagos", "Lima"] .into_iter() .filter(|c| c.to_lowercase().starts_with(&typed)) .map(|c| text_choice(c, c)) .collect() }
fn on_command(&mut self, ctx: Context, cmd: CommandData) { let city = cmd.str("city").unwrap_or("?"); let _ = ctx.reply(EmbedBuilder::new().title(format!("Weather in {city}")).description("☀️ 24 °C")); }}
export_bot!(Weather);