Discord Bot Setup
Prerequisites
Collect from user via AskUserQuestion:
- Bot Token — from Discord Developer Portal
- Discord User ID — for whitelist (Developer Mode > right-click avatar > Copy User ID)
- Auto-respond Channel ID — (optional) channel where bot responds without @mention
- STT engine — gemini (recommended) / chirp2 / whisper:medium (default: gemini)
- GCP Project — for Gemini/Chirp2 STT (default: use gcloud config)
If no Bot Token, guide user through Discord Developer Portal setup:
- Create Application > Bot > Reset Token > enable all 3 Intents (Presence, Server Members, Message Content)
- OAuth2 > Scopes: bot, applications.commands > Permissions: Send Messages, Read Message History, Embed Links, Attach Files, View Channels
- Disable User Install in Installation settings (security)
Installation
pip install py-cord --break-system-packages
sudo apt-get install -y ffmpeg jq
pip install google-genai --break-system-packages # Gemini STT (recommended)
pip install openai-whisper --break-system-packages # Whisper fallback
Deploy Bot
- Read
scripts/bot_template.py — this is the complete, production-ready bot script
- Copy to
~/.claude/discord-bot/bot.py
- Create
~/.claude/discord-bot/.env with config:DISCORD_BOT_TOKEN=<bot_token>
ALLOWED_USER_IDS=<user_discord_id>
AUTO_RESPOND_CHANNELS=<channel_id>
STT_ENGINE=gemini
GOOGLE_CLOUD_PROJECT=<gcp_project>
CHIRP2_LOCATION=us-central1
- Copy
scripts/send-to-discord.sh to ~/.claude/scripts/send-to-discord.sh and set BOT_TOKEN/CHANNEL_ID
chmod +x ~/.claude/scripts/send-to-discord.sh
STT Engine Configuration
Three engines available, configured via STT_ENGINE in .env:
gemini (recommended) — Uses Gemini multimodal LLM for transcription. Understands semantics, corrects homophones, handles tech terms. Requires google-genai SDK and GCP project with Vertex AI API enabled.
- Current best:
gemini-3-flash-preview with thinking_level=MINIMAL (~3.4s, excellent quality)
- Alternative:
gemini-2.0-flash (~3s, excellent quality, more regions)
- Budget option:
gemini-2.5-flash-lite (~2.1s, good quality)
chirp2 — Google Cloud Speech-to-Text V2 (Chirp 2). Pure ASR, no semantic understanding. Poor with homophones.
whisper:medium — Local OpenAI Whisper. No network dependency, ~2-3s, ok quality.
Fallback chain: Gemini → Chirp 2 → Whisper (automatic on failure)
Important: Gemini 3 Flash Preview only available in global region. Other Gemini models work in us-central1.
Start Bot
mkdir -p ~/.claude/discord-bot
# Copy wrapper script
cp scripts/run.sh ~/.claude/discord-bot/run.sh
chmod +x ~/.claude/discord-bot/run.sh
# Start in tmux (survives shell disconnect, wrapper auto-restarts on /restart)
tmux new-session -d -s discord-bot 'bash ~/.claude/discord-bot/run.sh'
Verify: tmux ls and tail -5 ~/.claude/discord-bot/bot.log
Critical: Only one bot process must run. Multiple processes = duplicate messages.
Memory Setup
For auto memory to work across sessions:
- Ensure
~/.claude/CLAUDE.md exists with global preferences
- Create
~/.claude/projects/-home-<user>/memory/MEMORY.md as memory index
- Bot already passes
CLAUDE_CODE_DISABLE_AUTO_MEMORY=0 to Claude subprocess
Slash Commands
| Command |
Function |
/status |
Bot status, active Claude processes, Whisper model |
/end |
Archive current session, stop Claude process |
/sessions |
List history with summaries + dropdown switcher |
/restart |
Graceful restart (exit code 42 → wrapper auto-restarts) |
Key Features
- Persistent Claude process per user via
socket.socketpair() — full interactive mode
- Session history —
/end archives, /sessions shows dropdown to switch back
- Gemini STT — multimodal voice transcription with semantic understanding (understands context, corrects homophones)
- STT fallback chain — Gemini → Chirp 2 → Whisper (auto-fallback on failure)
- Smart message splitting — split at newlines for Discord's 2000 char limit
- Process auto-restart — if Claude dies, recreate transparently on next message
- Graceful restart —
/restart command triggers exit code 42, wrapper script auto-restarts
- FD isolation —
subprocess.Popen(close_fds=True) prevents Claude from inheriting Discord websocket FD
- Safety prompt — system prompt forbids Claude subprocess from killing/restarting bot process
References
- Architecture details: See references/architecture.md for socketpair protocol, message format, session management internals, and security model
- Troubleshooting: See references/troubleshooting.md for common issues and fixes
Files Created
~/.claude/discord-bot/
├── bot.py # Main bot script
├── run.sh # Wrapper script (auto-restart on exit code 42)
├── bot.log # Runtime logs (tail -f)
└── sessions.json # Per-user session mapping (auto-created)
~/.claude/scripts/
└── send-to-discord.sh # Claude Code → Discord messaging
1---2name: discord-bot-setup3description: Deploy a Discord Bot that connects to Claude Code via persistent processes using Unix socketpair + stream-json (same mechanism as VSCode extension). Each user gets their own long-running Claude process with full interactive mode support (auto memory, CLAUDE.md, skills). Includes Whisper voice transcription, user whitelist, session history with dropdown switcher, and slash commands. Use when the user says "帮我建一个 Discord Bot", "setup discord bot", "搭建 Discord 机器人", "discord bot 设置", or "部署 discord bot".4---56# Discord Bot Setup78## Prerequisites910Collect from user via AskUserQuestion:111. **Bot Token** — from Discord Developer Portal122. **Discord User ID** — for whitelist (Developer Mode > right-click avatar > Copy User ID)133. **Auto-respond Channel ID** — (optional) channel where bot responds without @mention144. **STT engine** — gemini (recommended) / chirp2 / whisper:medium (default: gemini)155. **GCP Project** — for Gemini/Chirp2 STT (default: use gcloud config)1617If no Bot Token, guide user through Discord Developer Portal setup:18- Create Application > Bot > Reset Token > enable all 3 Intents (Presence, Server Members, **Message Content**)19- OAuth2 > Scopes: bot, applications.commands > Permissions: Send Messages, Read Message History, Embed Links, Attach Files, View Channels20- **Disable User Install** in Installation settings (security)2122## Installation2324```bash25pip install py-cord --break-system-packages26sudo apt-get install -y ffmpeg jq27pip install google-genai --break-system-packages # Gemini STT (recommended)28pip install openai-whisper --break-system-packages # Whisper fallback29```3031## Deploy Bot32331. Read `scripts/bot_template.py` — this is the complete, production-ready bot script342. Copy to `~/.claude/discord-bot/bot.py`353. Create `~/.claude/discord-bot/.env` with config:36 ```37 DISCORD_BOT_TOKEN=<bot_token>38 ALLOWED_USER_IDS=<user_discord_id>39 AUTO_RESPOND_CHANNELS=<channel_id>40 STT_ENGINE=gemini41 GOOGLE_CLOUD_PROJECT=<gcp_project>42 CHIRP2_LOCATION=us-central143 ```444. Copy `scripts/send-to-discord.sh` to `~/.claude/scripts/send-to-discord.sh` and set BOT_TOKEN/CHANNEL_ID455. `chmod +x ~/.claude/scripts/send-to-discord.sh`4647## STT Engine Configuration4849Three engines available, configured via `STT_ENGINE` in `.env`:5051- **`gemini`** (recommended) — Uses Gemini multimodal LLM for transcription. Understands semantics, corrects homophones, handles tech terms. Requires `google-genai` SDK and GCP project with Vertex AI API enabled.52 - Current best: `gemini-3-flash-preview` with `thinking_level=MINIMAL` (~3.4s, excellent quality)53 - Alternative: `gemini-2.0-flash` (~3s, excellent quality, more regions)54 - Budget option: `gemini-2.5-flash-lite` (~2.1s, good quality)55- **`chirp2`** — Google Cloud Speech-to-Text V2 (Chirp 2). Pure ASR, no semantic understanding. Poor with homophones.56- **`whisper:medium`** — Local OpenAI Whisper. No network dependency, ~2-3s, ok quality.5758Fallback chain: Gemini → Chirp 2 → Whisper (automatic on failure)5960**Important**: Gemini 3 Flash Preview only available in `global` region. Other Gemini models work in `us-central1`.6162## Start Bot6364```bash65mkdir -p ~/.claude/discord-bot66# Copy wrapper script67cp scripts/run.sh ~/.claude/discord-bot/run.sh68chmod +x ~/.claude/discord-bot/run.sh69# Start in tmux (survives shell disconnect, wrapper auto-restarts on /restart)70tmux new-session -d -s discord-bot 'bash ~/.claude/discord-bot/run.sh'71```7273Verify: `tmux ls` and `tail -5 ~/.claude/discord-bot/bot.log`7475**Critical**: Only one bot process must run. Multiple processes = duplicate messages.7677## Memory Setup7879For auto memory to work across sessions:801. Ensure `~/.claude/CLAUDE.md` exists with global preferences812. Create `~/.claude/projects/-home-<user>/memory/MEMORY.md` as memory index823. Bot already passes `CLAUDE_CODE_DISABLE_AUTO_MEMORY=0` to Claude subprocess8384## Slash Commands8586| Command | Function |87|---------|----------|88| `/status` | Bot status, active Claude processes, Whisper model |89| `/end` | Archive current session, stop Claude process |90| `/sessions` | List history with summaries + dropdown switcher |91| `/restart` | Graceful restart (exit code 42 → wrapper auto-restarts) |9293## Key Features9495- **Persistent Claude process** per user via `socket.socketpair()` — full interactive mode96- **Session history** — `/end` archives, `/sessions` shows dropdown to switch back97- **Gemini STT** — multimodal voice transcription with semantic understanding (understands context, corrects homophones)98- **STT fallback chain** — Gemini → Chirp 2 → Whisper (auto-fallback on failure)99- **Smart message splitting** — split at newlines for Discord's 2000 char limit100- **Process auto-restart** — if Claude dies, recreate transparently on next message101- **Graceful restart** — `/restart` command triggers exit code 42, wrapper script auto-restarts102- **FD isolation** — `subprocess.Popen(close_fds=True)` prevents Claude from inheriting Discord websocket FD103- **Safety prompt** — system prompt forbids Claude subprocess from killing/restarting bot process104105## References106107- **Architecture details**: See [references/architecture.md](references/architecture.md) for socketpair protocol, message format, session management internals, and security model108- **Troubleshooting**: See [references/troubleshooting.md](references/troubleshooting.md) for common issues and fixes109110## Files Created111112```113~/.claude/discord-bot/114├── bot.py # Main bot script115├── run.sh # Wrapper script (auto-restart on exit code 42)116├── bot.log # Runtime logs (tail -f)117└── sessions.json # Per-user session mapping (auto-created)118119~/.claude/scripts/120└── send-to-discord.sh # Claude Code → Discord messaging121```