|
| 1 | +import { Client, TextChannel } from "discord.js" |
| 2 | +import { Robot } from "hubot" |
| 3 | +import moment from "moment" |
| 4 | + |
| 5 | +const GUILD: string = process.env.GUILD ?? "" |
| 6 | + |
| 7 | +function weekdaysBefore(theMoment: any, days: any) { |
| 8 | + let newMoment = theMoment.clone() |
| 9 | + while(days > 0) { |
| 10 | + if (newMoment.isoWeekday() < 6) { |
| 11 | + days -= 1 |
| 12 | + } |
| 13 | + newMoment = newMoment.subtract(1, 'days') |
| 14 | + } |
| 15 | + return newMoment |
| 16 | +} |
| 17 | + |
| 18 | +export default async function webhookDiscord( |
| 19 | + discordClient: Client, |
| 20 | + robot: Robot, |
| 21 | +) { |
| 22 | + robot.hear(/blow everything up/, async (msg) => { |
| 23 | + const guild = await discordClient.guilds.fetch(GUILD) |
| 24 | + const channels = await guild.channels.fetch() |
| 25 | + const archiveThreshold = weekdaysBefore(moment(), 4) |
| 26 | + channels |
| 27 | + .filter((channel): channel is TextChannel => channel !== null && channel.isTextBased() && channel.viewable) |
| 28 | + .forEach(async channel => { |
| 29 | + const threads = await channel.threads.fetch() |
| 30 | + threads.threads.forEach(async thread => { |
| 31 | + const messages = await thread.messages.fetch({limit: 1}) |
| 32 | + |
| 33 | + const firstMessage = messages.first() |
| 34 | + const lastActivity = Math.max( |
| 35 | + firstMessage?.createdTimestamp ?? 0, |
| 36 | + thread.archiveTimestamp ?? 0 |
| 37 | + ) |
| 38 | + if (moment(lastActivity).isAfter(archiveThreshold)) { |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + // await thread.setArchived(true) |
| 43 | + msg.reply("We would archive", thread.name) |
| 44 | + }) |
| 45 | + }) |
| 46 | + }) |
| 47 | +} |
0 commit comments