# Using bots

> List a community's bots and commands, run commands, click components, submit modals, and (as the owner) install and manage bots.

Bots answer through the same connection as everything else, so an app uses them with a few calls.
This page is about an app **using** bots; to write one, see [Bots](/docs/guide/bots/quickstart/) (hosted)
or [External bots](/docs/guide/bots/external-bots/).

## Bots and their commands

The client keeps the community's bot commands and bot cards up to date, and loads them once the
community is ready.

**JavaScript:**

```js
client.on("appsReady", (community) => {
  for (const command of community.commands.values()) console.log(`/${command.name}`);
  for (const card of community.bots.values()) console.log(card.name, card.capabilities);
});
client.on("commandsUpdate", (community, botId) => { /* a bot changed its commands */ });
client.on("botUpdate", (community, card) => { /* installed, updated, re-granted, disabled */ });

const me = community.member(community.data.me);
const usable = [...community.commands.values()].filter((c) => me.canUse(c, channel));
```

A member's `isBot` and `botId` tell whether it is a bot; `community.bot(botId)` fetches its card.

**Rust:**

```rust
use zaun_client::Event;

for event in client.events() {
    match event {
        Event::AppsReady(community) => {
            for command in community.commands() { println!("/{}", command.name); }
            for card in community.bots() { println!("{} {:?}", card.name, card.capabilities); }
        }
        Event::CommandsUpdate { .. } | Event::BotUpdate { .. } => { /* refresh the picker */ }
        _ => {}
    }
}
```

`Member::is_bot()`, `Member::bot_id()` and `can_use(command, channel)` mirror the JavaScript calls.

Every member can read every bot's card: its permissions, whether it can read message content, the
hosts it may contact, its storage, voice and direct-message capabilities. Show it wherever a bot's
name appears (the BOT badge).

## Running commands, clicking, submitting

The host passes each use to the bot; the bot's answer arrives as an event (a normal message, or
one of the events below).

**JavaScript:**

```js
// /weather city: Cairo
await channel.invokeCommand(weather, [{ name: "city", type: 3, value: "Cairo" }]);

// A button or select on a bot's message
await message.click("vote:yes");

// Suggestions while typing: they come as `autocomplete`
await channel.autocomplete(weather, [{ name: "city", type: 3, value: "Ca", focused: true }]);
client.on("autocomplete", ({ choices }) => showSuggestions(choices));

// A modal the bot opened
client.on("modalOpen", async ({ channel, interaction, customId, components }) => {
  await channel.submitModal(interaction, customId, [{ custom_id: "subject", value: "Hello" }]);
});

// Replies only you see, and failures
client.on("interactionReply", ({ op, message }) => { /* ephemeral: created, updated, deleted */ });
client.on("interactionFail", ({ reason }) => { /* "timeout", "bot_error" or "bot_unavailable" */ });
```

**Rust:**

```rust
let interaction = channel.invoke_command(weather.command_id, options, None)?;
message.click("vote:yes", vec![])?;
channel.autocomplete(weather.command_id, focused_options)?;
channel.submit_modal(modal_interaction, "feedback", inputs)?;
```

The answers arrive as `Event::InteractionReply`, `Event::ModalOpen`, `Event::Autocomplete` and
`Event::InteractionFail`.

Using a command needs `USE_APPLICATION_COMMANDS` (on `@everyone` by default) and whatever default
permissions the command asks for.

## Installing and managing bots (owners)

Only the community's owner, or the host's operator with `zaun-host bot`, installs bots. Upload the
`.zbot` file as an attachment first, then install it; show the owner what it asks for before they
confirm.

**JavaScript:**

```js
const card = await community.installBot(attachmentId);   // grants: what the manifest asks, within the host's ceiling
await community.grantBot(card.bot_id, { capabilities: { message_content: false } });
await community.disableBot(card.bot_id);
await community.enableBot(card.bot_id);
await community.updateBot(card.bot_id, newAttachmentId); // wider requests wait for approval
await community.removeBot(card.bot_id);
await community.registerBot(member, "Helper");          // mark a member as an external bot
```

**Rust:**

`Community::install_bot`, `grant_bot`, `enable_bot`, `disable_bot`, `update_bot`, `remove_bot` and
`register_bot` mirror the JavaScript calls.
