From 7123a15d03b4f23e8fdc88d7585be4973a5d6683 Mon Sep 17 00:00:00 2001 From: Antonio Salazar Cardozo Date: Fri, 3 Nov 2023 17:13:02 -0400 Subject: [PATCH] Add a command to blow everything up By this we mean archive all open threads that haven't been active in 4 weekdays. Currently we just log what threads we would archive. --- discord-scripts/blow-everything-up.ts | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 discord-scripts/blow-everything-up.ts diff --git a/discord-scripts/blow-everything-up.ts b/discord-scripts/blow-everything-up.ts new file mode 100644 index 00000000..e6eb53bf --- /dev/null +++ b/discord-scripts/blow-everything-up.ts @@ -0,0 +1,47 @@ +import { Client, TextChannel } from "discord.js" +import { Robot } from "hubot" +import moment from "moment" + +const GUILD: string = process.env.GUILD ?? "" + +function weekdaysBefore(theMoment: any, days: any) { + let newMoment = theMoment.clone() + while(days > 0) { + if (newMoment.isoWeekday() < 6) { + days -= 1 + } + newMoment = newMoment.subtract(1, 'days') + } + return newMoment +} + +export default async function webhookDiscord( + discordClient: Client, + robot: Robot, +) { + robot.hear(/blow everything up/, async (msg) => { + const guild = await discordClient.guilds.fetch(GUILD) + const channels = await guild.channels.fetch() + const archiveThreshold = weekdaysBefore(moment(), 4) + channels + .filter((channel): channel is TextChannel => channel !== null && channel.isTextBased() && channel.viewable) + .forEach(async channel => { + const threads = await channel.threads.fetch() + threads.threads.forEach(async thread => { + const messages = await thread.messages.fetch({limit: 1}) + + const firstMessage = messages.first() + const lastActivity = Math.max( + firstMessage?.createdTimestamp ?? 0, + thread.archiveTimestamp ?? 0 + ) + if (moment(lastActivity).isAfter(archiveThreshold)) { + return + } + + // await thread.setArchived(true) + msg.reply("We would archive", thread.name) + }) + }) + }) +}