From d4bb9e2584b32c4dbc733f1674b510722f4803bc Mon Sep 17 00:00:00 2001 From: RedGuy12 Date: Sun, 27 Aug 2023 18:30:37 -0500 Subject: [PATCH] fix srp Signed-off-by: RedGuy12 --- modules/board/misc.ts | 49 +++++++++++++++++++++++++-------------- modules/reminders/send.ts | 25 +++++++++++++------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/modules/board/misc.ts b/modules/board/misc.ts index aa14b611e..39ed7c911 100644 --- a/modules/board/misc.ts +++ b/modules/board/misc.ts @@ -7,6 +7,7 @@ import { MessageType, type Snowflake, type TextBasedChannel, + BaseChannel, } from "discord.js"; import config from "../../common/config.js"; import Database from "../../common/database.js"; @@ -30,6 +31,7 @@ export const boardDatabase = new Database<{ }>("board"); await boardDatabase.init(); +const COUNTS = { scradd: 2, admins: 2, mods: 3, misc: 4, default: 6, memes: 8, info: 12 }; /** * Determines the board reaction count for a channel. * @@ -37,37 +39,48 @@ await boardDatabase.init(); * * @returns The reaction count. */ -export function boardReactionCount(channel?: TextBasedChannel): number { - const COUNTS = { scradd: 2, admins: 2, mods: 3, misc: 4, default: 6, memes: 8, info: 12 }; - +export function boardReactionCount(channel?: TextBasedChannel): number; +export function boardReactionCount(channel: { id: Snowflake }): number | undefined; +export function boardReactionCount(channel?: TextBasedChannel | { id: Snowflake }) { if (process.env.NODE_ENV !== "production") return COUNTS.scradd; + if (!channel) return COUNTS.default; + + if (channel.id === config.channels.updates?.id) return COUNTS.info; - if (channel?.id === config.channels.updates?.id) return COUNTS.info; + const baseCount = baseReactionCount(channel.id); + if (!(channel instanceof BaseChannel)) return baseCount; + + if (!channel.isTextBased()) return COUNTS.misc; const baseChannel = getBaseChannel(channel); - if (!baseChannel || baseChannel.isDMBased()) return COUNTS.default; + if (!baseChannel || baseChannel.isDMBased()) return COUNTS.misc; if (baseChannel.isVoiceBased()) return COUNTS.misc; return ( - { - [config.channels.tickets?.id || ""]: COUNTS.mods, - [config.channels.admin?.id || ""]: COUNTS.admins, - "853256939089559583": COUNTS.misc, // #ba-doosters - "869662117651955802": COUNTS.misc, // #devs-only - "811065897057255424": COUNTS.memes, // #memes - "806609527281549312": COUNTS.memes, // #collabs-and-ideas - "806656240129671188": COUNTS.memes, // #showcase - [config.channels.advertise?.id || ""]: COUNTS.memes, - "939350305311715358": COUNTS.mods, // #modmail - "894314668317880321": COUNTS.mods, // #evil-secret-youtube-plans - }[baseChannel.id] || + baseReactionCount(baseChannel.id) ?? { [config.channels.info?.id || ""]: COUNTS.info, [config.channels.modlogs?.parent?.id || ""]: COUNTS.mods, "866028754962612294": COUNTS.misc, // #The Cache - }[baseChannel.parent?.id || ""] || + }[baseChannel.parent?.id || ""] ?? COUNTS.default ); } +function baseReactionCount(id: Snowflake) { + if (process.env.NODE_ENV !== "production") return COUNTS.scradd; + + return { + [config.channels.tickets?.id || ""]: COUNTS.mods, + [config.channels.admin?.id || ""]: COUNTS.admins, + "853256939089559583": COUNTS.misc, // #ba-doosters + "869662117651955802": COUNTS.misc, // #devs-only + "811065897057255424": COUNTS.memes, // #memes + "806609527281549312": COUNTS.memes, // #collabs-and-ideas + "806656240129671188": COUNTS.memes, // #showcase + [config.channels.advertise?.id || ""]: COUNTS.memes, + "939350305311715358": COUNTS.mods, // #modmail + "894314668317880321": COUNTS.mods, // #evil-secret-youtube-plans + }[id]; +} /** * Generate an embed and button to represent a board message with. diff --git a/modules/reminders/send.ts b/modules/reminders/send.ts index 365eb96ae..107d09a67 100644 --- a/modules/reminders/send.ts +++ b/modules/reminders/send.ts @@ -13,7 +13,6 @@ import constants from "../../common/constants.js"; import { backupDatabases, cleanDatabaseListeners } from "../../common/database.js"; import config from "../../common/config.js"; import { boardDatabase, boardReactionCount } from "../board/misc.js"; -import { asyncFilter } from "../../util/promises.js"; import updateBoard from "../board/update.js"; let nextReminder: NodeJS.Timeout | undefined; @@ -181,24 +180,32 @@ async function sendReminders(): Promise { }, ]; - const message = await asyncFilter([...boardDatabase.data], async (info) => { - if (info.onBoard) return false; + for (const info of [...boardDatabase.data].sort(() => Math.random() - 0.5)) { + if (info.onBoard) continue; + + const reactionsNeeded = boardReactionCount({ id: info.channel }); + if (reactionsNeeded !== undefined && info.reactions < reactionsNeeded) + continue; const channel = await config.guild.channels .fetch(info.channel) .catch(() => void 0); - if (!channel?.isTextBased()) return false; + if (!channel?.isTextBased()) continue; - const reactionsNeeded = boardReactionCount(channel); - if (info.reactions < reactionsNeeded) return false; + if (reactionsNeeded === undefined) { + const reactionsNeeded = boardReactionCount(channel); + if (info.reactions < reactionsNeeded) continue; + } const message = await channel.messages .fetch(info.source) .catch(() => void 0); - return message ?? false; - }).next(); + if (message) { + await updateBoard(message); + break; + } + } - if (message.value) await updateBoard(message.value); return; } }