A Discord bot that uses the OpenRouter API to provide AI-powered responses to user questions through slash commands.
- 🤖 AI-powered responses using OpenRouter API
- 📝 Customizable system prompt via XML file
- ⚡ Slash command
/askfor easy interaction - 🎨 Beautiful embed responses
- 🔧 Configurable AI model and parameters
- 🛡️ Error handling and rate limiting
- Node.js 16.9.0 or higher
- A Discord bot token
- An OpenRouter API key
git clone <your-repo-url>
cd ai-bot
npm install-
Copy the example environment file:
cp env.example .env
-
Edit
.envand add your credentials:# Discord Bot Configuration DISCORD_TOKEN=your_discord_bot_token_here DISCORD_CLIENT_ID=your_discord_client_id_here # OpenRouter API Configuration OPENROUTER_API_KEY=your_openrouter_api_key_here OPENROUTER_BASE_URL=https://openrouter.ai/api/v1 # Bot Configuration DEFAULT_MODEL=anthropic/claude-3.5-sonnet MAX_TOKENS=1000 TEMPERATURE=0.7
- Go to the Discord Developer Portal
- Create a new application
- Go to the "Bot" section and create a bot
- Copy the bot token and add it to your
.envfile - Go to "OAuth2" > "URL Generator"
- Select "bot" and "applications.commands" scopes
- Select the permissions you want (at minimum: "Send Messages", "Use Slash Commands")
- Use the generated URL to invite the bot to your server
- Go to OpenRouter
- Create an account and get your API key
- Add the API key to your
.envfile
node deploy-commands.jsnpm startFor development with auto-restart:
npm run devOnce the bot is running, users can interact with it using the /ask slash command:
/ask question: What is the capital of France?
The bot will respond with an AI-generated answer in a beautiful embed format.
The bot's behavior is controlled by the system-prompt.xml file. You can customize:
- Role: The bot's primary function
- Guidelines: How the bot should behave
- Limitations: What the bot should not do
- Formatting: How responses should be formatted for Discord
DISCORD_TOKEN: Your Discord bot tokenDISCORD_CLIENT_ID: Your Discord application client IDOPENROUTER_API_KEY: Your OpenRouter API keyOPENROUTER_BASE_URL: OpenRouter API base URL (usually doesn't need to change)DEFAULT_MODEL: The AI model to use (default: anthropic/claude-3.5-sonnet)MAX_TOKENS: Maximum tokens for AI responses (default: 1000)TEMPERATURE: AI response creativity (0.0-1.0, default: 0.7)
OpenRouter supports many AI models. Some popular options:
anthropic/claude-3.5-sonnet(default)openai/gpt-4openai/gpt-3.5-turbogoogle/gemini-prometa-llama/llama-2-70b-chat
- Bot not responding: Check if the bot token is correct and the bot is online
- Commands not working: Make sure you've run
deploy-commands.jsand the bot has the "Use Slash Commands" permission - API errors: Verify your OpenRouter API key is correct and you have sufficient credits
- Permission errors: Ensure the bot has the necessary permissions in your Discord server
The bot provides detailed console logs for debugging. Check the console output for any error messages.
Feel free to submit issues and enhancement requests!
MIT License - see LICENSE file for details.