Skip to content

When someone joins, the bot sends them a welcome in a direct message and answers what they write there. It needs the members capability (to hear joins) and dms.

Source in zaun-bot: examples/ts/welcome-dm (TypeScript) and examples/rust/welcome-dm (Rust).

src/index.ts
// Welcomes every new member with a direct message, answers what they write back, and offers
// `/dm-me` to open the conversation on demand.
import { Bot, SlashCommandBuilder, ZaunError } from '@zaun/bot';
const bot = new Bot();
let greeted = 0;
bot.on('ready', (me) => bot.log.info(`welcome bot ${me.memberId} is up`));
bot.on('memberAdd', (member, ctx) => {
if (member.botId) return;
try {
ctx.dm(member.memberId, `Welcome, ${member.displayName}! Reply here if you have a question, or type \`rules\` to see them.`);
greeted += 1;
} catch (e) {
// The member turned DMs from bots off: respect it.
bot.log.info(`no welcome DM for ${member.memberId}: ${e instanceof ZaunError ? e.code : e}`);
}
});
bot.on('messageCreate', (message, ctx) => {
if (!message.isDM || ctx.isMe(message.authorId)) return;
const answer = message.content.trim().toLowerCase() === 'rules'
? '1. Be kind. 2. No spam. 3. Have fun.'
: 'Thanks! A moderator will read this soon.';
ctx.reply(message, answer);
});
bot.command(new SlashCommandBuilder().setName('dm-me').setDescription('Get a direct message from the welcome bot'), (i) => {
try {
bot.messages.dm(i.member.memberId, 'Hi! What can I help you with?');
i.reply({ content: 'Check your direct messages.', ephemeral: true });
} catch {
i.reply({ content: "I can't message you: you have direct messages from bots turned off.", ephemeral: true });
}
});
bot.command(new SlashCommandBuilder().setName('greeted').setDescription('How many members I welcomed'), (i) => {
i.reply({ content: `I welcomed ${greeted} member(s) since I started.`, ephemeral: true });
});
export const { init, onEvent, onInteraction, onAutocomplete, onTimer, onVoiceEvent } = bot.exports();
zaun-bot.toml
app_id = "org.zaun.examples.ts-welcome-dm"
version = "0.1.0"
name = "Welcome"
description = "Greets new members by direct message and answers their replies."
permissions = ["VIEW_CHANNEL", "SEND_MESSAGES"]
# `members` for member joins (needs the `members` capability), `dms` for the DM's own
# events, `messages` for the replies (DMs to a bot always arrive whole).
events = ["members", "dms", "messages"]
[capabilities]
members = true
dms = true
  • events asks for members, dms and messages; DMs to a bot always arrive whole, so it needs no message_content.
  • The member-join handler opens the DM and sends the welcome in one call (ctx.dm(..)). A member who turned DMs from bots off makes the call fail with dms_closed; the bot just logs it.
  • Message events in a DM are marked (isDM / is_dm), so the bot only answers there, never in public channels, and never itself.
Terminal window
git clone https://github.com/zaunchat/zaun-bot && cd zaun-bot/examples/ts/welcome-dm
npm install
zbot build && zbot pack --unsigned
zaun-host bot install --community <ID> *.zbot # on a host with allow_unsigned = true