$npx -y skills add rianvdm/product-ai-public --skill discord-botDiscord bot development on Cloudflare Workers — Discord API, Developer Portal, slash commands, Gateway WebSocket, Interactions Endpoint, bot permissions, intents, and Ed25519 verification.
| 1 | # Discord Bot Development |
| 2 | |
| 3 | Use this skill when working on Discord bots, Discord API integration, bot setup/configuration, or slash command registration. Triggered by mentions of Discord bots, Discord Developer Portal, Discord API, slash commands, Gateway, or Interactions Endpoint. |
| 4 | |
| 5 | ## Critical Rule: Always Web Search |
| 6 | |
| 7 | **The Discord Developer Portal UI changes frequently.** Do NOT rely on pre-trained knowledge for: |
| 8 | * Where settings are located in the Developer Portal |
| 9 | * OAuth2 flow steps |
| 10 | * Bot permission configuration |
| 11 | * Interactions Endpoint setup |
| 12 | * Install link generation |
| 13 | |
| 14 | **Always web search first** for any Developer Portal instructions. Use queries like: |
| 15 | * `Discord Developer Portal [specific task] 2026` (include the current year) |
| 16 | * `Discord bot setup [specific feature] latest` |
| 17 | |
| 18 | If search results are older than 6 months, note this to the user and suggest they verify in the portal. |
| 19 | |
| 20 | ## Discord Bot Architecture on Cloudflare Workers |
| 21 | |
| 22 | Two models for receiving Discord events: |
| 23 | |
| 24 | ### Interactions Endpoint (HTTP) |
| 25 | * Discord POSTs slash commands and component interactions to your Worker URL |
| 26 | * No persistent connection needed — pure request/response |
| 27 | * Set the URL in Developer Portal → General Information → Interactions Endpoint URL |
| 28 | * Requires Ed25519 signature verification on every request (`discord-interactions` npm package) |
| 29 | * Supports: slash commands, buttons, select menus, modals, autocomplete |
| 30 | |
| 31 | ### Gateway (WebSocket) |
| 32 | * Persistent WebSocket connection to `wss://gateway.discord.gg` |
| 33 | * Required for: reaction events, message events, presence, voice state, typing indicators |
| 34 | * On Cloudflare Workers: use a **Durable Object** with `new WebSocket(url)` for the outbound connection |
| 35 | * NOT `fetch()` with `Upgrade: websocket` — that doesn't work for `wss://` URLs |
| 36 | * DO won't hibernate while outbound WS is open — use alarm-based keepalive (30s interval) |
| 37 | * Deploys evict the DO; alarm reconnects within ~30 seconds |
| 38 | |
| 39 | ### Which Do You Need? |
| 40 | |
| 41 | | Feature | Interactions Endpoint | Gateway | |
| 42 | |---------|----------------------|---------| |
| 43 | | Slash commands | Yes | Not needed | |
| 44 | | Message reactions | No | Required | |
| 45 | | Message content | No | Required (with intent) | |
| 46 | | Typing indicators | No | Required | |
| 47 | | Presence updates | No | Required | |
| 48 | | Voice state | No | Required | |
| 49 | |
| 50 | Most bots need both: Interactions Endpoint for slash commands, Gateway for real-time events. |
| 51 | |
| 52 | ## Setting Up a New Discord Bot |
| 53 | |
| 54 | ### 1. Create Application |
| 55 | * Go to https://discord.com/developers/applications |
| 56 | * Click **New Application**, name it, click Create |
| 57 | * Copy **Application ID** and **Public Key** from General Information page |
| 58 | |
| 59 | ### 2. Get Bot Token |
| 60 | * Sidebar → **Bot** |
| 61 | * Click **Reset Token**, copy immediately (shown once) |
| 62 | * Toggle privileged intents as needed (see Intents section below) |
| 63 | |
| 64 | ### 3. Configure Installation |
| 65 | * Sidebar → **Installation** |
| 66 | * Enable **Guild Install** |
| 67 | * Select **Discord Provided Link** |
| 68 | * Under Default Install Settings → Guild Install: |
| 69 | * **Scopes:** `bot`, `applications.commands` |
| 70 | * **Permissions:** Select based on your bot's needs (see Permissions section) |
| 71 | |
| 72 | ### 4. Set Interactions Endpoint |
| 73 | * Sidebar → **General Information** |
| 74 | * Set **Interactions Endpoint URL** to your Worker's `/interactions` path |
| 75 | * Discord sends a PING to verify — your code must handle it (return Pong) |
| 76 | * Save — if verification fails, your endpoint isn't working |
| 77 | |
| 78 | ### 5. Register Slash Commands |
| 79 | * Use `PUT https://discord.com/api/v10/applications/{app_id}/commands` |
| 80 | * Global commands can take up to an hour to propagate |
| 81 | * Guild-specific commands update instantly (useful for testing) |
| 82 | |
| 83 | ### 6. Install to Server |
| 84 | * Copy the Install Link from the Installation page |
| 85 | * Paste in browser, select server, authorize |
| 86 | |
| 87 | ## Intents |
| 88 | |
| 89 | Gateway intents control which events Discord sends to your bot. |
| 90 | |
| 91 | **Standard intents** (no approval needed): |
| 92 | * `GUILDS` (1 << 0) — guild create/update/delete, role changes |
| 93 | * `GUILD_MESSAGES` (1 << 9) — message create/update/delete in guilds |
| 94 | * `GUILD_MESSAGE_REACTIONS` (1 << 10) — reaction add/remove |
| 95 | |
| 96 | **Privileged intents** (must be enabled in Developer Portal → Bot): |
| 97 | * `MESSAGE_CONTENT` (1 << 15) — access to message content via Gateway AND REST API. Without this, `message.content` is empty for messages the bot didn't send or wasn't mentioned in. |
| 98 | * `GUILD_MEMBERS` (1 << 1) — member join/leave/update events |
| 99 | * `GUILD_PRESENCES` (1 << 8) — presence (online/offline) updates |
| 100 | |
| 101 | **No approval needed for bots in <100 servers.** Over 100 servers requires Discord review. |
| 102 | |
| 103 | ## Common Permissions |
| 104 | |
| 105 | | Permission | Bit | When needed | |
| 106 | |-----------|-----|-------------| |
| 107 | | View Channels | 1 << 10 | See channels and receive events from them | |
| 108 | | Send Messages | 1 << 11 | Send messages (DMs, channel messages) | |
| 109 | | Read Message History | 1 << 16 | Fetch messages via REST API | |
| 110 | | Use |