# Porting an existing bot

> Moving a bot from another chat platform's interactions API to Zaun; what maps directly and what differs.

Zaun's interaction model uses the same shapes most chat platforms' bot APIs have converged on:
application commands with typed options, components, modals, deferred responses and follow-ups.
If you have written an interactions-based bot before, the concepts carry over. What changes is
where the bot runs.

## What maps directly

| Concept | In Zaun |
|---|---|
| Registering commands | TypeScript: `bot.command(builder, handler)`, or `setCommands`; Rust: return them from `Bot::commands`, or call `commands::set` |
| Command, user and message commands | TypeScript: `SlashCommandBuilder`, `ContextMenuCommandBuilder`; Rust: `SlashCommand::new`, `::user`, `::message` |
| Option types 1–11, choices, bounds, autocomplete | the same numbers and rules |
| Interaction types 2–5, callback types 4–9 | the same numbers |
| Components (types 1–14, 17–19, 21–23), button styles 1–5 | the same numbers |
| Message flags: suppress embeds 4, ephemeral 64, silent 4096, components V2 32768 | the same values |
| Limits: 25 options, 5 buttons per row, 40 V2 components, 3 s, 15 min | the same |

## Coming from a JavaScript bot library

`@zaun/bot` deliberately uses the names most JavaScript bot libraries share, so most code moves over
by changing imports and the entry point:

| You may know | In `@zaun/bot` |
|---|---|
| a client object you log in with a token | `const bot = new Bot()`; no token, no login: the host runs the bot |
| registering commands through a REST call | `bot.command(new SlashCommandBuilder()…, execute)`; registered at every start |
| an `interactionCreate` listener with `if (interaction.isChatInputCommand())` | the same listener and type guards, or per-command handlers in `bot.command` |
| `interaction.reply`, `deferReply`, `editReply`, `followUp`, `showModal`, `update`, `deferUpdate` | the same methods |
| `interaction.options.getString(..)`, `getSubcommand()`, `getFocused()` | the same |
| `interaction.fields.getTextInputValue(..)` | the same |
| `SlashCommandBuilder`, `EmbedBuilder`, `ButtonBuilder`, `ActionRowBuilder`, `StringSelectMenuBuilder`, `ModalBuilder`, `TextInputBuilder`, `ContainerBuilder`, … | the same names |
| `messageCreate`, `messageReactionAdd` and member-join events | `bot.on('messageCreate' \| 'messageReactionAdd' \| 'memberAdd', (payload, ctx) => …)`; ask for them in the manifest's `events` |
| `user.send(..)` for a direct message | `ctx.dm(memberId, ..)` or `bot.messages.dm(memberId, ..)` (capability `dms`) |
| setting the bot's presence or activity ("Listening to …") | `bot.presence.set('online', Activity.listening(..))`; custom status text with `bot.presence.setStatusText(..)` ([Status and presence](/docs/guide/bots/status-and-presence/)) |
| a voice library and an audio player | `bot.voice.join`, `play`, `enqueue`, … (capability `voice`; the host plays Opus) |
| your own database, `fetch`, `setTimeout` | `bot.storage`, `bot.http.fetch` (granted hosts only), `bot.timers` |
| `client.login(..)` at the end of the file | `export const { init, onEvent, onInteraction, onAutocomplete, onTimer, onVoiceEvent } = bot.exports();` |

## What differs

- **There's no hosting to run.** The bot is a `.zbot` package the community's owner installs, and it
  runs inside their host.
- **No tokens.** The bot's own connection is the credential; only that bot can answer its
  interactions, so there's nothing to leak.
- **Events are opt-in.** List the event groups or types you want in the manifest; reading message
  content is a capability every member can see.
- **Ids** are Zaun ids (base64url, 16 bytes), not integers.
- **Mentions are explicit.** Messages name their mentions; nothing is parsed from text.
- **Pictures are attachments, never URLs**, in embeds and components alike. Link buttons take
  `https://` only and open after the reader confirms.
- **Your own database and HTTP client** become storage (granted bytes) and HTTP fetch (granted
  hosts only); `setTimeout`/cron become timers. In TypeScript, `setTimeout` never fires.
- **One handler at a time** per bot instance, and state in fields may vanish on restart.
- **Not available:** premium buttons, entitlements, pictures on activities, polls, stickers, custom emoji in
  components (Unicode only), webhooks.
