A Discord bot that integrates with CustomGPT's API to answer questions using your agent's knowledge base.
More information on GitHub - https://github.com/Poll-The-People/customgpt-integrations
Get you CustomGPT.ai RAG API key here, needed to use this integration.
Features
- AI-Powered Responses: Uses CustomGPT's API to provide intelligent answers
- Conversation Memory: Maintains context within Discord channels
- Starter Questions: Interactive buttons for common questions
- Source Citations: Shows sources for answers when available
- Rate Limiting: Prevents abuse with configurable limits
- Security: Channel and role-based access control
- Pagination: Handles long responses elegantly
- Real-time: Typing indicators and responsive UI
Setup Instructions
1. Prerequisites
- Python 3.8 or higher
- Discord Bot Token (Create a bot here)
- CustomGPT API Key and Agent ID (Get from CustomGPT)
2. Installation
# Clone the repository
git clone <your-repo-url>
cd discord
# Install dependencies
pip install -r requirements.txt
# Copy environment variables
cp .env.example .env3. Configuration
Edit .env file with your credentials:
# Required
DISCORD_BOT_TOKEN=your_discord_bot_token
CUSTOMGPT_API_KEY=your_customgpt_api_key
CUSTOMGPT_AGENT_ID=your_agent_id
# Optional (defaults shown)
DISCORD_COMMAND_PREFIX=!
RATE_LIMIT_PER_USER=10
RATE_LIMIT_PER_CHANNEL=304. Discord Bot Setup
- Go to Discord Developer Portal
- Create a New Application
- Go to Bot section and create a bot
- Copy the bot token
- IMPORTANT: Enable Privileged Gateway Intents:
- Scroll down to "Privileged Gateway Intents"
- Toggle ON the "MESSAGE CONTENT INTENT" (Required for the bot to read messages)
- Click "Save Changes"
- Go to OAuth2 > URL Generator
- Select scopes:
bot,applications.commands - Select permissions:
Send Messages,Embed Links,Read Message History,Use Slash Commands - Use the generated URL to invite the bot to your server
5. Running Locally
python bot.pyDeployment Options
Option 1: Railway (Recommended - Free Tier Available)
- Install Railway CLI:
npm install -g @railway/cli- Deploy:
railway login
railway init
railway up- Set environment variables in Railway dashboard
Option 2: Fly.io (Free Tier Available)
- Install Fly CLI:
curl -L https://fly.io/install.sh | sh- Deploy:
flyctl launch
flyctl deploy
flyctl secrets set DISCORD_BOT_TOKEN=xxx CUSTOMGPT_API_KEY=xxx CUSTOMGPT_AGENT_ID=xxxOption 3: Replit (Free with Limitations)
- Create new Python repl on Replit
- Upload all files
- Set environment variables in Secrets tab
- Run the bot
- Use UptimeRobot to keep it alive
Option 4: Heroku (No longer free)
See heroku/ directory for Heroku-specific files.
Option 5: VPS (Self-hosted)
- Use systemd service (see
discord.servicefile) - Or use Docker (see
Dockerfile)
Bot Commands
!ask [question]- Ask a question to the CustomGPT agent!help- Show interactive help menu!info- Display agent information!starters- Show starter questions with buttons!reset- Reset conversation in current channel
Advanced Configuration
Rate Limiting
Configure in .env:
RATE_LIMIT_PER_USER: Queries per minute per userRATE_LIMIT_PER_CHANNEL: Queries per minute per channelREDIS_URL: Optional Redis URL for distributed rate limiting
Access Control
Configure in .env:
ALLOWED_CHANNELS: Comma-separated channel IDsALLOWED_ROLES: Comma-separated role IDs
Redis Setup (Optional)
For distributed rate limiting across multiple bot instances:
- Free Redis: Redis Cloud (30MB free)
- Set
REDIS_URLin.env
Monitoring
- Check logs for errors
- Monitor rate limit hits
- Track API usage in CustomGPT dashboard
Troubleshooting
Bot not responding
- Check bot token is correct
- Verify bot has proper permissions
- Check allowed channels/roles configuration
PrivilegedIntentsRequired Error
- Go to Discord Developer Portal
- Select your application > Bot section
- Enable "MESSAGE CONTENT INTENT" under Privileged Gateway Intents
- Save changes and restart the bot
API errors
- Verify CustomGPT API key and Agent ID
- Check API rate limits
- Ensure agent is active
Rate limiting issues
- Adjust limits in
.env - Consider using Redis for better rate limiting
Security Best Practices
- Never commit
.envfile - Use environment variables in production
- Regularly rotate API keys
- Monitor for unusual activity
- Set appropriate rate limits
- Use channel/role restrictions
Resources
CustomGPT Links
- CustomGPT Landing Page
- Live Demo
- CustomGPT Starter Kit
- CustomGPT Integrations
- API Documentation
- Postman Collection
- MCP Documentation
- Office Hours
- YouTube Channel
Video Tutorial
Platform Documentation
- Discord.py Documentation: discordpy.readthedocs.io
