# Options and autocomplete

> Command option types, choices, bounds, reading values, and suggesting values as members type.

## 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](/docs/guide/bots/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.

> **Tip: The host validates for you**
>
> Values are checked **before** the bot sees them: types, choices, bounds and required options. A bot
> never receives an integer out of range or a missing required option.
## Reading values

**TypeScript:**

```ts
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
```

**Rust:**

The `Options` trait reads options by name:

```rust
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

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.

**TypeScript:**

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

```ts

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();
```

**Rust:**

Return the suggestions from `on_autocomplete`; the runtime answers with them.

```rust
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);
```
