Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
feat: fetch all threads at once, no pagination (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mama Naomi authored Feb 14, 2024
1 parent e6a48c6 commit 742f6ef
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 58 deletions.
16 changes: 3 additions & 13 deletions src/commands/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,16 @@ 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;
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));
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) =>
Expand Down
9 changes: 6 additions & 3 deletions src/modules/threads/aggregateUnansweredThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 3 additions & 21 deletions src/modules/threads/aggregateWeeklyThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 3 additions & 21 deletions src/modules/threads/autorespondToThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions src/utils/loadChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 742f6ef

Please sign in to comment.