diff --git a/src/bot/AGENTS.md b/src/bot/AGENTS.md index d59a4725..55751237 100644 --- a/src/bot/AGENTS.md +++ b/src/bot/AGENTS.md @@ -48,6 +48,7 @@ bot/ lib/ debugActions.ts # /debug action registry: each action builds the production message and sends it live + interactions.ts # safeEphemeralReply: error-path reply that never throws (stale interaction = 10062) roles.ts # isStaff check resolve.ts # discordId ↔ Payload user lookups transferMessaging.ts # Transfer embed builders + delivery keys @@ -132,9 +133,20 @@ A poll loop (`services/discordPresence.ts`, `DISCORD_PRESENCE_POLL_MS`, default | Edit bot Rich Presence (command) | `bot/commands/discordPresence.ts` (+ Global `src/collections/discord/DiscordPresence.ts`) | | Rich Presence reconcile loop / mapping | `bot/services/discordPresence.ts` + `bot/lib/presence.ts` | +## Interaction reliability + +Discord kills an interaction token that is not acknowledged within **3 seconds**: the user sees "The application did not respond", and every later reply (`deferReply` included) fails with `DiscordAPIError: Unknown interaction` (10062). Two rules follow, both enforced by `tests/int/bot-commands.int.spec.ts`: + +1. **Ack first, then work.** Guild commands must `deferReply` as their first Discord call, before `guild.members.fetch()` and `isStaff()`. `/rollcall`, `/remind-evaluations`, and `/debug` defer up front and report permission denials through `editReply`. Commands that must open a modal (`/announce`, `/changelog edit=true`, `/discord-presence edit`) cannot defer, so their staff check runs before the modal and the modal is the very next call. +2. **Error paths never reply unguarded.** A reply inside a `catch` throws whenever the interaction is already dead, and that rejection is what crashed the process on 2026-11-03. Use `safeEphemeralReply()` (`lib/interactions.ts`) or a local try/catch. + +`client.on(Events.Error)` in `index.ts` is the last line of defence: discord.js re-emits any rejected event listener on the client, and without a handler Node throws and the whole bot exits (the entrypoint then restarts it after 5s, so the symptom is a silent gap, not a visible crash). + ## Anti-patterns - **NEVER** run bot and web app with separate database connections without connection pooling — they share PostgreSQL - **NEVER** register guild-only commands if the command needs to work in DMs (signup, link, ping must be global) - **NEVER** assume DM delivery succeeded — `/signup` uses ephemeral reply as primary delivery path - **NEVER** modify `discordId` directly in Payload admin — use the `/link` command or the resolve helper +- **NEVER** fetch the guild member or run `isStaff` before `deferReply` on a guild command; that is a 3-second ack race and produces "The application did not respond" +- **NEVER** `await interaction.reply(...)` directly inside a catch block; use `safeEphemeralReply` so a stale interaction (10062) cannot take the bot down