From adf7e324ae0b6e36afb1102999ca3abae13dc2f5 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Wed, 2 Dec 2026 11:37:28 -0500 Subject: [PATCH] feat(bot): add DISCORD_MODLIST_CHANNEL_ID for modlist posts --- .env.development | 1 + .env.example | 3 ++ DEPLOYMENT.md | 1 + docs/bot/design.md | 3 +- src/bot/AGENTS.md | 4 +-- src/bot/config.ts | 11 +++++++ src/bot/lib/debugActions.ts | 11 +++++-- src/bot/services/missionEmbeds.ts | 45 +++++++++++++++++----------- tests/int/modlist-buffer.int.spec.ts | 19 ++++++++++++ 9 files changed, 74 insertions(+), 24 deletions(-) diff --git a/.env.development b/.env.development index eac6e4a5..030b551c 100644 --- a/.env.development +++ b/.env.development @@ -17,6 +17,7 @@ GAME_TICK_NOTIFY_SECRET=4707fba13f1c66db44df38721fba204f2d6bdc6796ced7a5 DISCORD_TOKEN=MTUzNzI2MzQ0Njk2NDMwNjEwMQ.GnTFBc.2LHX1FO7aAm9LbrKzj2IlVFNDmTBXsnBebzk3o DISCORD_GUILD_ID=851910807020765275 DISCORD_OPS_CHANNEL_ID=1136692636481237173 +DISCORD_MODLIST_CHANNEL_ID=851910807632871468 DISCORD_ANNOUNCE_CHANNEL_ID=1136692636481237173 DISCORD_STAFF_ROLE_IDS=851910807277404187,851910807277404188,851910807277404189,851910807289462814 DISCORD_ATTENDANCE_POLL_MS=5000 diff --git a/.env.example b/.env.example index 89e16092..8ce904d0 100644 --- a/.env.example +++ b/.env.example @@ -68,6 +68,9 @@ APP_PORT=3000 # DISCORD_GUILD_ID= # Roll call: mission attendance embeds and roll-call posts go here (ops channel). # DISCORD_OPS_CHANNEL_ID= +# Modlists: new/changed mission modlists and the 8-hour pre-op modlist buffer +# post here. Unset falls back to DISCORD_OPS_CHANNEL_ID. +# DISCORD_MODLIST_CHANNEL_ID= # App updates: where /announce posts when no channel is given; also the # channel preselected in the /announce compose dialog. # DISCORD_ANNOUNCE_CHANNEL_ID= diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index 91beae0f..9d269a2d 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -97,6 +97,7 @@ The Dockerfile pins Bun 1.3.11 (**build-time only**) and Node 22 alpine (runtime | `DISCORD_TOKEN` | iff bot enabled in this env | Set in **prod only** — presence activates the bot (activation gate below). | | `DISCORD_GUILD_ID` | iff `DISCORD_TOKEN` set | Config throws at import if the token is set and this is missing. | | `DISCORD_OPS_CHANNEL_ID` | — | Ops channel for attendance RSVP embeds. | +| `DISCORD_MODLIST_CHANNEL_ID` | — | New/changed mission modlists and the 8h pre-op buffer; falls back to `DISCORD_OPS_CHANNEL_ID`. | | `DISCORD_ANNOUNCE_CHANNEL_ID` | — | Channel for `/announce`. | | `DISCORD_STAFF_ROLE_IDS` | — | Comma-separated staff role IDs (staff-only `/announce`). | | `DISCORD_ATTENDANCE_POLL_MS` | — | Attendance reconcile poll interval; default `60000`. | diff --git a/docs/bot/design.md b/docs/bot/design.md index e8e37a3d..982a8f88 100644 --- a/docs/bot/design.md +++ b/docs/bot/design.md @@ -162,7 +162,8 @@ Dependency: `discord.js` ^14 (add to root `package.json`). Script: `"bot": "bun |---|---|---| | `DISCORD_TOKEN` | yes | bot token | | `DISCORD_GUILD_ID` | yes | guild for slash commands | -| `DISCORD_OPS_CHANNEL_ID` | yes | attendance embeds | +| `DISCORD_OPS_CHANNEL_ID` | yes | attendance embeds and roll-calls | +| `DISCORD_MODLIST_CHANNEL_ID` | no | new/changed mission modlists and the 8h pre-op buffer; falls back to `DISCORD_OPS_CHANNEL_ID` | | `DISCORD_ANNOUNCE_CHANNEL_ID` | no | default `/announce` target | | `DISCORD_STAFF_ROLE_IDS` | no | comma-separated role IDs granting staff | | `DISCORD_ATTENDANCE_POLL_MS` | no | default 60000 | diff --git a/src/bot/AGENTS.md b/src/bot/AGENTS.md index d4248b40..a99c5fff 100644 --- a/src/bot/AGENTS.md +++ b/src/bot/AGENTS.md @@ -8,7 +8,7 @@ Standalone long-running process (`bun run bot`). Imports `@payload-config` direc ## Env -Required: `DISCORD_TOKEN`, `DISCORD_GUILD_ID` (fail-fast on missing vars in `config.ts`). Optional: `DISCORD_OPS_CHANNEL_ID`, `DISCORD_ANNOUNCE_CHANNEL_ID`, `DISCORD_CHANGELOG_CHANNEL_ID` (dedicated #releases channel for `/changelog`; falls back to the announce channel), `DISCORD_STAFF_ROLE_IDS`, `DISCORD_SIDE_OPS_CHANNEL_ID` (Friday open-slot notice channel), `DISCORD_COMMUNITY_ZEUS_ROLE_ID` (role ping for that notice), `DISCORD_NPC_EVENT_CHANNEL_ID` (NPC dispatch channel for dialogue events; unset disables posting), `DISCORD_DEBUG_CHANNEL_ID` (sandbox channel for `/debug run ... mode:sandbox`; unset disables sandbox), `APP_URL`, plus poll intervals `DISCORD_ATTENDANCE_POLL_MS` (default 60s), `DISCORD_NOTIFICATION_POLL_MS` (default 20s), `DISCORD_EVALUATION_POLL_MS` (default 60s), `DISCORD_IMMINENT_POLL_MS` (default 60s), `DISCORD_SIDE_OPS_POLL_MS` (default 30 min), `DISCORD_PRESENCE_POLL_MS` (default 30s), `DISCORD_NPC_EVENT_POLL_MS` (default 60s), `DISCORD_POLL_CHANNEL_ID` (unit poll channel; falls back to the ops channel), `DISCORD_POLL_POLL_MS` (default 60s). Logs through `payload.logger`. +Required: `DISCORD_TOKEN`, `DISCORD_GUILD_ID` (fail-fast on missing vars in `config.ts`). Optional: `DISCORD_OPS_CHANNEL_ID`, `DISCORD_MODLIST_CHANNEL_ID` (new/changed mission modlists and the 8h pre-op buffer; falls back to the ops channel), `DISCORD_ANNOUNCE_CHANNEL_ID`, `DISCORD_CHANGELOG_CHANNEL_ID` (dedicated #releases channel for `/changelog`; falls back to the announce channel), `DISCORD_STAFF_ROLE_IDS`, `DISCORD_SIDE_OPS_CHANNEL_ID` (Friday open-slot notice channel), `DISCORD_COMMUNITY_ZEUS_ROLE_ID` (role ping for that notice), `DISCORD_NPC_EVENT_CHANNEL_ID` (NPC dispatch channel for dialogue events; unset disables posting), `DISCORD_DEBUG_CHANNEL_ID` (sandbox channel for `/debug run ... mode:sandbox`; unset disables sandbox), `APP_URL`, plus poll intervals `DISCORD_ATTENDANCE_POLL_MS` (default 60s), `DISCORD_NOTIFICATION_POLL_MS` (default 20s), `DISCORD_EVALUATION_POLL_MS` (default 60s), `DISCORD_IMMINENT_POLL_MS` (default 60s), `DISCORD_SIDE_OPS_POLL_MS` (default 30 min), `DISCORD_PRESENCE_POLL_MS` (default 30s), `DISCORD_NPC_EVENT_POLL_MS` (default 60s), `DISCORD_POLL_CHANNEL_ID` (unit poll channel; falls back to the ops channel), `DISCORD_POLL_POLL_MS` (default 60s). Logs through `payload.logger`. ## Structure @@ -88,7 +88,7 @@ Actions live in `bot/lib/debugActions.ts`. Each `build` receives the client and ## Feature flow: attendance -Bot posts RSVP embeds (Yes/Tentative/No) for future, Scheduled, visibility:"unit" missions into ops channel. Stores `discordMessageId` + `discordAttendanceHash` on mission. Reconciles hash changes every poll tick (web ↔ Discord two-way sync, loop-safe). The embed title links to the mission page, a mission summary sits under the title (truncated to 300 characters with an ellipsis), and an "Open the op" field carries the URL; `attendanceHash` includes an embed schema version so layout-only changes re-render live roll-calls once (idempotent). Web UI: `MissionAttendance` component. Weekly main missions are cloned by `bun run payload generate-mission` (a payload bin, see `src/scripts/AGENTS.md`). Staff can force one outside the lead-time window with `/rollcall [mission]` (ID, name, or code name; omit to target the next upcoming Scheduled unit op): `forceRollCall` in `missionEmbeds.ts` re-renders the live message or posts a fresh one (even after a manual deletion), bypassing only the 3-day gate; target lookup/validation lives in `src/lib/intelligence/rollCall.ts`. +Bot posts RSVP embeds (Yes/Tentative/No) for future, Scheduled, visibility:"unit" missions into ops channel. New/changed modlist attachments and the 8-hour modlist buffer post to `DISCORD_MODLIST_CHANNEL_ID`, falling back to the ops channel. Stores `discordMessageId` + `discordAttendanceHash` on mission. Reconciles hash changes every poll tick (web ↔ Discord two-way sync, loop-safe). The embed title links to the mission page, a mission summary sits under the title (truncated to 300 characters with an ellipsis), and an "Open the op" field carries the URL; `attendanceHash` includes an embed schema version so layout-only changes re-render live roll-calls once (idempotent). Web UI: `MissionAttendance` component. Weekly main missions are cloned by `bun run payload generate-mission` (a payload bin, see `src/scripts/AGENTS.md`). Staff can force one outside the lead-time window with `/rollcall [mission]` (ID, name, or code name; omit to target the next upcoming Scheduled unit op): `forceRollCall` in `missionEmbeds.ts` re-renders the live message or posts a fresh one (even after a manual deletion), bypassing only the 3-day gate; target lookup/validation lives in `src/lib/intelligence/rollCall.ts`. ## Feature flow: unit polls diff --git a/src/bot/config.ts b/src/bot/config.ts index eb76c20b..0c77c2f3 100644 --- a/src/bot/config.ts +++ b/src/bot/config.ts @@ -12,6 +12,8 @@ export interface BotConfig { npcEventChannelId: string | null; /** Channel unit polls are posted in. Falls back to the ops channel. */ pollChannelId: string | null; + /** Channel modlist posts are sent to. Falls back to the ops channel. */ + modlistChannelId: string | null; debugChannelId: string | null; attendancePollMs: number; notificationPollMs: number; @@ -81,6 +83,7 @@ export const botConfig: BotConfig = { announceMentionRoleId: optional("DISCORD_ANNOUNCE_MENTION_ROLE_ID"), npcEventChannelId: optional("DISCORD_NPC_EVENT_CHANNEL_ID"), pollChannelId: optional("DISCORD_POLL_CHANNEL_ID"), + modlistChannelId: optional("DISCORD_MODLIST_CHANNEL_ID"), debugChannelId: optional("DISCORD_DEBUG_CHANNEL_ID"), attendancePollMs: pollMs("DISCORD_ATTENDANCE_POLL_MS", 60_000), notificationPollMs: pollMs("DISCORD_NOTIFICATION_POLL_MS", 20_000), @@ -94,3 +97,11 @@ export const botConfig: BotConfig = { pollPollMs: pollMs("DISCORD_POLL_POLL_MS", 60_000), modlistBufferHours: parseHours("DISCORD_MODLIST_BUFFER_HOURS", 8), }; + +/** + * Modlist posts (a new or changed preset, and the 8-hour pre-op buffer) use + * the dedicated channel when one is configured, otherwise the ops channel. + */ +export const resolveModlistChannelId = ( + config: Pick, +): string | null => config.modlistChannelId ?? config.opsChannelId; diff --git a/src/bot/lib/debugActions.ts b/src/bot/lib/debugActions.ts index b25027c5..0b3ece16 100644 --- a/src/bot/lib/debugActions.ts +++ b/src/bot/lib/debugActions.ts @@ -7,7 +7,7 @@ import { } from "discord.js"; import { readFileSync } from "node:fs"; import type { Payload } from "payload"; -import { botConfig } from "@/bot/config"; +import { botConfig, resolveModlistChannelId } from "@/bot/config"; import { CHANGELOG_PATH, postVersion, @@ -259,8 +259,13 @@ export const debugActions: DebugAction[] = [ const mission = await resolveMission(payload, target); const modlist = await resolveModlist(payload, mission); if (!modlist) throw new Error(`${mission.name} has no modlist attached.`); - const opsChannelId = requireChannel(botConfig.opsChannelId, "DISCORD_OPS_CHANNEL_ID"); - await postModlistMessage(client, payload, opsChannelId, mission, modlist, "new"); + const channelId = resolveModlistChannelId(botConfig); + if (!channelId) { + throw new Error( + "Neither DISCORD_MODLIST_CHANNEL_ID nor DISCORD_OPS_CHANNEL_ID is set; cannot post modlists.", + ); + } + await postModlistMessage(client, payload, channelId, mission, modlist, "new"); return `Posted the modlist message for **${mission.name}**. Stored markers were not changed.`; }, }, diff --git a/src/bot/services/missionEmbeds.ts b/src/bot/services/missionEmbeds.ts index 2b78abf7..c7bee724 100644 --- a/src/bot/services/missionEmbeds.ts +++ b/src/bot/services/missionEmbeds.ts @@ -19,7 +19,7 @@ import { import { MISSION_RATING_CORE_FACTORS } from "@/lib/missionRatings/factors"; import { ATTENDANCE_CHANGED_CHANNEL } from "@/lib/attendance/realtime"; import { sortAttendanceUnlocks } from "@/lib/intelligence/attendanceUnlocks"; -import { botConfig } from "@/bot/config"; +import { botConfig, resolveModlistChannelId } from "@/bot/config"; const EMBED_COLOR = 0x3b82f6; const CANCELLED_COLOR = 0x6b7280; @@ -449,7 +449,7 @@ export const postEmbed = async ( payload.logger.info( `[Bot] Posted attendance embed for mission #${mission.id} (${mission.name}).`, ); - await syncModlistMessage(client, payload, opsChannelId, { + await syncModlistMessage(client, payload, { ...mission, discordAttendanceSentAt: new Date().toISOString(), }); @@ -563,11 +563,18 @@ export const postModlistMessage = async ( const syncModlistMessage = async ( client: Client, payload: Payload, - opsChannelId: string, mission: Mission, ): Promise => { if (!mission.discordAttendanceSentAt) return; + const channelId = resolveModlistChannelId(botConfig); + if (!channelId) { + payload.logger.warn( + "[Bot] Neither DISCORD_MODLIST_CHANNEL_ID nor DISCORD_OPS_CHANNEL_ID is set; skipping modlist posts.", + ); + return; + } + const modlist = await resolveModlist(payload, mission); const currentHash = modlist ? modlistHash(modlist) : null; const storedHash = mission.discordModlistHash ?? null; @@ -575,7 +582,7 @@ const syncModlistMessage = async ( if (modlist) { const kind = storedHash ? "updated" : "new"; - await postModlistMessage(client, payload, opsChannelId, mission, modlist, kind); + await postModlistMessage(client, payload, channelId, mission, modlist, kind); payload.logger.info( `[Bot] Posted ${kind} modlist message for mission #${mission.id} (${mission.name}).`, ); @@ -613,8 +620,8 @@ export const reconcileModlistBuffer = async ( payload: Payload, now: Date = new Date(), ): Promise => { - const opsChannelId = botConfig.opsChannelId; - if (!opsChannelId) return 0; + const channelId = resolveModlistChannelId(botConfig); + if (!channelId) return 0; const bufferMs = botConfig.modlistBufferHours * 60 * 60 * 1000; const upperBound = new Date(now.getTime() + bufferMs).toISOString(); @@ -652,7 +659,7 @@ export const reconcileModlistBuffer = async ( if (!modlist) continue; try { - await postModlistMessage(client, payload, opsChannelId, mission, modlist, "updated"); + await postModlistMessage(client, payload, channelId, mission, modlist, "updated"); await payload.update({ collection: "missions", id: mission.id, @@ -684,7 +691,7 @@ const manageMission = async ( } if (mission.discordAttendanceSentAt) { - await syncModlistMessage(client, payload, opsChannelId, mission); + await syncModlistMessage(client, payload, mission); } if (!mission.discordMessageId) { @@ -898,6 +905,18 @@ export const reconcile = async (client: Client, payload: Payload): Promise if (reconcileRunning) return; reconcileRunning = true; try { + // 8-hour modlist buffer: post the modlist message once per scheduled + // mission, 8 hours before its start. It resolves its own channel + // (DISCORD_MODLIST_CHANNEL_ID, falling back to the ops channel), so it + // runs even when the ops channel is unset. + try { + await reconcileModlistBuffer(client, payload); + } catch (error) { + payload.logger.error( + `[Bot] Modlist buffer reconcile failed: ${errorMessage(error)}`, + ); + } + const opsChannelId = botConfig.opsChannelId; if (!opsChannelId) { if (!opsChannelWarned) { @@ -958,16 +977,6 @@ export const reconcile = async (client: Client, payload: Payload): Promise ); } } - - // 8-hour modlist buffer: post the modlist message once per scheduled - // mission, 8 hours before its start, independent of the roll-call sync. - try { - await reconcileModlistBuffer(client, payload); - } catch (error) { - payload.logger.error( - `[Bot] Modlist buffer reconcile failed: ${errorMessage(error)}`, - ); - } } catch (error) { payload.logger.error(`[Bot] Attendance embed reconcile failed: ${errorMessage(error)}`); } finally { diff --git a/tests/int/modlist-buffer.int.spec.ts b/tests/int/modlist-buffer.int.spec.ts index 071dbb53..4fa09a87 100644 --- a/tests/int/modlist-buffer.int.spec.ts +++ b/tests/int/modlist-buffer.int.spec.ts @@ -4,6 +4,7 @@ import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; import type { Campaign, Map as MissionMap, Mission, User } from "@/payload-types"; import { reconcileModlistBuffer } from "@/bot/services/missionEmbeds"; +import { resolveModlistChannelId } from "@/bot/config"; let payload: Payload; @@ -281,3 +282,21 @@ describe("reconcileModlistBuffer (8h modlist reminder)", () => { expect((await fetchMission(mission.id)).discordModlistBufferSentAt).toBeTruthy(); }); }); + +describe("resolveModlistChannelId", () => { + it("prefers the dedicated modlist channel", () => { + expect( + resolveModlistChannelId({ modlistChannelId: "mod-channel", opsChannelId: "ops-channel" }), + ).toBe("mod-channel"); + }); + + it("falls back to the ops channel when the modlist channel is unset", () => { + expect( + resolveModlistChannelId({ modlistChannelId: null, opsChannelId: "ops-channel" }), + ).toBe("ops-channel"); + }); + + it("returns null when neither channel is configured", () => { + expect(resolveModlistChannelId({ modlistChannelId: null, opsChannelId: null })).toBeNull(); + }); +});