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,
`/channels/${channelId}/messages?limit=50`,
);
repliesToBot.push(
...messages
.filter((message) => message?.author?.bot !== true)
.filter((message) => lastRunAt < new Date(message.timestamp))
.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"),
);
}
};