Skip to content

Commit

Permalink
Merge pull request #4 from akena-engineering/archive-threads
Browse files Browse the repository at this point in the history
Fetch latest message based on thread property
  • Loading branch information
thevops authored Apr 13, 2023
2 parents 566dd76 + 136c630 commit b684bd9
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cron/thread-lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,21 @@ module.exports = {
const channels = await guild.channels.fetch()
const longRunningThreadIds = (await read("long-running-thread-ids")) || {}
const archiveThreshold = weekdaysBefore(moment(), 4)

channels
.filter((channel) => channel.isText() && 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 lastMessage = await thread.messages.fetch(
thread.lastMessageId
)

const lastActivity = Math.max(
(messages.first() && messages.first().createdTimestamp) || 0,
lastMessage?.createdTimestamp ?? 0,
thread.archiveTimestamp
)

if (moment(lastActivity).isAfter(archiveThreshold)) {
return
}
Expand Down

0 comments on commit b684bd9

Please sign in to comment.