From 742f6ef31f402f32f29f764d418e947a8dd3326e Mon Sep 17 00:00:00 2001 From: Mama Naomi Date: Tue, 13 Feb 2024 17:03:32 -0800 Subject: [PATCH] feat: fetch all threads at once, no pagination (#152) --- src/commands/stats.ts | 16 +++---------- .../threads/aggregateUnansweredThreads.ts | 9 ++++--- src/modules/threads/aggregateWeeklyThreads.ts | 24 +++---------------- src/modules/threads/autorespondToThreads.ts | 24 +++---------------- src/utils/loadChannels.ts | 15 ++++++++++++ 5 files changed, 30 insertions(+), 58 deletions(-) diff --git a/src/commands/stats.ts b/src/commands/stats.ts index fe6beb2..4d3cd8a 100644 --- a/src/commands/stats.ts +++ b/src/commands/stats.ts @@ -30,7 +30,9 @@ export const stats: Command = { return; } - let rawArchived = await bot.cache.helpChannel.threads.fetchArchived(); + const rawArchived = await bot.cache.helpChannel.threads.fetchArchived({ + fetchAll: true, + }); const archived = rawArchived.threads; const active = (await bot.cache.helpChannel.threads.fetchActive()) .threads; @@ -38,18 +40,6 @@ export const stats: Command = { const sorted = threads .map((e) => e) .sort((a, b) => (b.createdTimestamp ?? 0) - (a.createdTimestamp ?? 0)); - let oldest = sorted.slice(-1)[0]; - - while (rawArchived.hasMore) { - rawArchived = await bot.cache.helpChannel.threads.fetchArchived({ - before: oldest.id, - }); - sorted.push(...rawArchived.threads.map((e) => e)); - sorted.sort( - (a, b) => (b.createdTimestamp ?? 0) - (a.createdTimestamp ?? 0) - ); - oldest = sorted.slice(-1)[0]; - } const total = sorted.length; const answeredArray = sorted.filter((thread) => diff --git a/src/modules/threads/aggregateUnansweredThreads.ts b/src/modules/threads/aggregateUnansweredThreads.ts index 26a3b03..d0772bd 100644 --- a/src/modules/threads/aggregateUnansweredThreads.ts +++ b/src/modules/threads/aggregateUnansweredThreads.ts @@ -10,12 +10,15 @@ import { stripLinks } from "../../utils/stripLinks"; */ export const aggregateUnansweredThreads = async (bot: ExtendedClient) => { try { - const archived = (await bot.cache.helpChannel.threads.fetchArchived()) - .threads; + const archived = ( + await bot.cache.helpChannel.threads.fetchArchived({ fetchAll: true }) + ).threads; const active = (await bot.cache.helpChannel.threads.fetchActive()).threads; const threads = [...archived.map((e) => e), ...active.map((e) => e)]; const unanswered = threads.filter( - (thread) => !thread.appliedTags.includes(bot.cache.answerTag) + (thread) => + !thread.appliedTags.includes(bot.cache.answerTag) && + !thread.appliedTags.includes(bot.cache.inactiveTag) ); const mapped = unanswered .map((e) => e) diff --git a/src/modules/threads/aggregateWeeklyThreads.ts b/src/modules/threads/aggregateWeeklyThreads.ts index 48eee9b..b473855 100644 --- a/src/modules/threads/aggregateWeeklyThreads.ts +++ b/src/modules/threads/aggregateWeeklyThreads.ts @@ -10,33 +10,15 @@ import { stripLinks } from "../../utils/stripLinks"; */ export const aggregateWeeklyThreads = async (bot: ExtendedClient) => { try { - const archived = (await bot.cache.helpChannel.threads.fetchArchived()) - .threads; + const archived = ( + await bot.cache.helpChannel.threads.fetchArchived({ fetchAll: true }) + ).threads; const active = (await bot.cache.helpChannel.threads.fetchActive()).threads; const threads = [...archived.map((e) => e), ...active.map((e) => e)]; const answered = threads .filter((thread) => thread.appliedTags.includes(bot.cache.answerTag)) .map((e) => e) .sort((a, b) => (b.createdTimestamp ?? 0) - (a.createdTimestamp ?? 0)); - let oldest = answered.slice(-1)[0]; - - while ( - oldest.createdTimestamp && - oldest.createdTimestamp > Date.now() - 1000 * 60 * 60 * 24 * 7 - ) { - const archived = ( - await bot.cache.helpChannel.threads.fetchArchived({ before: oldest.id }) - ).threads; - answered.push( - ...archived - .map((e) => e) - .filter((thread) => thread.appliedTags.includes(bot.cache.answerTag)) - ); - answered.sort( - (a, b) => (b.createdTimestamp ?? 0) - (a.createdTimestamp ?? 0) - ); - oldest = answered.slice(-1)[0]; - } const filtered = answered .filter( diff --git a/src/modules/threads/autorespondToThreads.ts b/src/modules/threads/autorespondToThreads.ts index 6fcc2fd..ecb6443 100644 --- a/src/modules/threads/autorespondToThreads.ts +++ b/src/modules/threads/autorespondToThreads.ts @@ -9,33 +9,15 @@ import { errorHandler } from "../../utils/errorHandler"; */ export const autorespondToThreads = async (bot: ExtendedClient) => { try { - const archived = (await bot.cache.helpChannel.threads.fetchArchived()) - .threads; + const archived = ( + await bot.cache.helpChannel.threads.fetchArchived({ fetchAll: true }) + ).threads; const active = (await bot.cache.helpChannel.threads.fetchActive()).threads; const threads = [...archived.map((e) => e), ...active.map((e) => e)]; const unanswered = threads .filter((thread) => !thread.appliedTags.includes(bot.cache.answerTag)) .map((e) => e) .sort((a, b) => (b.createdTimestamp ?? 0) - (a.createdTimestamp ?? 0)); - let oldest = unanswered.slice(-1)[0]; - - while ( - oldest.createdTimestamp && - oldest.createdTimestamp > Date.now() - 1000 * 60 * 60 * 24 * 30 - ) { - const archived = ( - await bot.cache.helpChannel.threads.fetchArchived({ before: oldest.id }) - ).threads; - unanswered.push( - ...archived - .map((e) => e) - .filter((thread) => thread.appliedTags.includes(bot.cache.answerTag)) - ); - unanswered.sort( - (a, b) => (b.createdTimestamp ?? 0) - (a.createdTimestamp ?? 0) - ); - oldest = unanswered.slice(-1)[0]; - } for (const thread of unanswered) { const lastMessage = (await thread.messages.fetch({ limit: 1 })).first(); diff --git a/src/utils/loadChannels.ts b/src/utils/loadChannels.ts index d75c74b..5d56bfe 100644 --- a/src/utils/loadChannels.ts +++ b/src/utils/loadChannels.ts @@ -3,6 +3,7 @@ import { ChannelType } from "discord.js"; import { ExtendedClient } from "../interfaces/ExtendedClient"; import { errorHandler } from "./errorHandler"; +import { logHandler } from "./logHandler"; /** * Loads the guild and channel IDs from the environment, fetches @@ -85,6 +86,20 @@ export const loadChannels = async (bot: ExtendedClient) => { lastSticky: "", }; } + + logHandler.debug("Loading help threads to cache."); + + const rawArchived = await bot.cache.helpChannel.threads.fetchArchived({ + fetchAll: true, + }); + const archived = rawArchived.threads; + const active = (await bot.cache.helpChannel.threads.fetchActive()).threads; + const threads = [...archived.map((e) => e), ...active.map((e) => e)]; + const sorted = threads + .map((e) => e) + .sort((a, b) => (b.createdTimestamp ?? 0) - (a.createdTimestamp ?? 0)); + + logHandler.debug(`Loaded ${sorted.length} threads.`); } catch (err) { await errorHandler(bot, "load channels utility", err); // shut down because the cache is essential.