Skip to content

A Discord bot powered by Google's Gemini 1.5 Flash LLM that generates intelligent, contextual responses to messages and user input through the /yap slash command.

Notifications You must be signed in to change notification settings

Chandu4143/YapAi

Repository files navigation

YapAI Discord Bot

A Discord bot that uses Google's Gemini 1.5 Flash LLM to craft contextual replies to messages and user input.

Features

  • 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

Commands

/yap

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

Setup

Prerequisites

  • Node.js 18.0.0 or higher
  • A Discord application and bot token
  • Google Gemini API key

Installation

  1. Clone the repository:
git clone <repository-url>
cd yapai-discord-bot
  1. Install dependencies:
npm install
  1. Create a .env file in the root directory:
DISCORD_TOKEN=your_discord_bot_token
DISCORD_CLIENT_ID=your_discord_application_id
GEMINI_API_KEY=your_gemini_api_key
  1. Deploy slash commands to Discord:
npm run deploy-commands
  1. Start the bot:
npm start

Development

Scripts

  • npm start - Start the bot in production mode
  • npm run dev - Start the bot in development mode with auto-restart
  • npm run deploy-commands - Deploy slash commands to Discord
  • npm test - Run the test suite

Project Structure

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

Testing

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

Configuration

Environment Variables

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)

Discord Permissions

The bot requires the following permissions:

  • Send Messages
  • Use Slash Commands
  • Read Message History
  • View Channels

API Integration

Google Gemini

  • Uses Gemini 1.5 Flash model for response generation
  • Implements proper error handling and rate limiting
  • Supports contextual prompt building

Discord.js

  • Built with Discord.js v14
  • Implements modern slash command architecture
  • Proper interaction handling and error responses

Error Handling

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

Logging

Structured logging includes:

  • Command usage statistics
  • API call tracking (Discord and Gemini)
  • Error tracking with context
  • Performance metrics

Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make your changes and add tests
  4. Run the test suite: npm test
  5. Commit your changes: git commit -m 'Add feature'
  6. Push to the branch: git push origin feature-name
  7. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you encounter any issues or have questions:

  1. Check the logs for error details
  2. Ensure all environment variables are properly set
  3. Verify Discord and Gemini API credentials
  4. Check the Issues page for known problems

Acknowledgments

About

A Discord bot powered by Google's Gemini 1.5 Flash LLM that generates intelligent, contextual responses to messages and user input through the /yap slash command.

Topics

Resources

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors