A Discord bot that uses Google's Gemini 1.5 Flash LLM to craft contextual replies to messages and user input.
- AI-Powered Responses: Uses Google's Gemini 1.5 Flash model for intelligent, contextual replies
- Flexible Usage: Can reply to messages or generate responses from user input
- Smart Context Handling: Analyzes message context to provide relevant responses
- Error Handling: Robust error handling with user-friendly messages
- Comprehensive Logging: Detailed logging for monitoring and debugging
- Rate Limiting: Built-in protection against API abuse
Generate an AI response to a message or your input.
Usage:
- Reply to a message with
/yap- Bot will respond to the referenced message - Use
/yap text: your message- Bot will respond to your input - Combine both - Reply to a message and add additional context
Parameters:
text(optional): Additional text to include in the response generation
- Node.js 18.0.0 or higher
- A Discord application and bot token
- Google Gemini API key
- Clone the repository:
git clone <repository-url>
cd yapai-discord-bot- Install dependencies:
npm install- Create a
.envfile in the root directory:
DISCORD_TOKEN=your_discord_bot_token
DISCORD_CLIENT_ID=your_discord_application_id
GEMINI_API_KEY=your_gemini_api_key- Deploy slash commands to Discord:
npm run deploy-commands- Start the bot:
npm startnpm start- Start the bot in production modenpm run dev- Start the bot in development mode with auto-restartnpm run deploy-commands- Deploy slash commands to Discordnpm test- Run the test suite
src/
├── commands/ # Slash command implementations
│ └── yap.js # Main /yap command
├── services/ # Core business logic
│ ├── llmClient.js # Gemini API integration
│ ├── messageService.js # Message validation and processing
│ └── promptBuilder.js # AI prompt construction
├── utils/ # Utility functions
│ ├── config.js # Configuration management
│ └── logger.js # Logging utilities
├── deploy-commands.js # Command deployment script
└── index.js # Main bot entry point
tests/ # Test files
├── integration/ # Integration tests
├── services/ # Service unit tests
└── utils/ # Utility unit tests
The project includes comprehensive tests using Vitest:
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage| Variable | Description | Required |
|---|---|---|
DISCORD_TOKEN |
Your Discord bot token | Yes |
DISCORD_CLIENT_ID |
Your Discord application ID | Yes |
GEMINI_API_KEY |
Your Google Gemini API key | Yes |
LOG_LEVEL |
Logging level (debug, info, warn, error) | No (default: info) |
The bot requires the following permissions:
- Send Messages
- Use Slash Commands
- Read Message History
- View Channels
- Uses Gemini 1.5 Flash model for response generation
- Implements proper error handling and rate limiting
- Supports contextual prompt building
- Built with Discord.js v14
- Implements modern slash command architecture
- Proper interaction handling and error responses
The bot includes comprehensive error handling for:
- Discord API errors (permissions, deleted messages, etc.)
- Gemini API errors (rate limits, network issues, etc.)
- Invalid user input and edge cases
- Network connectivity issues
Structured logging includes:
- Command usage statistics
- API call tracking (Discord and Gemini)
- Error tracking with context
- Performance metrics
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Run the test suite:
npm test - Commit your changes:
git commit -m 'Add feature' - Push to the branch:
git push origin feature-name - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the logs for error details
- Ensure all environment variables are properly set
- Verify Discord and Gemini API credentials
- Check the Issues page for known problems
- Built with Discord.js
- Powered by Google Gemini AI
- Testing with Vitest