Runs every 1 min
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { blob } from "https://esm.town/v/std/blob?v=12";
import { discordGetMembers } from "https://esm.town/v/vtdocs/discordGetMembers?v=6";
import { discordSendDM } from "https://esm.town/v/vtdocs/discordSendDM?v=6";
// Welcome new members to the Val Town Discord
// Make your own welcome bot: https://docs.val.town/create-a-discord-welcome-bot
export const discordWelcomeBotCron = async () => {
let discordWelcomeBotStartedAt = await blob.getJSON("discordWelcomeBotStartedAt");
let discordWelcomedMembers = await blob.getJSON("discordWelcomedMembers");
let discordDMs = await blob.getJSON("discordDMs");
// 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(
Deno.env.get("discordBot"),
Deno.env.get("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(
Deno.env.get("discordBot"),
member.user.id,
`👋 Welcome to Val Town's Discord! Everyone is excited to meet you!
Feel free to post anything Val Town related in <#1020432421243592717>
Bugs in <#1063180396931592192>
DM <@420257368417239041> for anything private
The Val Town Team and community are very friendly,
but I am an antisocial bot that doesn't answer messages.
View my source code: https://www.val.town/v/stevekrouse.discordWelcomeBotCron
`,
);
// Store that we have messaged this user
discordWelcomedMembers.push(member.user.id);
if (DMid !== undefined)
discordDMs.push(DMid);
}));
await Promise.all([
blob.setJSON(
"discordWelcomeBotStartedAt",
discordWelcomeBotStartedAt,
),
blob.setJSON("discordDMs", discordDMs),
blob.setJSON(
"discordWelcomedMembers",
discordWelcomedMembers,
),
]);
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 2, 2024