October 23, 2023
Runs every 15 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
import { discordSendDM } from "https://esm.town/v/vtdocs/discordSendDM";
import process from "node:process";
import { discordFetch } from "https://esm.town/v/vtdocs/discordFetch";
import { discordDMs } from "https://esm.town/v/vtdocs/discordDMs";
export const discordWelcomeBotMsgForwarder = async ({ lastRunAt }: Interval) => {
if (discordDMs === undefined) {
throw `expected @me.discordDMs to be a string[] of channel ids`;
}
const repliesToBot = [];
for (const channelId of discordDMs) {
const messages = await discordFetch(
process.env.discordBot,
// Note: not using pagination here
// (we assume < 50 replies to the bot per each DM)
`/channels/${channelId}/messages?limit=50`,
);
repliesToBot.push(
...messages
// Ignore the welcome message
.filter((message) => message?.author?.bot !== true)
// Only forward new messages
.filter((message) => lastRunAt < new Date(message.timestamp))
// A little formatting
.map((message) =>
`${message.author.username}#${message.author.discriminator}: ${message.content}`
),
);
}
if (repliesToBot.length !== 0) {
await discordSendDM(
process.env.discordBot,
process.env.discordUserId,
repliesToBot.join("\n"),
);
}
};
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