Channels
Openclaw_discord_plugin
The Openclaw offical discord channel, currently do not respect the env setting as decribe in https://github.com/openclaw/openclaw/issues/27409 . this is a approach to use a plugin to fix the issue
Install
npm install
#
README
# OpenClaw Discord Proxy Plugin
A workaround plugin for OpenClaw's Discord channel that properly respects proxy configuration for both WebSocket and REST API calls.
## Problem
The official OpenClaw Discord channel has a bug (issue [#27409](https://github.com/openclaw/openclaw/issues/27409)) where:
- Gateway WebSocket connections respect the proxy configuration ✓
- REST API calls (sending messages) do NOT respect the proxy configuration ✗
- Result: Bot can receive messages but cannot send replies, showing "fetch failed" errors
This plugin provides a complete workaround by implementing a Discord channel handler with proper proxy support for both WebSocket and REST API calls.
## Architecture
### How the Plugin Works
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ OpenClaw Gateway │
│ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ Plugin System │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ Discord Proxy Plugin (This Plugin) │ │ │
│ │ │ │ │ │
│ │ │ ┌──────────────────┐ ┌──────────────────┐ │ │ │
│ │ │ │ createDiscord │───▶│ HttpsProxyAgent │ │ │ │
│ │ │ │ Client() │ │ Injection │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ - Configures │ │ - Wraps HTTP │ │ │ │
│ │ │ │ Intents │ │ requests │ │ │ │
│ │ │ │ - Sets up │ │ - Routes all │ │ │ │
│ │ │ │ Event │ │ traffic │ │ │ │
│ │ │ │ Handlers │ │ through proxy │ │ │ │
│ │ │ └──────────────────┘ └──────────────────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ │ ▼ ▼ │ │ │
│ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │
│ │ │ │ discord.js Client (v14) │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ │ • WebSocket Connection ──┐ │ │ │ │
│ │ │ │ • REST API Calls ────────┼──▶ Both use proxy agent │ │ │ │
│ │ │ │ • Event Processing │ │ │ │ │
│ │ │ └─────────────────────────────────────────────────────────┘ │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────────┐ │ │
│ │ │ Message Flow │ │ │
│ │ │ │ │ │
│ │ │ Inbound: Discord → Plugin → Session Key → OpenClaw Agent │ │ │
│ │ │ Outbound: OpenClaw Agent → Plugin → Discord (via proxy) │ │ │
│ │ └─────────────────────────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼
┌───────────────────────┐
│ HTTP Proxy │
│ (e.g., 127.0.0.1:7890)│
└───────────────────────┘
│
▼
┌───────────────────────┐
│ Discord API │
│ • Gateway (WS) │
│ • REST (HTTPS) │
└───────────────────────┘
```
### Key Fix: Proxy Injection Point
The critical fix for issue #27409 is injecting the proxy agent at client creation time:
```
┌─────────────────────────────────────────────────────────────────────┐
│ Official Channel (Bug) │ Discord Proxy Plugin (Fixed) │
│────────────────────────────────┼──────────────────────────────────│
│ │ │
│ Discord.js Client │ Discord.js Client │
│ ┌──────────────────────┐ │ ┌──────────────────────┐ │
│ │ WebSocket: ✓ Proxy │ │ │ WebSocket: ✓ Proxy │ │
│ │ REST: ✗ No Proxy │ │ │ REST: ✓ Proxy │ │
│ │ (fetch failed) │ │ │ (via HttpsProxyAgent)│ │
│ └──────────────────────┘ │ └──────────────────────┘ │
│ │ ▲ │
│ Config: proxy set │ │ │
│ Result: Partial working │ Config: proxy set │
│ │ Result: Full proxy support │
└───────────────────────────────┴────────────────────────────────────┘
The Fix (src/index.ts: createDiscordClient):
─────────────────────────────────────────────
if (proxyUrl) {
const proxyAgent = new HttpsProxyAgent(proxyUrl);
clientOptions.rest = {
agent: proxyAgent as any, // ← Injects proxy into REST calls
};
}
```
### Integration with OpenClaw
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ OpenClaw Ecosystem │
│ │
│ ┌─────────────┐ ┌──────────────┐ ┌─────────────────────────────┐ │
│ │ User │────▶│ Discord │────▶│ Discord Proxy Plugin │ │
│ │ Message │ │ Bot/Gateway │ │ - Message received │ │
│ │ │◀────│ │◀────│ - Session key generated │ │
│ └─────────────┘ └──────────────┘ │ - Forward to OpenClaw │ │
│ └──────────┬──────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────┐ │
│ │ OpenClaw Gateway │ │
│ │ - Session routing │ │
│ │ - Agent workspace │ │
│ │ - Memory/context │ │
│ └──────────┬──────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────┐ │
│ │ Pi Agent (LLM) │ │
│ │ - Process message │ │
│ │ - Generate response │ │
│ └──────────┬──────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────┐ │
│ │ Discord Proxy Plugin │ │
│ │ - Receive response │ │
│ │ - Send via proxy │ │
│ └──────────┬──────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────┐ │
│ │ Discord │ │
│ │ (via proxy)│ │
│ └──────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### Session Key Generation
The plugin generates OpenClaw-compatible session keys for proper message routing:
```typescript
// For DM messages:
{
agentId: 'default',
channel: 'discord',
accountId: 'default',
peer: '<user_id>'
}
// For guild messages:
{
agentId: 'default',
channel: 'discord',
accountId: 'default',
guildId: '<guild_id>',
channelId: '<channel_id>'
}
```
## Features
- ✅ **Full proxy support** for both WebSocket and REST API calls
- ✅ **Device ID mechanism** for session management and identification
- ✅ **Multi-account support** with per-account proxy configuration
- ✅ **All official channel features**: DMs, guilds, pairing, allowlists
- ✅ **Drop-in replacement** configuration (disable official, enable plugin)
- ✅ **Thread bindings** for subagent sessions
- ✅ **Interactive components** (buttons, selects, modals)
- ✅ **Voice channel support** (with proxy)
- ✅ **Exec approvals** via Discord buttons
## Installation
### Prerequisites
- Node.js >= 18.0.0
- OpenClaw >= 2024.1.0
- A Discord bot token (get from [Discord Developer Portal](https://d
... (truncated)
channels
Comments
Sign in to leave a comment