Skip to content

External bots with zaun-client

Not every bot has to run inside the host. A program using zaun-client 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)
import { Client } from "zaun-client";
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");
});

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.

import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, CommandBuilder } from "zaun-client";
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" });
}
});

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.

The complete program is in Examples → Ping bot with zaun-client.