1
0
Fork 0
polaris-task-force/src/bot/config.ts
Z8MB1E dee3dbea09 feat(polls): mirror unit polls to Discord with buttons and a reconcile loop
Buttons rather than a native Discord poll: native polls (message type 46) show
a raw headcount, cannot be edited, and cannot be closed early, so they could not
carry weighted tallies, the tie ladder, or a two-way sync.

- One button per option (`ptf-poll:<pollId>:<optionKey>`), acknowledged with
  deferReply before the member lookup, because the interaction token dies at 3s.
- Buttons stay neutral on purpose. The message is shared, so highlighting a
  selection would show one member's ballot to the unit, and on an anonymous
  poll it would leak exactly what the poll hides. The voter's own ballot comes
  back ephemerally instead.
- `discordPollHash` fingerprints everything the message shows (schema version,
  status, counts, winner, deadline, public voter names); the reconcile loop
  edits on drift and stamps the hash back, so it settles in one pass.
- A `LISTEN ptf_poll_changed` listener reacts to web-side votes in under a
  second, and the timer pass is the fallback if a notification is dropped.
- `advancePoll` runs inside the reconcile pass, so a poll closes and resolves
  even when nobody has the page open. A deleted message is never reposted.
- `DISCORD_POLL_CHANNEL_ID` sets the channel; unset falls back to ops.
2026-12-01 14:17:44 -05:00

96 lines
3.5 KiB
TypeScript

export interface BotConfig {
token: string;
guildId: string;
appUrl: string;
opsChannelId: string | null;
announceChannelId: string | null;
staffRoleIds: string[];
sideOpsChannelId: string | null;
communityZeusRoleId: string | null;
changelogChannelId: string | null;
announceMentionRoleId: string | null;
npcEventChannelId: string | null;
/** Channel unit polls are posted in. Falls back to the ops channel. */
pollChannelId: string | null;
debugChannelId: string | null;
attendancePollMs: number;
notificationPollMs: number;
evaluationPollMs: number;
readyReminderPollMs: number;
imminentReminderPollMs: number;
sideOpsNoticePollMs: number;
achievementPollMs: number;
presencePollMs: number;
npcEventPollMs: number;
pollPollMs: number;
/** Hours before a scheduled operation the modlist buffer reminder fires. */
modlistBufferHours: number;
}
const required = (name: string): string => {
const value = process.env[name]?.trim();
if (!value) {
throw new Error(`[Bot] Missing required environment variable: ${name}`);
}
return value;
};
const optional = (name: string): string | null => {
const value = process.env[name]?.trim();
return value ? value : null;
};
const pollMs = (name: string, fallback: number): number => {
const raw = process.env[name]?.trim();
if (!raw) {
return fallback;
}
const value = Number.parseInt(raw, 10);
if (!Number.isInteger(value) || value <= 0) {
throw new Error(`[Bot] Invalid ${name}: expected a positive integer, got "${raw}"`);
}
return value;
};
/** Parse a positive-integer count of hours from an env var (milliseconds unused here). */
const parseHours = (name: string, fallback: number): number => {
const raw = process.env[name]?.trim();
if (!raw) {
return fallback;
}
const value = Number.parseInt(raw, 10);
if (!Number.isInteger(value) || value <= 0) {
throw new Error(`[Bot] Invalid ${name}: expected a positive integer, got "${raw}"`);
}
return value;
};
export const botConfig: BotConfig = {
token: required("DISCORD_TOKEN"),
guildId: required("DISCORD_GUILD_ID"),
appUrl: required("APP_URL"),
opsChannelId: optional("DISCORD_OPS_CHANNEL_ID"),
announceChannelId: optional("DISCORD_ANNOUNCE_CHANNEL_ID"),
staffRoleIds: (optional("DISCORD_STAFF_ROLE_IDS") ?? "")
.split(",")
.map((id) => id.trim())
.filter((id) => id.length > 0),
sideOpsChannelId: optional("DISCORD_SIDE_OPS_CHANNEL_ID"),
communityZeusRoleId: optional("DISCORD_COMMUNITY_ZEUS_ROLE_ID"),
changelogChannelId: optional("DISCORD_CHANGELOG_CHANNEL_ID"),
announceMentionRoleId: optional("DISCORD_ANNOUNCE_MENTION_ROLE_ID"),
npcEventChannelId: optional("DISCORD_NPC_EVENT_CHANNEL_ID"),
pollChannelId: optional("DISCORD_POLL_CHANNEL_ID"),
debugChannelId: optional("DISCORD_DEBUG_CHANNEL_ID"),
attendancePollMs: pollMs("DISCORD_ATTENDANCE_POLL_MS", 60_000),
notificationPollMs: pollMs("DISCORD_NOTIFICATION_POLL_MS", 20_000),
evaluationPollMs: pollMs("DISCORD_EVALUATION_POLL_MS", 60_000),
readyReminderPollMs: pollMs("DISCORD_READY_REMINDER_POLL_MS", 60_000),
imminentReminderPollMs: pollMs("DISCORD_IMMINENT_POLL_MS", 60_000),
sideOpsNoticePollMs: pollMs("DISCORD_SIDE_OPS_POLL_MS", 1_800_000),
achievementPollMs: pollMs("DISCORD_ACHIEVEMENT_POLL_MS", 60_000),
presencePollMs: pollMs("DISCORD_PRESENCE_POLL_MS", 30_000),
npcEventPollMs: pollMs("DISCORD_NPC_EVENT_POLL_MS", 60_000),
pollPollMs: pollMs("DISCORD_POLL_POLL_MS", 60_000),
modlistBufferHours: parseHours("DISCORD_MODLIST_BUFFER_HOURS", 8),
};