Architecture Overview
The bot uses Effect for dependency injection, error handling, and functional composition. All services are organized as Effect Layers that can be composed together.Core Stack
- discord.js 14.25+ - Discord API client
- Effect - Functional effects system for composition and error handling
- Reacord - React-based UI components for Discord interactions
- Convex - Real-time database (via @packages/database)
- Sentry - Error tracking and observability
Project Structure
Bot Service Layer
The bot composes all feature layers insrc/bot.ts:38:
Discord Service
The Discord service (src/core/discord-service.ts) wraps discord.js with Effect for type-safe error handling and observability.
Key Features
- Automatic metrics - API calls and errors tracked via Prometheus metrics
- Effect-based event handlers - Event handlers return
Effectfor composable error handling - Fiber tracking - Active event handler fibers are tracked for graceful shutdown
- Unified error types -
DiscordAPIErrorandUnknownDiscordErrorfor all Discord operations
Usage Example
Commands
Mark Solution
Location:src/commands/mark-solution.ts
Context menu command that marks a message as the solution to a thread question.
- Features
- Code Example
- Permission checking (question author, ManageThreads, or Admin)
- Automatic tag management (apply solution tag, remove “needs help” tags)
- Thread archiving/locking based on server preferences
- Checkmark reactions on solution and question
- Analytics tracking via PostHog
- 25-second timeout with error reporting
Channel Settings
Location:src/commands/channel-settings.ts
Slash command that generates a dashboard link for channel configuration.
Leaderboard
Location:src/commands/leaderboard.ts
Displays top question solvers for the server.
Index Command
Location:src/commands/index-command.ts
Superuser-only DM command for triggering manual indexing.
Commands
!index start all- Index all servers!index start <serverId>- Index specific server!index status- Check if indexing is running!index help- Show help message
Services
Indexing Service
Location:src/services/indexing.ts
Backfills messages from Discord into the database.
- Iterates through all guilds the bot is in
- Fetches messages from enabled channels
- Batches upserts to database
- Uses semaphore for concurrency control
Auto Thread Service
Location:src/services/auto-thread.ts
Automatically creates threads on messages in configured channels.
DM Forwarding
Location:src/services/dm-forwarding.ts
Forwards DMs to the bot to a configured channel for support.
Gateway Health
Location:src/services/gateway-health.ts
Monitors Discord gateway connection health and reports metrics.
Sync Services
Sync services maintain data parity between Discord and the database.Server Sync
Location:src/sync/server.ts
Listens to:
guildCreate- Bot joins new serverguildUpdate- Server name/icon changesguildDelete- Bot leaves server
Channel Sync
Location:src/sync/channel.ts
Syncs channel creates, updates, deletes, and thread lifecycle events.
Message Sync
Location:src/sync/message.ts
Syncs message creates, updates, and deletes to database.
User Sync
Location:src/sync/user.ts
Tracks user join/leave events and profile updates.
Metrics
The bot exposes Prometheus metrics:Error Handling
All commands use standardized error handling utilities fromsrc/utils/error-reporting.ts:
Development
Running Locally
Environment Variables
Testing
Deployment
The bot is deployed as a Docker container:Related Resources
- Main Site - Next.js web application
- Bot E2E Tests - Integration tests for bot commands
- Database Package - Convex database schema and queries