|
| 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 guilds = discordClient.guilds.cache |
| 24 | + const guild = discordClient.guilds.cache.first() |
| 25 | + if (guild === undefined) { |
| 26 | + msg.reply("Whoops, no guilds.") |
| 27 | + return |
| 28 | + } else { |
| 29 | + msg.reply("Running it with", guild.name) |
| 30 | + } |
| 31 | + const channels = await guild.channels.fetch() |
| 32 | + const archiveThreshold = weekdaysBefore(moment(), 4) |
| 33 | + channels |
| 34 | + .filter((channel): channel is TextChannel => channel !== null && channel.isTextBased() && channel.viewable) |
| 35 | + .forEach(async channel => { |
| 36 | + const threads = await channel.threads.fetch() |
| 37 | + threads.threads.forEach(async thread => { |
| 38 | + const messages = await thread.messages.fetch({limit: 1}) |
| 39 | + |
| 40 | + const firstMessage = messages.first() |
| 41 | + const lastActivity = Math.max( |
| 42 | + firstMessage?.createdTimestamp ?? 0, |
| 43 | + thread.archiveTimestamp ?? 0 |
| 44 | + ) |
| 45 | + if (moment(lastActivity).isAfter(archiveThreshold)) { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + // await thread.setArchived(true) |
| 50 | + msg.reply("We would archive", thread.name) |
| 51 | + }) |
| 52 | + }) |
| 53 | + }) |
| 54 | +} |
0 commit comments