Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

20 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ Discord XP Bot

Python Discord.py License FastAPI

A comprehensive Discord bot for managing XP, levels, and leaderboards with image generation capabilities.

Features โ€ข Installation โ€ข Configuration โ€ข Commands โ€ข API โ€ข Contributing


โœจ Features

  • ๐ŸŽฏ XP System: Track user XP from messages and voice activity
  • ๐Ÿ“Š Level System: Automatic level calculation with custom formulas (exponential or XP-anchors)
  • ๐ŸŽ–๏ธ Role Rewards: Automatic role assignment based on levels
  • ๐Ÿ† Leaderboards: Beautiful image-based leaderboards (permanent & weekly)
  • โš™๏ธ Admin Commands: Full control over XP, levels, and settings
  • ๐ŸŒ Web Service: REST API for external integrations
  • ๐ŸŽค Voice Tracking: Real-time voice activity monitoring
  • ๐Ÿ’ฌ Message Tracking: Smart message XP with cooldowns
  • ๐Ÿ”’ Security: Environment-based configuration with sensitive data protection

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.8 or higher
  • Discord Bot Token (Get one here)
  • Discord Server (Guild) with appropriate permissions

Installation

  1. Clone the repository

    git clone https://github.com/NourEldeenMahmoud/XPBot.git
    cd XPBot
  2. Install dependencies

    pip install -r requirements.txt
  3. Set up environment variables

    cp config/env.example .env
    # Edit .env with your actual values
  4. Set up configuration file

    cp config/config.example.json config.json
    # Edit config.json with your server settings
  5. Configure your bot

    • Go to Discord Developer Portal
    • Create a new application
    • Go to "Bot" section and copy the token
    • Enable required intents (Message Content, Server Members, Voice States)
    • Add the bot to your server with appropriate permissions
  6. Run the bot

    python main.py

    Or:

    python -m src.bot

๐Ÿ”ง Configuration

Environment Variables

Create a .env file with the following variables:

# Required
DISCORD_TOKEN=your_discord_bot_token_here
GUILD_ID=your_guild_id_here

# Optional
ANNOUNCEMENTS_CHANNEL_ID=your_announcements_channel_id_here
MOD_LOG_CHANNEL_ID=your_mod_log_channel_id_here
WEBHOOK_URL=your_webhook_url_here
DATABASE_PATH=xp_bot.db
LOG_LEVEL=INFO

Configuration File

The config.json file contains all bot settings:

  • XP Settings: Message and voice XP ranges, cooldowns
  • Channel Whitelists: Which channels track XP
  • Role Rewards: Automatic role assignment at specific levels
  • Level Formula: Customizable level calculation (exponential or XP-anchors)
  • Exempt Roles: Roles that don't earn XP

See config.example.json for a template.

Bot Permissions

The bot needs the following permissions:

  • โœ… Send Messages
  • โœ… Embed Links
  • โœ… Attach Files
  • โœ… Manage Messages (for delete command)
  • โœ… Manage Roles (for role rewards)
  • โœ… View Channels
  • โœ… Connect (for voice tracking)
  • โœ… Speak (for voice tracking)

Required Intents:

  • Message Content Intent
  • Server Members Intent
  • Voice States Intent

๐Ÿ“‹ Commands

User Commands

Command Description
!rank Show your current rank, XP, and level
!leaderboard Show permanent leaderboard (top 10)
!weeklyleaderboard Show weekly activity leaderboard (top 10)

Admin Commands

Command Description Permission
!setxp @user <amount> Set user's permanent XP Administrator
!setweekly @user <amount> Set user's weekly XP Administrator
!setlevel @user <level> Set user's level Administrator
!resetxp @user Reset all user data Administrator
!resetweekly Reset weekly leaderboard Administrator
!delete <amount> Delete last N messages Manage Messages
!config Show current configuration Administrator

๐ŸŒ Web Service API

