# External bots with zaun-client

> A bot that runs outside the host and joins like a member, using zaun-client in Rust or JavaScript.

Not every bot has to run inside the host. A program using [`zaun-client`](/docs/guide/client/installation/)
joins a community with an invite like any member and acts through the same API as an app: read
messages, reply, react, moderate, play audio in voice.

Compared with a hosted bot:

| | Hosted bot (zaun-bot) | External bot (zaun-client) |
|---|---|---|
| Runs | inside the host, as WebAssembly | anywhere: your machine, a server |
| Latency | in-process | every exchange crosses SimpleX |
| Sandbox | enforced by the host | none: it's your program |
| Installs | by the owner or operator | joins with an invite |
| Reads messages | only with `message_content` | whatever its roles let it see |
| Slash commands, components, modals | yes | yes: `setCommands`, the `interactionCreate` event and its `Interaction` |
| Direct messages | with `dms` | yes: `member.send(..)` |
| Voice | yes (`voice`, played by the host) | yes (`AudioPlayer`) |

## A text-command bot

**JavaScript:**

```js

const client = await Client.start({ name: "Ping bot", key: process.env.ZAUN_KEY });
client.on("messageCreate", async (message) => {
  if (!message.mine && message.content === "!ping") await message.reply("pong");
});
```

**Rust:**

```rust
use zaun_client::{Client, Event};

let client = Client::builder("./bot").name("Ping bot").key(std::env::var("ZAUN_BOT_KEY")?).start()?;
for event in client.events() {
    if let Event::MessageCreate(message) = event {
        if !message.is_mine() && message.content() == "!ping" {
            message.reply("pong")?;
        }
    }
}
```

The owner marks such a member as a bot (`registerBot`, or Settings → Bots in the app), so it gets a
BOT badge and a card like a hosted one. Once marked, it can register slash commands and answer
interactions.

## Slash commands, buttons and modals

**JavaScript:**

```js

client.on("appsReady", async (community) => {
  await community.setCommands([
    new CommandBuilder().setName("ping").setDescription("Check the bot is alive"),
  ]);
});

client.on("interactionCreate", async (interaction) => {
  if (interaction.commandName === "ping") {
    await interaction.reply({
      content: "pong",
      components: [new ActionRowBuilder().addComponents(
        new ButtonBuilder().setCustomId("again").setLabel("Ping again").setStyle(ButtonStyle.Primary))],
    });
  } else if (interaction.customId === "again") {
    await interaction.update({ content: "pong, again" });
  }
});
```

**Rust:**

```rust
use zaun_client::protocol::command::object::InteractionType;
use zaun_client::ui::{ActionRow, Button, CommandBuilder};
use zaun_client::{Event, Response};

for event in client.events() {
    match event {
        Event::AppsReady(community) => {
            community.set_commands(vec![CommandBuilder::slash("ping", "Check the bot is alive").build()])?;
        }
        Event::InteractionCreate(i) => match (i.kind(), i.command_name(), i.custom_id()) {
            (InteractionType::ApplicationCommand, Some("ping"), _) => { i.reply("pong")?; }
            (InteractionType::MessageComponent, _, Some("again")) => { /* i.respond(Response::update(..)) */ }
            _ => {}
        },
        _ => {}
    }
}
```

The complete Rust program is `zaun/examples/slash_bot.rs` in zaun-client; a JavaScript bot that
greets members by DM is `zaun/examples/dm_bot.mjs`.

## Resulting code

The complete program is in [Examples → Ping bot with zaun-client](/docs/examples/client-ping-bot/).
