A Discord bot that integrates with OpenRouter APIs to provide AI chat capabilities. Features a web interface for managing multiple AI models and user-specific preferences.
- AI Chat: Ask questions using
/askcommand with intelligent responses - Conversation Memory: AI remembers previous conversations for context-aware responses
- Per-User Memory: Each user has their own separate conversation history
- Model Switching: Change AI models with
/changecommand and autocomplete - User Preferences: Each user maintains their own preferred AI model
- Web Interface: Flask web interface for managing models and settings
- Multiple Models: Support for unlimited OpenRouter AI models
- Real-time: Instant responses with comprehensive error handling
- Secure: Environment-based configuration with proper security practices
| Command | Description | Example |
|---|---|---|
/ping |
Check if the bot is working | /ping |
/ask <content> |
Ask the AI a question (remembers context) | /ask What is machine learning? |
/change <model> |
Change your preferred AI model | /change GPT-4 |
/models |
Show available models and your current selection | /models |
/clear_memory |
Clear your conversation history | /clear_memory |
/memory_info |
View your conversation memory statistics | /memory_info |
pip install -r requirements.txtCreate a .env file in the root directory:
DISCORD_BOT_TOKEN=your_discord_bot_token_here
GUILD_ID=your_guild_id_here
FLASK_SECRET_KEY=your_secret_key_here- Visit Discord Developer Portal
- Create a new application
- Navigate to the "Bot" section and create a bot
- Copy the bot token to your
.envfile - Enable these bot permissions:
- Send Messages
- Use Slash Commands
- Read Message History
- Invite the bot to your server using the OAuth2 URL
- Sign up at OpenRouter
- Obtain your API key from the dashboard
- Use the web interface to add models with their official names
python run.pyThe bot will automatically:
- Initialize the database
- Start the Flask web interface on
http://localhost:5000 - Connect to Discord and sync slash commands
Access the web interface at http://localhost:5000 to manage your AI models:
- Add Models: Configure new AI models with OpenRouter identifiers
- Edit Models: Modify existing model configurations
- Toggle Models: Enable/disable models for use
- Delete Models: Remove unused models
- Navigate to the web interface
- Click "Add New Model"
- Fill in the details:
- Model Name: Friendly name (e.g., "GPT-4", "Claude-3")
- Official Name: OpenRouter model identifier (e.g., "openai/gpt-4")
- API Key: Your OpenRouter API key
- Click "Add Model"
- Set Your Model: Use
/change <model_name>to set your preferred model (autocomplete available) - Ask Questions: Use
/ask <your question>to chat with the AI (remembers previous conversations) - Switch Models: Use
/change <model_name>to select a different AI model - Check Models: Use
/modelsto see available models and your current selection - Manage Memory:
- Use
/memory_infoto view your conversation statistics - Use
/clear_memoryto reset your conversation history for a fresh start
- Use
- Add Models: Use the web interface to add new AI models
- Monitor Usage: Check the bot logs for any issues
- Manage Models: Enable/disable models as needed
DiscordAIChatBot/
├── discord_bot.py # Main Discord bot with memory
├── flask_web.py # Flask web interface
├── openrouter_client.py # OpenRouter API client
├── database.py # Database models (includes ConversationHistory)
├── config.py # Configuration
├── update_memory_database.py # Memory database migration script
├── requirements.txt # Python dependencies
├── README.md # This file
└── templates/ # Flask HTML templates
├── base.html
├── index.html
├── add_model.html
└── edit_model.html
For a complete step-by-step setup guide, visit: Discord AI Setup Guide
This tutorial provides detailed instructions for:
- Discord bot creation and configuration
- Environment setup
- Memory functionality setup
- Local deployment
- Cloud deployment options
The bot now includes sophisticated conversation memory:
- Each user has their own separate conversation history
- The AI remembers up to the last 10 message exchanges for context
- Memory persists across bot restarts and sessions
- Conversations are stored securely in the database
/ask: Now context-aware - the AI remembers your previous messages/memory_info: Shows your conversation statistics and recent message preview/clear_memory: Clears your conversation history for a fresh start
- Each user's conversations are completely separate and private
- Memory can be cleared at any time using
/clear_memory - No conversation data is shared between users
- All messages are stored locally in your database
- Check if the bot token is correct
- Ensure the bot has proper permissions
- Verify the bot is online in Discord
- Check if the model is active in the web interface
- Verify the OpenRouter API key is correct
- Ensure the official model name is valid
- Check if Flask is running on port 5000
- Ensure no other application is using the port
- Check the console for any error messages
- Keep your Discord bot token and API keys secure
- Never share your
.envfile - Use strong, unique API keys for each model
- Regularly rotate your API keys
- Enable Railway's automatic HTTPS in production
This bot is ready for deployment on Railway! See the deployment guides:
- DEPLOYMENT.md - Complete deployment guide
- DEPLOYMENT_CHECKLIST.md - Pre-deployment checklist
Click the button above to deploy directly to Railway with our pre-configured template!
Template Features:
- AI/ML Category - Listed in Railway's AI/ML templates
- 3 Required Variables - Simple configuration
- Full-Stack Solution - Discord bot + web interface
We welcome contributions! Feel free to:
- Submit bug reports
- Suggest new features
- Submit pull requests
- Improve documentation
This project is open source and available under the MIT License.