The bot includes a FastAPI web service for external integrations:

Base URL

http://localhost:8000

Endpoints

Method Endpoint Description
GET / Health check
GET /leaderboard?limit=10 Get permanent leaderboard
GET /weeklyboard?limit=10 Get weekly leaderboard
GET /user/{user_id} Get user statistics
GET /stats Get overall bot statistics
GET /config Get current bot configuration (read-only)

Example Response

{
  "leaderboard": [
    {
      "rank": 1,
      "user_id": 123456789,
      "permanent_xp": 50000,
      "level": 25
    }
  ],
  "total_entries": 10,
  "guild_id": 987654321
}

๐Ÿ“ Project Structure

XPBot/
โ”œโ”€โ”€ main.py                # Main entry point
โ”œโ”€โ”€ src/                   # Source code
โ”‚   โ”œโ”€โ”€ bot.py             # Main bot entry point
โ”‚   โ”œโ”€โ”€ config_manager.py  # Configuration management
โ”‚   โ”œโ”€โ”€ database.py        # Database operations
โ”‚   โ”œโ”€โ”€ xp_manager.py      # XP and level calculations
โ”‚   โ”œโ”€โ”€ web_service.py     # FastAPI web service
โ”‚   โ””โ”€โ”€ cogs/              # Bot command modules
โ”‚       โ”œโ”€โ”€ xp_commands.py     # XP-related commands
โ”‚       โ”œโ”€โ”€ message_tracker.py # Message XP tracking
โ”‚       โ”œโ”€โ”€ voice_tracker.py   # Voice XP tracking
โ”‚       โ””โ”€โ”€ assistant.py       # Assistant features
โ”œโ”€โ”€ config/                # Configuration files
โ”‚   โ”œโ”€โ”€ config.example.json    # Configuration template
โ”‚   โ””โ”€โ”€ env.example            # Environment variables template
โ”œโ”€โ”€ tests/                 # Test files
โ”‚   โ””โ”€โ”€ test_bot.py        # Bot tests
โ”œโ”€โ”€ requirements.txt       # Python dependencies
โ””โ”€โ”€ README.md             # This file

๐Ÿ”’ Security

Important Security Notes

  1. Never commit sensitive files

    • .env - Contains your bot token
    • config.json - Contains server-specific IDs
    • *.db - Database files
  2. Keep your bot token secret

    • Anyone with the token can control your bot
    • Rotate tokens periodically
    • Use environment variables for all sensitive data
  3. Production Deployment

    • Use environment variables for all configuration
    • Set up proper logging and monitoring
    • Use a production database (PostgreSQL recommended)
    • Enable rate limiting on web service
    • Set up health checks

๐Ÿš€ Deployment

Render (Recommended)

  1. Connect your GitHub repository to Render
  2. Create a new Web Service
  3. Set environment variables in Render dashboard
  4. Set build command: pip install -r requirements.txt
  5. Set start command: python bot.py
  6. Deploy!

Heroku

  1. Create a Heroku app
  2. Set environment variables using heroku config:set
  3. Deploy using Git: git push heroku main

VPS/Dedicated Server

  1. Clone the repository
  2. Set up environment variables
  3. Use systemd or PM2 for process management
  4. Set up reverse proxy (nginx) for web service

Docker (Coming Soon)

Docker support is planned for future releases.

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please read CONTRIBUTING.md for details on our code of conduct.

๐Ÿ“ License

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

๐Ÿ™ Acknowledgments

  • discord.py - Discord API wrapper
  • FastAPI - Modern web framework
  • Discord community for feedback and suggestions

๐Ÿ“ž Support


Made with โค๏ธ for the Discord community

โญ Star this repo if you find it helpful!

About

A comprehensive Discord bot for managing XP, levels, and leaderboards with automatic role rewards. Features message and voice activity tracking, beautiful image-based leaderboards, and a REST API for integrations.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages