Skip to main content
The Discord bot adapter allows you to trigger Magpie pipelines by mentioning the bot in a Discord server. The bot creates threads per task and archives them on completion.

Prerequisites

Before deploying the Discord bot, ensure you have:
  • Rust 1.75+ installed via rustup
  • Claude CLI configured (required for both Tier 1 and Tier 2 LLM operations)
  • GitHub CLI (gh) authenticated for creating PRs
  • A Discord application with a bot token
  • Git for cloning repositories

Creating a Discord Bot Application

1

Create Discord Application

  1. Go to the Discord Developer Portal
  2. Click New Application and give it a name (e.g., “Magpie”)
  3. Navigate to the Bot section in the left sidebar
  4. Click Add Bot to convert your application to a bot
2

Configure Bot Permissions

Under the Bot section:
  • Enable MESSAGE CONTENT INTENT (required to read message text)
  • Enable SERVER MEMBERS INTENT (optional, for user context)
Under OAuth2 → URL Generator:
  • Select scopes: bot
  • Select bot permissions:
    • Read Messages/View Channels
    • Send Messages
    • Send Messages in Threads
    • Create Public Threads
    • Manage Threads
3

Get Bot Token

  1. In the Bot section, click Reset Token
  2. Copy the token (you’ll need this for DISCORD_TOKEN)
  3. Keep this token secret — treat it like a password
4

Invite Bot to Server

  1. Use the OAuth2 URL from step 2 to invite the bot to your Discord server
  2. Select the server and authorize the requested permissions

Environment Configuration

The Discord bot requires several environment variables. Create a .env file or set them in your deployment environment:

Required Variables

# Discord Bot Token (from Discord Developer Portal)
DISCORD_TOKEN=your-discord-bot-token-here

Pipeline Configuration

# Repository settings
MAGPIE_REPO_DIR=.                    # Path to git repo (default: current directory)
MAGPIE_BASE_BRANCH=main              # Base branch for PRs (default: main)

# CI Commands
MAGPIE_TEST_CMD="cargo test"         # Test command for CI loop
MAGPIE_LINT_CMD="cargo clippy"       # Lint command for CI loop
MAGPIE_MAX_CI_ROUNDS=2               # Max CI retry rounds (default: 2)

# GitHub Organization (optional — enables multi-repo support)
MAGPIE_GITHUB_ORG=your-org-name      # Restrict to specific GitHub org

Optional: Plane Issue Tracking

# Self-hosted Plane integration for automatic issue creation
PLANE_BASE_URL=https://plane.yourcompany.com
PLANE_API_KEY=your-plane-api-key
PLANE_WORKSPACE_SLUG=your-workspace
PLANE_PROJECT_ID=project-uuid

Optional: Daytona Sandbox

# Use remote Daytona sandboxes instead of local execution
DAYTONA_API_KEY=your-daytona-api-key
DAYTONA_BASE_URL=https://app.daytona.io/api
DAYTONA_ORGANIZATION_ID=your-org-id
DAYTONA_SANDBOX_CLASS=small          # small | medium | large
DAYTONA_SNAPSHOT_NAME=magpie-devbox  # Pre-configured snapshot
DAYTONA_ENV="KEY1=val1,KEY2=val2"    # Additional env vars for sandbox

Building and Running

1

Build the Discord Bot

Build the magpie-discord binary from the workspace root:
cargo build --release -p magpie-discord
The compiled binary will be at target/release/magpie-discord.
First build takes 4-5 minutes due to transitive dependencies (Goose, candle, tree-sitter, etc.).
2

Run the Bot Locally

Set your environment variables and run:
# Load .env file
export $(cat .env | xargs)

# Run the bot
cargo run --release -p magpie-discord
Or run the compiled binary directly:
DISCORD_TOKEN=your-token ./target/release/magpie-discord
You should see:
INFO starting magpie-discord bot...
3

Test the Bot

In your Discord server:
  1. Mention the bot in any channel: @Magpie add a health check endpoint
  2. The bot will create a thread and reply with “Working on it…”
  3. After the pipeline completes, you’ll receive a status message with:
    • Pipeline status (Success/PartialSuccess/Failed)
    • PR URL (if created)
    • Plane issue ID (if configured)
    • CI status
  4. The thread will be automatically archived and locked

Production Deployment

Running as a Systemd Service

Create a systemd service file at /etc/systemd/system/magpie-discord.service:
[Unit]
Description=Magpie Discord Bot
After=network.target

[Service]
Type=simple
User=magpie
WorkingDirectory=/opt/magpie
EnvironmentFile=/opt/magpie/.env
ExecStart=/opt/magpie/magpie-discord
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable magpie-discord
sudo systemctl start magpie-discord
sudo systemctl status magpie-discord

Docker Deployment

See the Docker Setup Guide for containerized deployment options.

Process Management with PM2

Alternatively, use PM2 for process management:
pm2 start magpie-discord --name magpie-discord
pm2 save
pm2 startup

Architecture

The Discord bot implements the ChatPlatform trait from magpie-core:
  • Event Handler: Listens for messages mentioning the bot using Serenity (Discord library)
  • Thread Creation: Creates a new thread for each task to keep context isolated
  • Pipeline Trigger: Calls run_pipeline() with the Discord platform adapter
  • Thread Archival: Archives and locks threads after pipeline completion

Key Files

FilePurpose
crates/magpie-discord/src/main.rs:26Reads DISCORD_TOKEN and builds PipelineConfig
crates/magpie-discord/src/handler.rsSerenity event handler for message events
crates/magpie-discord/src/adapter.rsChatPlatform implementation for Discord
crates/magpie-discord/src/reply.rsFormats pipeline results for Discord

Gateway Intents

The bot requires these Discord Gateway intents (configured in code at main.rs:30):
GatewayIntents::GUILD_MESSAGES | 
GatewayIntents::MESSAGE_CONTENT | 
GatewayIntents::GUILDS

Troubleshooting

Bot doesn’t respond to mentions

Ensure MESSAGE CONTENT INTENT is enabled in Discord Developer Portal under Bot settings.
The bot needs:
  • Read Messages/View Channels
  • Send Messages
  • Send Messages in Threads
  • Create Public Threads
  • Manage Threads
Run with verbose logging:
RUST_LOG=info cargo run -p magpie-discord

Pipeline fails immediately

Test the Claude CLI:
claude -p "test" "say hello"
gh auth status
Ensure MAGPIE_REPO_DIR points to a valid git repository with remote configured.

Thread not archived after completion

This is expected if close_thread() fails. Check that the bot has Manage Threads permission in the channel.

Next Steps

Docker Setup

Containerize the Discord bot for easier deployment

Pipeline Configuration

Learn about all available pipeline settings

Blueprint Engine

Understand how Magpie orchestrates agent tasks

Org-Scoped Repos

Enable dynamic multi-repository support

Build docs developers (and LLMs) love