From 402acffb5cc44773cee4c7242133ee9a0a81b917 Mon Sep 17 00:00:00 2001 From: Naomi Carrigan Date: Tue, 30 Dec 2025 11:55:51 -0800 Subject: [PATCH] fix: properly handle duplicated channels --- src/config/progressReminders.ts | 14 ++---- src/interfaces/reminder.ts | 12 +++++ src/modules/postProgressReminders.ts | 75 ++++++++++++++++------------ 3 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 src/interfaces/reminder.ts diff --git a/src/config/progressReminders.ts b/src/config/progressReminders.ts index 17cd038..b8e56e6 100644 --- a/src/config/progressReminders.ts +++ b/src/config/progressReminders.ts @@ -3,15 +3,9 @@ * @license Naomi's Public License * @author Naomi Carrigan */ +import type { ProgressReminder } from "../interfaces/reminder.js"; -interface Channel { - channelId: string; - roleId: string; - name: string; - createThread: boolean; -} - -const nhcarriganMentorshipChannels: Array = [ +const nhcarriganMentorshipChannels: Array = [ { channelId: "1400630073010163792", createThread: true, @@ -19,7 +13,7 @@ const nhcarriganMentorshipChannels: Array = [ roleId: "1400588705273745550", }, ]; -const freeCodeCampSprintChannels: Array = [ +const freeCodeCampSprintChannels: Array = [ { channelId: "1400935953887006861", createThread: true, @@ -49,7 +43,7 @@ const freeCodeCampSprintChannels: Array = [ /** * The channels to post progress reminders in. */ -export const progressReminders: Array = [ +export const progressReminders: Array = [ ...nhcarriganMentorshipChannels, ...freeCodeCampSprintChannels, ]; diff --git a/src/interfaces/reminder.ts b/src/interfaces/reminder.ts new file mode 100644 index 0000000..412ad5f --- /dev/null +++ b/src/interfaces/reminder.ts @@ -0,0 +1,12 @@ +/** + * @copyright nhcarrigan + * @license Naomi's Public License + * @author Naomi Carrigan + */ + +export interface ProgressReminder { + channelId: string; + roleId: string; + name: string; + createThread: boolean; +} diff --git a/src/modules/postProgressReminders.ts b/src/modules/postProgressReminders.ts index 8bbc6b8..78086c1 100644 --- a/src/modules/postProgressReminders.ts +++ b/src/modules/postProgressReminders.ts @@ -3,70 +3,79 @@ * @license Naomi's Public License * @author Naomi Carrigan */ -import { ChannelType } from "discord.js"; +import { ChannelType, type TextChannel } from "discord.js"; import { progressReminders } from "../config/progressReminders.js"; import { logger } from "../utils/logger.js"; import type { Amari } from "../interfaces/amari.js"; +import type { ProgressReminder } from "../interfaces/reminder.js"; /** * Posts a daily progress check-in reminder in configured channels. * @param amari - Amari's instance. */ // eslint-disable-next-line max-lines-per-function -- shut up -export const postProgressReminders = async( - amari: Amari, -): Promise => { +export const postProgressReminders = async(amari: Amari): Promise => { try { const mapped = await Promise.all( - progressReminders.map(async(channel) => { + progressReminders.map(async(reminder) => { const fetched = await amari.discord.channels. - fetch(channel.channelId). + fetch(reminder.channelId). catch(() => { - void logger.log("warn", `Failed to fetch channel ${channel.name}.`); + void logger.log( + "warn", + `Failed to fetch channel ${reminder.name}.`, + ); return null; }); if (!fetched) { - await logger.log("warn", `Channel ${channel.name} not found.`); - return null; + await logger.log("warn", `Channel ${reminder.name} not found.`); + return { channel: null, reminder: reminder }; } if (fetched.type !== ChannelType.GuildText) { await logger.log( "warn", - `Channel ${channel.name} is not a text channel.`, + `Channel ${reminder.name} is not a text channel.`, ); - return null; + return { channel: null, reminder: reminder }; } - return fetched; + return { channel: fetched, reminder: reminder }; }), ); - const filtered = mapped.filter((channel) => { + const filtered: Array<{ + channel: TextChannel; + reminder: ProgressReminder; + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Filter is dumb. + }> = mapped.filter(({ channel }) => { return channel !== null; - }); + }) as Array<{ + channel: TextChannel; + reminder: ProgressReminder; + }>; await Promise.all( - filtered.map(async(channel) => { - const isThread = progressReminders.find((c) => { - return c.channelId === channel.id; - })?.createThread ?? false; - const sent = await channel. - send({ allowedMentions: { - parse: [ "roles" ], - }, - content: - `Good morning <@&${ - progressReminders.find((c) => { - return c.channelId === channel.id; - })?.roleId ?? channel.guildId - }> It is time for your daily progress update. Please share the following in ${isThread - ? "the thread attached to this message" - : "this channel"}: + filtered.map(async(reminder) => { + const isThread + = progressReminders.find((c) => { + return c.channelId === reminder.channel.id; + })?.createThread ?? false; + const sent = await reminder.channel. + send({ + allowedMentions: { + parse: [ "roles" ], + }, + content: `Good morning <@&${reminder.reminder.roleId}> It is time for your daily progress update. Please share the following in ${ + isThread + ? "the thread attached to this message" + : "this channel" + }: 1️⃣ What did you accomplish yesterday? 2️⃣ What are you working on today? -3️⃣ Are there any blockers or issues you need help with?` }). +3️⃣ Are there any blockers or issues you need help with?`, + }). catch(() => { void logger.log( "warn", - `Failed to send progress reminder in channel ${channel.name}.`, + `Failed to send progress reminder in channel ${reminder.channel.name}.`, ); return null; }); @@ -79,7 +88,7 @@ export const postProgressReminders = async( catch(() => { void logger.log( "warn", - `Failed to start thread in channel ${channel.name}.`, + `Failed to start thread in channel ${reminder.channel.name}.`, ); return null; });