Events
React to what happens in a server without commands: answer !ping, log reactions and members leaving.
Before you start
Create an application, install it in a test server and set three environment variables (see Getting started):
export ZRZUR_BOT_TOKEN=YOUR_BOT_TOKEN
export ZRZUR_API_URL=https://api.zrzur.com
export ZRZUR_WS_URL=wss://events.zrzur.comThen switch on the Message Content intent for the bot in the Developer Portal: without it the bot gets messages without their text, and connecting with it asked for but off fails. See Intents.
The code
// Gateway events: answer "!ping", log reactions and members leaving.
import { Client, Intents } from "@zrzur/sdk/bot";
const bot = new Client({
token: process.env.ZRZUR_BOT_TOKEN,
apiUrl: process.env.ZRZUR_API_URL,
wsUrl: process.env.ZRZUR_WS_URL,
// privileged: switch it on for the bot in the Developer Portal first
intents: [Intents.Default, Intents.MessageContent],
});
// Message: every new message in a channel the bot can see (its own are skipped)
bot.onMessage(async (message) => {
if (message.content !== "!ping") return;
await bot.typing(message.channel);
const started = Date.now();
const reply = await bot.messages.reply(message, "pong");
await bot.messages.edit(
message.channel,
reply._id,
`pong (${Date.now() - started} ms)`,
);
});
// MessageReact: someone added a reaction
bot.onReaction(({ messageId, userId, emoji }) => {
console.log(`reaction ${emoji} by ${userId} on ${messageId}`);
});
// Any other event by its type name
bot.on("ServerMemberLeave", (event) =>
console.log(`member ${event.user} left ${event.id}`),
);
await bot.login();
console.log("ready");
Run it with node events.mjs.
Try it
Send !ping in a channel the bot can see.
What it shows
Messagearrives for every new message the bot can see; skip your own and other bots' messages.BeginTypingshows the bot as typing.- Editing the reply shows the round-trip time.