From 6263730d9c04528698aca78f066c87d800384674 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Mon, 26 Oct 2026 18:07:05 -0400 Subject: [PATCH] refactor(bot): export render and send helpers for debug reuse --- src/bot/commands/changelog.ts | 28 ++++++++------ src/bot/services/evaluationReminders.ts | 2 +- src/bot/services/fridayOpsNotice.ts | 15 +++++--- src/bot/services/imminentReminders.ts | 4 +- src/bot/services/missionEmbeds.ts | 46 ++++++++++++++++------- src/bot/services/notificationBridge.ts | 23 ++++++------ src/bot/services/npcEventAnnouncements.ts | 2 +- src/bot/services/readyReminders.ts | 2 +- src/bot/services/transferDelivery.ts | 2 +- 9 files changed, 76 insertions(+), 48 deletions(-) diff --git a/src/bot/commands/changelog.ts b/src/bot/commands/changelog.ts index 468499b3..68298be4 100644 --- a/src/bot/commands/changelog.ts +++ b/src/bot/commands/changelog.ts @@ -30,7 +30,7 @@ import { // CHANGELOG.md sits at the repo root; resolve it relative to this file so the // path is correct regardless of the process working directory. -const CHANGELOG_PATH = path.resolve( +export const CHANGELOG_PATH = path.resolve( path.dirname(fileURLToPath(import.meta.url)), "../../../CHANGELOG.md", ); @@ -80,21 +80,27 @@ const resolveChannel = async (guild: Guild): Promise { + const total = messages.length; + return messages.map((message, i) => { + const embed = new EmbedBuilder() + .setTitle(`Changelog v${version}`) + .setColor(ANNOUNCE_COLOR) + .setDescription(message); + if (total > 1) { + embed.setFooter({ text: `Page ${i + 1}/${total}` }); + } + return embed; + }); +}; + +export const postVersion = async ( channel: TextBasedChannelFields, version: string, messages: string[], ): Promise => { - const total = messages.length; let posted = 0; - for (let i = 0; i < messages.length; i += 1) { - const embed = new EmbedBuilder() - .setTitle(`Changelog v${version}`) - .setColor(ANNOUNCE_COLOR) - .setDescription(messages[i]); - if (total > 1) { - embed.setFooter({ text: `Page ${i + 1}/${total}` }); - } + for (const embed of renderVersionEmbeds(version, messages)) { await channel.send({ embeds: [embed] }); posted += 1; } diff --git a/src/bot/services/evaluationReminders.ts b/src/bot/services/evaluationReminders.ts index 4f8a3c7e..c6bffe2d 100644 --- a/src/bot/services/evaluationReminders.ts +++ b/src/bot/services/evaluationReminders.ts @@ -66,7 +66,7 @@ const formatLinks = (links: ReminderLink[], appUrl: string): string => { return lines.join("\n").slice(0, 1024) || "-"; }; -const renderReminderEmbed = ( +export const renderReminderEmbed = ( mission: Mission, rateLeaders: ReminderLink[], rateSubordinates: ReminderLink[], diff --git a/src/bot/services/fridayOpsNotice.ts b/src/bot/services/fridayOpsNotice.ts index 2db2832c..bde2abe7 100644 --- a/src/bot/services/fridayOpsNotice.ts +++ b/src/bot/services/fridayOpsNotice.ts @@ -30,11 +30,14 @@ let reconcileRunning = false; const errorMessage = (error: unknown): string => error instanceof Error ? error.message : "Unknown error"; -type SideOpsChannel = TextBasedChannelFields & { +export type SideOpsChannel = TextBasedChannelFields & { messages: { fetch(options: { limit: number }): Promise> }; }; -const getSideOpsChannel = async (client: Client, channelId: string): Promise => { +export const getSideOpsChannel = async ( + client: Client, + channelId: string, +): Promise => { const channel = await client.channels.fetch(channelId); if (!channel || !("send" in channel) || !("messages" in channel)) { throw new Error(`[Bot] Side-ops channel ${channelId} is not a text channel.`); @@ -43,16 +46,16 @@ const getSideOpsChannel = async (client: Client, channelId: string): Promise => { +export const resolveZeusRoleId = async (client: Client): Promise => { if (botConfig.communityZeusRoleId) return botConfig.communityZeusRoleId; const guild = await client.guilds.fetch(botConfig.guildId); const role = guild.roles.cache.find((role) => role.name === SIDE_OPS_NOTICE_ROLE_NAME); return role?.id ?? null; }; -const renderNotice = (zeusRoleId: string | null): string => { +export const renderNotice = (zeusRoleId: string | null, now = new Date()): string => { const mention = zeusRoleId ? `<@&${zeusRoleId}> ` : ""; - const { epoch } = upcomingFridayRange(new Date()); + const { epoch } = upcomingFridayRange(now); return ( `${mention}The Friday op slot () is still open, and you are welcome to host an op: ` + `schedule one via the Friday Ops calendar at ${botConfig.appUrl}/intelligence/missions/calendar. ` + @@ -67,7 +70,7 @@ const CLAIM_LINE_PATTERN = const creditedNameOf = (content: string): string | null => content.match(CLAIM_LINE_PATTERN)?.[1] ?? null; -const findNoticeMessage = async ( +export const findNoticeMessage = async ( channel: SideOpsChannel, token: string, ): Promise => { diff --git a/src/bot/services/imminentReminders.ts b/src/bot/services/imminentReminders.ts index 390e13e2..15a96e38 100644 --- a/src/bot/services/imminentReminders.ts +++ b/src/bot/services/imminentReminders.ts @@ -26,7 +26,7 @@ let reconcileRunning = false; const errorMessage = (error: unknown): string => error instanceof Error ? error.message : "Unknown error"; -const renderReminderEmbed = (mission: Mission): EmbedBuilder => { +export const renderReminderEmbed = (mission: Mission): EmbedBuilder => { const startUnix = Math.floor(new Date(mission.classification.startDateTime).getTime() / 1000); return new EmbedBuilder() .setTitle(`⏰ Op starting soon: ${missionTitle(mission)}`) @@ -38,7 +38,7 @@ const renderReminderEmbed = (mission: Mission): EmbedBuilder => { }); }; -const mentionsFor = (users: User[]): string[] => +export const mentionsFor = (users: User[]): string[] => users .filter((user) => user.discordId) .map((user) => `<@${user.discordId as string}>`); diff --git a/src/bot/services/missionEmbeds.ts b/src/bot/services/missionEmbeds.ts index 72f1fea3..69a6fa40 100644 --- a/src/bot/services/missionEmbeds.ts +++ b/src/bot/services/missionEmbeds.ts @@ -213,7 +213,7 @@ export const renderCancelledEmbed = (mission: Mission): EmbedBuilder => .setDescription("This operation has been cancelled.") .setColor(CANCELLED_COLOR); -const renderCancelledNoticeEmbed = (mission: Mission): EmbedBuilder => +export const renderCancelledNoticeEmbed = (mission: Mission): EmbedBuilder => new EmbedBuilder() .setTitle(`Cancelled: ${missionTitle(mission)}`) .setDescription( @@ -223,7 +223,7 @@ const renderCancelledNoticeEmbed = (mission: Mission): EmbedBuilder => .setColor(CANCELLED_COLOR) .setTimestamp(); -const renderRescheduledEmbed = (mission: Mission, rollCallDueUnix: number | null): EmbedBuilder => { +export const renderRescheduledEmbed = (mission: Mission, rollCallDueUnix: number | null): EmbedBuilder => { const startUnix = Math.floor(new Date(mission.classification.startDateTime).getTime() / 1000); return new EmbedBuilder() .setTitle(`Rescheduled: ${missionTitle(mission)}`) @@ -255,7 +255,7 @@ export const buildAttendanceRow = (missionId: number): ActionRowBuilder => { @@ -266,7 +266,18 @@ const getOpsChannel = async ( return channel as unknown as TextBasedChannelFields; }; -const postEmbed = async ( +export const buildRollCallMessage = async ( + payload: Payload, + mission: Mission, +): Promise<{ embeds: EmbedBuilder[]; components: ActionRowBuilder[] }> => { + const summary = await getMissionAttendance(payload, mission.id); + return { + embeds: [renderAttendanceEmbed(mission, summary)], + components: [buildAttendanceRow(mission.id)], + }; +}; + +export const postEmbed = async ( client: Client, payload: Payload, opsChannelId: string, @@ -301,7 +312,7 @@ const postEmbed = async ( }); }; -interface ModlistInfo { +export interface ModlistInfo { id: number; name: string; filename: string; @@ -310,7 +321,7 @@ interface ModlistInfo { updatedAt: string; } -const resolveModlist = async (payload: Payload, mission: Mission): Promise => { +export const resolveModlist = async (payload: Payload, mission: Mission): Promise => { const raw = mission.gameDetails.gameSetup?.requiredMods; if (raw == null || typeof raw === "number") return null; const modlist = raw as ModList; @@ -337,7 +348,7 @@ const modlistHash = (modlist: ModlistInfo): string => updatedAt: modlist.updatedAt, }); -const renderModlistEmbed = ( +export const renderModlistEmbed = ( mission: Mission, modlist: ModlistInfo, kind: "new" | "updated", @@ -354,15 +365,12 @@ const renderModlistEmbed = ( return embed; }; -const postModlistMessage = async ( - client: Client, +export const buildModlistMessage = async ( payload: Payload, - opsChannelId: string, mission: Mission, modlist: ModlistInfo, kind: "new" | "updated", -): Promise => { - const channel = await getOpsChannel(client, opsChannelId); +): Promise<{ embeds: EmbedBuilder[]; files?: AttachmentBuilder[] }> => { const embed = renderModlistEmbed(mission, modlist, kind); let files: AttachmentBuilder[] | undefined; @@ -389,7 +397,19 @@ const postModlistMessage = async ( embed.addFields({ name: "Download", value: `${botConfig.appUrl}${modlist.url}` }); } - await channel.send({ embeds: [embed], files }); + return { embeds: [embed], files }; +}; + +export const postModlistMessage = async ( + client: Client, + payload: Payload, + opsChannelId: string, + mission: Mission, + modlist: ModlistInfo, + kind: "new" | "updated", +): Promise => { + const channel = await getOpsChannel(client, opsChannelId); + await channel.send(await buildModlistMessage(payload, mission, modlist, kind)); }; /** diff --git a/src/bot/services/notificationBridge.ts b/src/bot/services/notificationBridge.ts index 0ad7c9bc..3c7cd53f 100644 --- a/src/bot/services/notificationBridge.ts +++ b/src/bot/services/notificationBridge.ts @@ -43,6 +43,16 @@ const eligibleForDm = (owner: User, type?: string | null): boolean => { return true; }; +export const renderNotificationEmbed = (notification: UserNotification): EmbedBuilder => + new EmbedBuilder() + .setTitle(notification.title) + .setDescription(notification.message ?? null) + .setColor(EMBED_COLOR) + .addFields({ + name: "Link", + value: `${botConfig.appUrl}${notification.link ?? ""}`, + }); + const poll = async (client: Client, payload: Payload): Promise => { try { if (cursor === null) { @@ -76,18 +86,7 @@ const poll = async (client: Client, payload: Payload): Promise => { if (!eligibleForDm(owner, notification.type)) continue; const target = await client.users.fetch(owner.discordId as string); - await target.send({ - embeds: [ - new EmbedBuilder() - .setTitle(notification.title) - .setDescription(notification.message ?? null) - .setColor(EMBED_COLOR) - .addFields({ - name: "Link", - value: `${botConfig.appUrl}${notification.link ?? ""}`, - }), - ], - }); + await target.send({ embeds: [renderNotificationEmbed(notification)] }); sent++; if (sent < MAX_SENDS_PER_TICK) await sleep(SEND_DELAY_MS); } catch { diff --git a/src/bot/services/npcEventAnnouncements.ts b/src/bot/services/npcEventAnnouncements.ts index 65b36007..6538c639 100644 --- a/src/bot/services/npcEventAnnouncements.ts +++ b/src/bot/services/npcEventAnnouncements.ts @@ -30,7 +30,7 @@ let running = false; const errorMessage = (error: unknown): string => error instanceof Error ? error.message : "Unknown error"; -const buildEmbed = (doc: GameEventLog): EmbedBuilder => { +export const buildEmbed = (doc: GameEventLog): EmbedBuilder => { const epoch = Math.floor(new Date(doc.timestamp).getTime() / 1000); const embed = new EmbedBuilder() .setTitle("NPC Dispatch") diff --git a/src/bot/services/readyReminders.ts b/src/bot/services/readyReminders.ts index aee1518d..41b0c66e 100644 --- a/src/bot/services/readyReminders.ts +++ b/src/bot/services/readyReminders.ts @@ -38,7 +38,7 @@ const errorMessage = (error: unknown): string => const creatorIdOf = (mission: Mission): number | null => typeof mission.createdBy === "object" ? (mission.createdBy?.id ?? null) : (mission.createdBy ?? null); -const renderReminderEmbed = (mission: Mission, now: Date): EmbedBuilder => { +export const renderReminderEmbed = (mission: Mission, now: Date): EmbedBuilder => { const days = daysUntil(mission.classification.startDateTime, now); const when = days === 0 ? "today" : days === 1 ? "tomorrow" : `in ${days} days`; return new EmbedBuilder() diff --git a/src/bot/services/transferDelivery.ts b/src/bot/services/transferDelivery.ts index c0e27985..3c56db53 100644 --- a/src/bot/services/transferDelivery.ts +++ b/src/bot/services/transferDelivery.ts @@ -35,7 +35,7 @@ const fetchUser = (payload: Payload, id: number): Promise => const fetchAssignment = (payload: Payload, id: number): Promise => payload.findByID({ collection: "assignments", id, depth: 0, overrideAccess: true }); -const contextFor = async ( +export const contextFor = async ( payload: Payload, doc: AssignmentTransfer, ): Promise => {