Skip to content

pwarnock/discord-ai-platform

Repository files navigation

Discord AI Platform

A production-ready Discord bot integrated with n8n workflows for AI-powered chat automation. Built with TypeScript, Bun, and Turborepo.

Features

  • 🤖 Discord Bot Bridge - Forwards Discord messages to n8n workflows
  • 🧠 AI Chat Integration - Channel-aware conversation memory with session management
  • 📊 Distributed Tracing - OpenTelemetry + Jaeger for observability
  • 📝 Structured Logging - Pino logger with configurable log levels
  • 🔄 Workflow as Code - n8n-kit for TypeScript-based workflow development
  • Turborepo Monorepo - Parallel builds with intelligent caching
  • Pre-commit Hooks - Automated testing before commits
  • 🛠️ Just Task Runner - Simple command orchestration

Architecture

System Overview

graph LR
    A[Discord User] -->|Message| B[Discord Bot]
    B -->|Webhook| C[n8n Discord Bridge]
    C -->|Execute Workflow| D[n8n AI Chat]
    D -->|API Call| E[AI Model]
    E -->|Response| D
    D -->|Return| C
    C -->|Webhook Response| B
    B -->|Reply| A
    
    B -.->|Traces| F[Jaeger]
    B -.->|Logs| G[Pino Logger]
Loading

Session Management Flow

graph TD
    A[Discord Message] --> B{Is DM?}
    B -->|Yes| C[Session: discord-dm-userId]
    B -->|No| D[Session: discord-server-channelId]
    C --> E[AI Agent Memory]
    D --> E
    E --> F[Conversation History]
    F --> G[AI Response]
Loading

Deployment Architecture

graph TB
    subgraph "Development"
        A[TypeScript Code] -->|n8n-kit| B[Workflow JSON]
        B -->|Deploy| C[n8n Instance]
    end
    
    subgraph "Runtime"
        D[Discord Bot Container] -->|Webhook| C
        C -->|Execute| E[AI Agent]
        E -->|Memory| F[Simple Memory Store]
    end
    
    subgraph "Observability"
        D -.->|Traces| G[Jaeger]
        D -.->|Logs| H[Pino Logger]
    end
Loading

Project Structure

packages/
├── discord-bot/        # Discord.js bot with n8n webhook integration
└── n8n-workflows/      # n8n workflows as TypeScript code

Session Management

  • Server channels: discord-server-{channelId} - Shared conversation history per channel
  • Direct messages: discord-dm-{userId} - Private conversation per user

Prerequisites

  • Bun >= 1.3.3
  • Docker & Docker Compose
  • Just (optional, for task runner)
  • Discord Bot Token
  • n8n API Key (for workflow deployment)

Quick Start

1. Clone and Install

git clone https://github.com/pwarnock/discord-ai-platform.git
cd discord-ai-platform
bun install

2. Configure Environment

cp .env.example .env

Edit .env with your credentials:

DISCORD_BOT_TOKEN=your_discord_bot_token
TEST_CHANNEL_ID=your_test_channel_id
N8N_API_KEY=your_n8n_api_key

3. Start Services

# Using Just
just up

# Or using Docker Compose
docker-compose up -d

# Activate n8n webhooks (required after n8n restart)
just activate-webhooks

4. Run Tests

# Using Just
just test

# Or using Bun
bun run test

Available Commands

Just Commands

just                    # List all commands
just test               # Run all tests
just build              # Build all packages
just up                 # Start services
just down               # Stop services
just logs [service]     # View logs
just backup             # Backup workflows (timestamped)
just backup-latest      # Backup to latest/
just sync               # Check for workflow changes and export
just delete-workflow ID # Delete workflow (backs up first)
just deploy-all         # Full pipeline: backup → build → deploy
just dev                # Development mode with watch
just test-webhook       # Test Discord webhook

Turborepo Commands

turbo test              # Run tests in all packages
turbo build             # Build all packages
turbo deploy            # Deploy workflows to n8n
turbo dev               # Development mode

n8n Workflow Management

# Export workflows from n8n
docker exec n8n n8n export:workflow --backup --output=backups/latest/

# Import workflow to TypeScript (requires N8N_API_KEY)
cd packages/n8n-workflows
export N8N_BASE_URL=http://localhost:5678
export N8N_API_KEY=your_api_key
bun run import -- --id=<workflow-id> --out=src/workflow.ts

# Deploy workflows from TypeScript
bun run deploy

# Watch mode for development
bun run watch

Development

Project Structure

.
├── packages/
│   ├── discord-bot/              # Discord bot package
│   │   ├── index.ts              # Main bot logic
│   │   ├── logger.ts             # Pino logger setup
│   │   ├── tracing.ts            # OpenTelemetry config
│   │   └── index.test.ts         # Bun tests
│   └── n8n-workflows/            # n8n workflows package
│       ├── src/                  # TypeScript workflows
│       └── n8n-kit.config.ts     # n8n-kit configuration
├── code-nodes/                   # Extracted code node scripts
├── backups/                      # n8n workflow exports
│   ├── latest/                   # Latest backup
│   └── YYYYMMDD-HHMMSS/         # Timestamped backups
├── compose.yaml                  # Docker Compose config
├── turbo.json                    # Turborepo pipeline
├── justfile                      # Just task definitions
└── README.md

Adding New Workflows

  1. Create workflow in n8n UI
  2. Export to TypeScript:
    just import <workflow-id>
  3. Edit TypeScript in packages/n8n-workflows/src/
  4. Deploy:
    just deploy-all

Running Tests

Tests run automatically on pre-commit. To run manually:

just test

Discord bot tests are in packages/discord-bot/index.test.ts.

Observability

Jaeger Tracing

Access Jaeger UI at http://localhost:16686

Traces show:

  • Discord message flow
  • n8n webhook calls
  • AI agent interactions
  • Response handling

Logs

View logs with:

just logs              # All services
just logs discord-bot  # Discord bot only
just logs n8n          # n8n only

Log levels: error, warn, info, debug, trace

Set via LOG_LEVEL environment variable.

Deployment

Backup Before Deploy

just backup

Full Deployment Pipeline

just deploy-all

This runs:

  1. Backup current workflows
  2. Build all packages
  3. Deploy to n8n

Manual Deployment

just build
cd packages/n8n-workflows
bun run deploy

Testing Discord Integration

Test Webhook

just test-webhook "Hello from test"

Manual Test

curl -X POST http://localhost:5678/webhook-test/discord \
  -H "Content-Type: application/json" \
  -d '{
    "content": "Test message",
    "author": {"id": "123456789"},
    "guild": {"id": "987654321"}
  }'

Troubleshooting

Discord Bot Not Responding

  1. Check bot is running: docker ps
  2. View logs: just logs discord-bot
  3. Verify webhook URL in bot config
  4. Test webhook: just test-webhook
  5. Ensure MESSAGE CONTENT intent is enabled in Discord Developer Portal
  6. Reactivate n8n webhooks: just activate-webhooks

n8n Workflow Issues

  1. Reactivate webhooks after restart: just activate-webhooks
  2. Check workflow is active in n8n UI
  3. View execution logs in n8n
  4. Verify API key is set: echo $N8N_API_KEY
  5. Re-export workflows: just backup-latest

Tests Failing

  1. Ensure services are running: just up
  2. Check test output: just test
  3. Run specific package tests:
    cd packages/discord-bot
    bun test

Contributing

  1. Create a feature branch
  2. Make changes
  3. Tests run automatically on commit (pre-commit hook)
  4. Push and create PR

License

MIT

Tech Stack

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors