Skip to content

Commit

Permalink
dont fetch all threads tons of times
Browse files Browse the repository at this point in the history
Signed-off-by: RedGuy12 <paul@reid-family.org>
  • Loading branch information
cobaltt7 committed Aug 27, 2023
1 parent c4d8b6b commit e73aaf3
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 24 deletions.
13 changes: 12 additions & 1 deletion common/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ChannelType, type NonThreadGuildBasedChannel } from "discord.js";
import {
ChannelType,
ForumChannel,
TextChannel,
type NonThreadGuildBasedChannel,
NewsChannel,
} from "discord.js";
import constants from "./constants.js";
import { client } from "strife.js";

Expand Down Expand Up @@ -112,3 +118,8 @@ export async function syncConfig() {
config.channels = newConfig.channels;
}
export default config;

const threads = await config.guild.channels.fetchActiveThreads();
export function getInitialChannelThreads(channel: ForumChannel | TextChannel | NewsChannel) {
return threads.threads.filter(({ parent }) => parent?.id === channel.id);
}
3 changes: 2 additions & 1 deletion modules/bot/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import constants from "../../common/constants.js";
import log, { getLoggingThread, LoggingEmojis, shouldLog } from "../logging/misc.js";
import { getBaseChannel, getMessageJSON } from "../../util/discord.js";
import { generateError } from "../logging/errors.js";
import { DATABASE_THREAD } from "../../common/database.js";

const databaseThread = await getLoggingThread("databases");
const databaseThread = await getLoggingThread(DATABASE_THREAD);
export default async function editMessage(interaction: MessageContextMenuCommandInteraction) {
if (
interaction.targetMessage.type !== MessageType.Default ||
Expand Down
9 changes: 6 additions & 3 deletions modules/games/tourney.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { userMention, type Snowflake } from "discord.js";
import config from "../../common/config.js";
import config, { getInitialChannelThreads } from "../../common/config.js";

const MESSAGE_HEADER = "## Pending Rounds";

const { threads } = (await config.channels.mod?.threads.fetch()) ?? {};
const tourneyThread = threads?.find(({ name }) => name === "Memory Match Tournament management");
const tourneyThread =
config.channels.mod &&
getInitialChannelThreads(config.channels.mod).find(
({ name }) => name === "Memory Match Tournament management",
);

export async function playNeeded(ids: [Snowflake, Snowflake]) {
const match = await getMatch(ids);
Expand Down
6 changes: 2 additions & 4 deletions modules/logging/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ThreadAutoArchiveDuration,
} from "discord.js";
import { getBaseChannel } from "../../util/discord.js";
import config from "../../common/config.js";
import config, { getInitialChannelThreads } from "../../common/config.js";
import { DATABASE_THREAD } from "../../common/database.js";
import constants from "../../common/constants.js";

Expand Down Expand Up @@ -105,10 +105,8 @@ export async function getLoggingThread(group?: LogGroup | typeof DATABASE_THREAD
if (!config.channels.modlogs) throw new ReferenceError("Cannot find logs channel");
if (!group) return config.channels.modlogs;

const threads = await config.channels.modlogs.threads.fetchActive();

return (
threads.threads.find((thread) => thread.name === group) ||
getInitialChannelThreads(config.channels.modlogs).find((thread) => thread.name === group) ||
(await config.channels.modlogs.threads.create({
name: group,
reason: "New logging thread",
Expand Down
7 changes: 4 additions & 3 deletions modules/modInterestForm.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { ButtonStyle, ComponentType, GuildMember, TextInputStyle, time } from "discord.js";
import { defineCommand, defineButton, defineModal } from "strife.js";
import config from "../common/config.js";
import config, { getInitialChannelThreads } from "../common/config.js";
import { getLevelForXp, getWeeklyXp, xpDatabase } from "./xp/misc.js";
import { EXPIRY_LENGTH, strikeDatabase } from "./punishments/misc.js";
import constants from "../common/constants.js";
import giveXp from "./xp/giveXp.js";

if (!config.channels.admin) throw new ReferenceError("Could not find admin channel");
const threads = await config.channels.admin.threads.fetchActive();
const thread =
threads.threads.find((thread) => thread.name === "Moderator Interest Forms") ||
getInitialChannelThreads(config.channels.admin).find(
(thread) => thread.name === "Moderator Interest Forms",
) ||
(await config.channels.admin.threads.create({
name: "Moderator Interest Forms",
reason: "For mod interest forms",
Expand Down
4 changes: 2 additions & 2 deletions modules/punishments/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Message,
type BaseMessageOptions,
} from "discord.js";
import Database from "../../common/database.js";
import Database, { DATABASE_THREAD } from "../../common/database.js";
import { GlobalUsersPattern, paginate } from "../../util/discord.js";
import { convertBase } from "../../util/numbers.js";
import { getLoggingThread } from "../logging/misc.js";
Expand All @@ -33,7 +33,7 @@ export const strikeDatabase = new Database<{
}>("strikes");
await strikeDatabase.init();

const databases = await (await getLoggingThread("databases")).messages.fetch({ limit: 100 });
const databases = await (await getLoggingThread(DATABASE_THREAD)).messages.fetch({ limit: 100 });
const { url } =
databases
.find((message) => message.attachments.first()?.name === "robotop_warns.json")
Expand Down
4 changes: 2 additions & 2 deletions modules/tickets/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default async function contactMods(
...data,
embeds: [details, ...(data.embeds ?? [])],
content:
category === MOD_CATEGORY || process.env.NODE_ENV === "development"
category === MOD_CATEGORY || process.env.NODE_ENV !== "production"
? ""
: config.roles.mod?.toString(),
allowedMentions: { parse: ["roles"] },
Expand All @@ -171,7 +171,7 @@ export default async function contactMods(
: thread.send({
embeds: [details],
content:
category === MOD_CATEGORY || process.env.NODE_ENV === "development"
category === MOD_CATEGORY || process.env.NODE_ENV !== "production"
? ""
: config.roles.mod?.toString(),
allowedMentions: { parse: ["roles"] },
Expand Down
20 changes: 12 additions & 8 deletions modules/tickets/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import {
type PrivateThreadChannel,
type Snowflake,
} from "discord.js";
import config from "../../common/config.js";
import config, { getInitialChannelThreads } from "../../common/config.js";

const initialThreads = await config.channels.tickets?.threads.fetchActive();
export const TICKETS_BY_MEMBER = Object.fromEntries(
initialThreads?.threads
.map((thread) => {
const id = getIdFromName(thread.name);
return [id, thread.type === ChannelType.PrivateThread ? thread : undefined] as const;
})
.filter((info): info is [Snowflake, PrivateThreadChannel | undefined] => !!info[0]) ?? [],
config.channels.tickets
? getInitialChannelThreads(config.channels.tickets)
.map((thread) => {
const id = getIdFromName(thread.name);
return [
id,
thread.type === ChannelType.PrivateThread ? thread : undefined,
] as const;
})
.filter((info): info is [Snowflake, PrivateThreadChannel | undefined] => !!info[0])
: [],
);

export const TICKET_CATEGORIES = [
Expand Down

0 comments on commit e73aaf3

Please sign in to comment.