Runs every 15 min
Fork
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { set } from "https://esm.town/v/std/set?v=11";
import { discordSendDM } from "https://esm.town/v/vtdocs/discordSendDM";
import process from "node:process";
import { discordGetMembers } from "https://esm.town/v/vtdocs/discordGetMembers";
let { discordDMs } = await import("https://esm.town/v/vtdocs/discordDMs");
let { discordWelcomedMembers } = await import("https://esm.town/v/vtdocs/discordWelcomedMembers");
let { discordWelcomeBotStartedAt } = await import("https://esm.town/v/vtdocs/discordWelcomeBotStartedAt");
export const discordWelcomeBotCron = async () => {
// Only target new users going forwards from bot creation
if (discordWelcomeBotStartedAt === undefined) {
discordWelcomeBotStartedAt = Date.now();
}
// Don't message a user more than once
if (discordWelcomedMembers === undefined) {
discordWelcomedMembers = [];
}
// Store channels we've sent a DM to
// in case we want to forward replies to the bot
if (discordDMs === undefined) {
discordDMs = [];
}
const members = await discordGetMembers(
process.env.discordBot,
process.env.discordServerId,
);
await Promise.all(members.map(async (member) => {
const existingMember = (new Date(member.joined_at)).getTime() <
discordWelcomeBotStartedAt;
const alreadyWelcomed = discordWelcomedMembers.includes(
member.user.id,
);
if (existingMember || alreadyWelcomed) {
return;
}
const DMid = await discordSendDM(
process.env.discordBot,
member.user.id,
"👋 Welcome!",
);
// Store that we have messaged this user
discordWelcomedMembers.push(member.user.id);
if (DMid !== undefined)
discordDMs.push(DMid);
}));
await Promise.all([
set(
"discordWelcomeBotStartedAt",
discordWelcomeBotStartedAt,
),
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.