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";
import { randomize } from "https://esm.town/v/stevekrouse/randomize";
import { discordDMs } from "https://esm.town/v/stevekrouse/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 randomize(
discordDMs.slice(0, 100),
)
) {
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"),
);
}
};