Public
Cron
Forked from stevekrouse/discordWelcomeBotCron
October 23, 2023
Runs every 1 hrs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
import { discordSendDM } from "https://esm.town/v/vtdocs/discordSendDM?v=6";
import process from "node:process";
import { discordFetch } from "https://esm.town/v/vtdocs/discordFetch?v=10";
let { discordDMs } = await import("https://esm.town/v/nthypes/discordDMs");
let { discordWelcomedMembers } = await import("https://esm.town/v/nthypes/discordWelcomedMembers");
let { discordWelcomeBotStartedAt } = await import("https://esm.town/v/nthypes/discordWelcomeBotStartedAt");
// Welcome new members to the Scriba Discord Server
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 discordGetMembers = async (
botToken: string,
serverId: number | string,
) => {
const allMembers = [];
// user.id is of type snowflake (too large to be represented as numbers)
let highestId = BigInt(0);
while (true) {
const result = await discordFetch(
botToken,
`/guilds/${serverId}/members?limit=1000&after=${highestId}`,
);
console.log(result);
if (result.length === 0) {
break;
}
console.log(result);
result.forEach((member) => {
const n = BigInt(member.user.id);
if (n > highestId) {
highestId = n;
}
});
allMembers.push(...result);
}
return allMembers;
};
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 to Scriba Discord! Everyone is excited to meet you!
Feel free to post anything Scriba related in <#1131391218979455041>
DM <@1132434359375372470> for anything private
The Scriba 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/nthypes.discordWelcomeBotCron
`,
);
// Store that we have messaged this user
discordWelcomedMembers.push(member.user.id);
if (DMid !== undefined)
discordDMs.push(DMid);
}));
};
// Forked from @stevekrouse.discordWelcomeBotCron
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!
October 23, 2023