1
0
Fork 0

feat(bot): add DISCORD_MODLIST_CHANNEL_ID for modlist posts

This commit is contained in:
Jason Fraley 2026-12-02 11:37:28 -05:00
parent 41b883343e
commit adf7e324ae
9 changed files with 74 additions and 24 deletions

View file

@ -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

View file

@ -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=

View file

@ -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`. |

View file

@ -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 |

View file

@ -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

View file

@ -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<BotConfig, "modlistChannelId" | "opsChannelId">,
): string | null => config.modlistChannelId ?? config.opsChannelId;

View file

@ -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.`;
},
},

View file

@ -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<void> => {
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<number> => {
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<void>
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<void>
);
}
}
// 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 {

View file

@ -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();
});
});