Ping (hosted bot)
The smallest bot there is: one slash command, one reply. It asks only for VIEW_CHANNEL and SEND_MESSAGES, with no message content, no network and no storage.
Source in zaun-bot: examples/ts/ping (TypeScript) and examples/rust/ping (Rust).
The code
Section titled “The code”// `/ping` → "pong", in TypeScript. Shows "Listening to /ping" as its activity.import { Activity, Bot, SlashCommandBuilder } from '@zaun/bot';
const bot = new Bot();
bot.on('init', () => bot.presence.set('online', Activity.listening('/ping')));
bot.command( new SlashCommandBuilder().setName('ping').setDescription('Check that the bot is alive'), (interaction) => { interaction.reply('pong 🏓'); },);
export const { init, onEvent, onInteraction, onAutocomplete, onTimer, onVoiceEvent } = bot.exports();//! `/ping` → "pong", with the round trip the bot saw. Shows "Listening to /ping" as its//! activity.
use zaun_bot::prelude::*;use zaun_bot::presence;
#[derive(Default)]struct Ping;
impl Bot for Ping { fn on_start(&mut self, _: &Config) { presence::set(Status::Online, Activity::listening("/ping")); }
fn commands(&mut self, _: &Config) -> Vec<ApplicationCommand> { vec![SlashCommand::new("ping", "Check that the bot is alive").build()] }
fn on_command(&mut self, ctx: Context, command: CommandData) { if command.name == "ping" && let Err(e) = ctx.reply("pong 🏓") { log_info!("reply refused: {}", e.code); } }}
export_bot!(Ping);The manifest
Section titled “The manifest”app_id = "org.zaun.examples.ts-ping"version = "0.1.0"name = "Ping"description = "Answers /ping with pong."permissions = ["VIEW_CHANNEL", "SEND_MESSAGES"]
[capabilities]# Nothing beyond interactions: no message content, no network, no storage.app_id = "org.zaun.examples.ping"version = "0.1.0"name = "Ping"description = "Answers /ping with pong."permissions = ["VIEW_CHANNEL", "SEND_MESSAGES"]
[capabilities]# Nothing beyond interactions: no message content, no network, no storage.How it works
Section titled “How it works”- The command is registered every time the bot starts (
bot.command(..)in TypeScript,Bot::commandsin Rust). - When the instance starts, it sets its activity: members see “Listening to /ping” next to it (Status and presence). Setting its own status needs no capability.
- The handler answers with a reply. A refused reply (for example, the bot lost
SEND_MESSAGESin that channel) is logged (TypeScript: theerrorevent and the host log; Rust:log_info!).
Run it
Section titled “Run it”git clone https://github.com/zaunchat/zaun-bot && cd zaun-bot/examples/ts/pingnpm installzbot build && zbot pack --unsignedzaun-host bot install --community <ID> *.zbot # on a host with allow_unsigned = truegit clone https://github.com/zaunchat/zaun-bot && cd zaun-bot/examples/rust/pingzbot build && zbot pack --unsignedzaun-host bot install --community <ID> *.zbot # on a host with allow_unsigned = true