Zrzur Developers

Welcome messages

Greets new members in a channel the server picks with /welcome-here.

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.com

Then switch on the Server Members intent for the bot in the Developer Portal: without it the bot never hears of new members, and connecting with it asked for but off fails. See Intents.

The code

// Welcome bot: `/welcome-here` picks the channel, new members are greeted
// there. Settings live in memory for the demo; store them in a database in a
// real bot.

import { ApiError, 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.ServerMembers],
});

const MANAGE_SERVER = 1n << 1n;
/** server id → channel id */
const welcomeChannel = new Map();

bot.command("welcome-here", (i) => {
  if ((BigInt(i.raw.user_permissions) & MANAGE_SERVER) !== MANAGE_SERVER)
    return i.reply({ content: "You need Manage Server.", ephemeral: true });
  welcomeChannel.set(i.serverId, i.channelId);
  return i.reply({
    content: "New members will be welcomed in this channel.",
    ephemeral: true,
  });
});

bot.onMemberJoin(async ({ serverId, userId }) => {
  const channel = welcomeChannel.get(serverId);
  if (!channel || userId === bot.user._id) return;
  try {
    await bot.send(channel, `Welcome <@${userId}>! 👋`);
  } catch (error) {
    // lost the permission to write there: stay quiet
    if (
      !(error instanceof ApiError && error.type === "MissingPermission")
    )
      throw error;
  }
});

await bot.login();
await bot.syncCommands([
  { name: "welcome-here", description: "Welcome new members in this channel" },
]);
console.log("ready");

Run it with node welcome.mjs.

Try it

Run /welcome-here in a channel (needs Manage Server), then invite someone.

What it shows