forked from claude-did-this/claude-hub
- Add required 'repo' parameter for repository specification - Add optional 'branch' parameter (defaults to 'main') - Implement extractRepoAndBranch() method in DiscordProvider - Add repository validation in chatbotController - Update parseWebhookPayload to include repo/branch context - Enhanced error messages for missing repository parameter - Updated all tests to handle new repo/branch fields - Added comprehensive test coverage for new functionality Discord slash command now requires: /claude repo:owner/repository command:your-instruction /claude repo:owner/repository branch:feature command:your-instruction 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
3.8 KiB
3.8 KiB
Discord Chatbot Provider Setup
Overview
This implementation provides a comprehensive chatbot provider system that integrates Claude with Discord using slash commands. The system requires repository and branch parameters to function properly.
Architecture
- ChatbotProvider.js: Abstract base class for all chatbot providers
- DiscordProvider.js: Discord-specific implementation with Ed25519 signature verification
- ProviderFactory.js: Dependency injection singleton for managing providers
- chatbotController.js: Generic webhook handler working with any provider
- chatbot.js: Express routes with rate limiting
Required Environment Variables
DISCORD_BOT_TOKEN=your_discord_bot_token
DISCORD_PUBLIC_KEY=your_discord_public_key
DISCORD_APPLICATION_ID=your_discord_application_id
DISCORD_AUTHORIZED_USERS=user1,user2,admin
DISCORD_BOT_MENTION=claude
Discord Slash Command Configuration
In the Discord Developer Portal, create a slash command with these parameters:
- Command Name:
claude - Description:
Ask Claude to help with repository tasks - Parameters:
repo(required, string): Repository in format "owner/name"branch(optional, string): Git branch name (defaults to "main")command(required, string): Command for Claude to execute
API Endpoints
POST /api/webhooks/chatbot/discord- Discord webhook handler (rate limited: 100 req/15min per IP)GET /api/webhooks/chatbot/stats- Provider statistics and status
Usage Examples
/claude repo:owner/myrepo command:help me fix this bug
/claude repo:owner/myrepo branch:feature command:review this code
/claude repo:owner/myrepo command:add error handling to this function
Security Features
- Ed25519 webhook signature verification
- User authorization checking
- Repository parameter validation
- Rate limiting (100 requests per 15 minutes per IP)
- Container isolation for Claude execution
- Input sanitization and validation
Installation
-
Install dependencies:
npm install -
Set up environment variables in
.env:DISCORD_BOT_TOKEN=your_token DISCORD_PUBLIC_KEY=your_public_key DISCORD_APPLICATION_ID=your_app_id DISCORD_AUTHORIZED_USERS=user1,user2 -
Configure Discord slash command in Developer Portal
-
Start the server:
npm start # or for development npm run dev
Testing
# Run all unit tests
npm run test:unit
# Run specific provider tests
npm test -- test/unit/providers/DiscordProvider.test.js
# Run controller tests
npm test -- test/unit/controllers/chatbotController.test.js
Key Features Implemented
- Repository Parameter Validation: Commands require a
repoparameter in "owner/name" format - Branch Support: Optional
branchparameter (defaults to "main") - Error Handling: Comprehensive error messages with reference IDs
- Rate Limiting: Protection against abuse with express-rate-limit
- Message Splitting: Automatic splitting for Discord's 2000 character limit
- Comprehensive Testing: 35+ unit tests covering all scenarios
Workflow
- User executes Discord slash command:
/claude repo:owner/myrepo command:fix this issue - Discord sends webhook to
/api/webhooks/chatbot/discord - System verifies signature and parses payload
- Repository parameter is validated (required)
- Branch parameter is extracted (defaults to "main")
- User authorization is checked
- Command is processed by Claude with repository context
- Response is sent back to Discord (automatically split if needed)
Extension Points
The architecture supports easy addition of new platforms:
- Implement new provider class extending ChatbotProvider
- Add environment configuration in ProviderFactory
- Register provider and add route handler
- System automatically handles authentication, validation, and Claude integration