Skip to content

Add Discord bot with Gateway WebSocket forwarding#567

Draft
RSO wants to merge 3 commits intomainfrom
feat/discord-bot
Draft

Add Discord bot with Gateway WebSocket forwarding#567
RSO wants to merge 3 commits intomainfrom
feat/discord-bot

Conversation

@RSO
Copy link
Contributor

@RSO RSO commented Feb 25, 2026

Summary

  • Implements the Discord bot with a Gateway WebSocket forwarding pattern (inspired by vercel/chat)
  • Bot receives @mentions via Gateway, processes them through the platform-agnostic runBot() engine, and replies with AI responses
  • Includes spawn_cloud_agent tool support for coding tasks on GitHub repos

Depends on: #558

Architecture: Gateway-to-Webhook Forwarding

Discord Gateway (WebSocket via discord.js)
    |
    v
/api/discord/gateway  (cron-triggered, runs ~4 min)
    |  forwards MESSAGE_CREATE via HTTP with x-discord-gateway-token
    v
/discord/webhook  (unified handler)
    |  also handles PING verification + slash commands via Ed25519
    v
discord-bot.ts -> runBot() -> spawn_cloud_agent

Why Gateway is needed: Discord's HTTP Interactions API only delivers slash commands and component interactions -- not regular chat messages. To receive @bot mentions in channels, a Gateway WebSocket connection is required.

How it works:

  1. A cron job hits /api/discord/gateway every ~3 min
  2. It connects to Discord Gateway via discord.js, listens for raw events
  3. MESSAGE_CREATE events are forwarded as HTTP POST to /discord/webhook with x-discord-gateway-token header for auth
  4. The webhook handler processes the message: strips bot mention, resolves user names, adds hourglass reaction, runs the AI bot, posts reply, swaps to checkmark reaction

New files

File Purpose
src/app/api/discord/gateway/route.ts Cron-triggered Gateway WebSocket listener
src/app/discord/webhook/route.ts Unified webhook handler (interactions + forwarded events)
src/lib/discord-bot.ts Main bot processor (reuses runBot() engine)
src/lib/discord/auth.ts Auth token generation for bot user
src/lib/discord/verify-request.ts Ed25519 signature verification
src/lib/discord-bot/discord-utils.ts Mention stripping, name resolution, truncation
src/lib/discord-bot/discord-channel-context.ts Channel info + recent messages for AI context

Modified files

File Change
src/lib/integrations/discord-service.ts Added postDiscordMessage, addDiscordReaction, removeDiscordReaction
src/lib/bot-users/types.ts Added 'discord-bot' to BotType
src/lib/feature-detection.ts Added 'discord' to FEATURE_VALUES
src/lib/config.server.ts Added DISCORD_PUBLIC_KEY env var

New dependencies

discord.js, discord-interactions, discord-api-types

New env var

DISCORD_PUBLIC_KEY -- from the Discord Developer Portal, used for Ed25519 verification of HTTP Interactions

Cron setup (for Gateway)

{ "path": "/api/discord/gateway?cron_secret=...", "schedule": "*/3 * * * *" }

Base automatically changed from feat/discord-integration to main February 26, 2026 12:21
RSO added 3 commits February 26, 2026 13:22
Implement the full Discord bot following the Vercel chat architecture pattern:

- Gateway WebSocket listener (/api/discord/gateway) connects to Discord
  via discord.js, receives MESSAGE_CREATE events, and forwards them to
  the webhook handler for unified processing
- Webhook handler (/discord/webhook) processes both Discord HTTP
  Interactions (PING, slash commands) and forwarded Gateway events
- Bot processor reuses the platform-agnostic runBot() engine with
  spawn_cloud_agent tool support, conversation context, and GitHub
  repository context
- All bot API calls use the DISCORD_BOT_TOKEN (Bot auth), not OAuth tokens
- Message posting, reactions, and channel context fetching via Discord
  REST API v10

New dependencies: discord.js, discord-interactions, discord-api-types
New env var: DISCORD_PUBLIC_KEY (Ed25519 verification for HTTP Interactions)
discord.js depends on optional native modules (zlib-sync, bufferutil,
utf-8-validate) that webpack cannot bundle. Add them to
serverExternalPackages so Next.js resolves them at runtime via Node.js
instead of trying to bundle them.
@RSO RSO force-pushed the feat/discord-bot branch from 3fd2bcf to 9ed1bed Compare February 26, 2026 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant