Skip to content

Commit

Permalink
Fetch latest message based on thread property
Browse files Browse the repository at this point in the history
`thread` object exposes `lastMessageId` which we can use to fetch the
latest messages.
  • Loading branch information
nkuba committed Apr 13, 2023
1 parent ef69b03 commit 136c630
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 136c630

Please sign in to comment.