Trivia
Rendered from docs/discord/trivia/README.md
Generated on every docs build. Edit the source in docs/, not here.
Multiple-choice trivia powered by Open Trivia DB (free, no API key needed). One player starts a round; anyone in the channel can be first to answer within 20 seconds. The bot posts a question as an embed with four A/B/C/D buttons, announces the winner instantly, and disables the buttons when time runs out.
Command surface
| Command | Effect |
|---|---|
/trivia [category] [difficulty] | Fetch one question and post it with four answer buttons. |
category and difficulty are optional slash-option choices. Categories map to
OpenTDB's category IDs (hard-coded at startup to avoid a network call on boot).
How it works
/trivia startdefers the reply while fetching from OpenTDB.- The bot GETs
https://opentdb.com/api.php?amount=1&type=multiple&…. On any network error or non-zeroresponse_code, it silently falls back to a bundled set of ~50 hand-written questions. - HTML entities in the question and answers are decoded with a small helper (no extra dependencies).
- The four answers (correct + 3 incorrect) are shuffled;
correctIdxrecords where the correct answer landed. - The embed is posted with buttons
trv:<sessionId>:0…trv:<sessionId>:3. A 20-secondsetTimeoutis scheduled to expire the session. - Correct answer clicked: the session is closed immediately, buttons are disabled (correct = green, others = grey), footer shows the winner.
- Wrong answer clicked: ephemeral reply "Not quite. Try again." — buttons stay active so others can still answer.
- Timer fires with no winner: the bot edits the original message to disable buttons, highlight the correct answer, and show "⏱️ Time's up".
All state is in-memory. No disk persistence in v1.
Source layout
| Concern | Location |
|---|---|
| Session type + store | bots/discord/src/trivia/session.ts |
| OpenTDB fetch + fallback dispatch | bots/discord/src/trivia/api.ts |
| Bundled fallback questions (~50) | bots/discord/src/trivia/fallback.ts |
| HTML entity decoder | bots/discord/src/trivia/decode.ts |
| Embed + button builders | bots/discord/src/trivia/render.ts |
| Slash command + button handler | bots/discord/src/commands/trivia.ts |
Design notes
- No API key. OpenTDB's free tier is plenty for a Discord bot. Rate limits would only matter under sustained rapid use; the fallback set covers any transient failures.
- In-memory sessions only. Sessions live for at most 20 seconds, so there is
no value in writing them to disk. Score tracking (v2) will use
data/trivia/<guildId>.json. - Concurrent sessions. Multiple
/triviacalls in the same channel run as independent sessions keyed by a short random ID embedded in each button'scustomId. No locking required. - Timer edits the original reply. The
ChatInputCommandInteractionis stored on the session so the expirysetTimeoutcan calleditReply. Interaction tokens last 15 minutes, well beyond the 20-second window. - Four-button layout in one action row. Discord limits action rows to 5 buttons; ABCD fits cleanly in a single row.