-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
f296158
commit 7123a15
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
Check warning on line 7 in discord-scripts/blow-everything-up.ts GitHub Actions / lint
|
||
let newMoment = theMoment.clone() | ||
while(days > 0) { | ||
if (newMoment.isoWeekday() < 6) { | ||
days -= 1 | ||
} | ||
newMoment = newMoment.subtract(1, 'days') | ||
Check failure on line 13 in discord-scripts/blow-everything-up.ts GitHub Actions / lint
Check failure on line 13 in discord-scripts/blow-everything-up.ts GitHub Actions / lint
|
||
} | ||
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) | ||
}) | ||
}) | ||
}) | ||
} |