Skip to content

This bot is designed to scrape online leaderboards and provide real-time updates by database on rally standings from https://rallysimfans.hu/.

License

Notifications You must be signed in to change notification settings

Snaze878/RBR_Bot_MySQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

26 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌟 RBR Discord Bot (MySQL Edition)

Welcome to the RBR Discord Bot (MySQL Edition)!
This bot scrapes online leaderboards from rallysimfans.hu and posts real-time updates to Discord, enhanced with MySQL database integration. Designed for rally communities who want historical tracking, leader change announcements, and interactive search features.


🏎️ Features

  • Automated Leaderboard Updates
    Continuously scrapes and posts the latest standings to your Discord server.

  • Live Leader Change Detection
    Announces when a new driver takes the lead in any rally leg or general standings.

  • MySQL 8.0 Database Backend
    All data is logged asynchronously to a MySQL database using aiomysql.

  • Interactive Dropdowns
    Driver and week selection via Discord UI elements.

  • Google Sheets Integration
    Dynamically syncs new rally weeks and configuration from a form submission.

  • Admin-Only Control
    Sync from Google Sheets, force restarts, recalculate points, and database checks.

  • Driver Stats, Trends & History
    Track individual performance, podiums, weekly progression, and vehicle usage.

  • Stage Completion Progress
    Monitor stage-by-stage progress with βœ…/❌ breakdowns.

  • Most Wins Tracker
    Displays top drivers with the most stage wins.

  • Custom Points System
    Built-in seasonal points system that calculates driver standings each week. Points are recalculated automatically when a new active week begins.

  • Archived Data Support
    Past results are cached and accessible via commands.

  • Two Leaderboard Tables
    Logs both LEFT and RIGHT table data from the rallysimfans website.

  • Stability Watchdog
    Auto-alerts if the bot enters a reconnect loop.


πŸ’¬ Commands

!search [driver] [s#w#]       β†’ Search for a driver's results (dropdown if blank)
!stats [driver]               β†’ View driver's stats: total events, avg pos, wins, podiums, vehicle, points
!history [driver]             β†’ View week-by-week positions & gaps on general leaderboard
!trend [driver]               β†’ See performance trend: arrows, medals, time gaps per week
!progress [driver]            β†’ Show stage-by-stage completion for current week (dropdown if blank)
!mostwins                     β†’ List top 10 drivers with the most stage wins
!leaderboard [s#w#]           β†’ Show general leaderboard
!leg1 to !leg6 [s#w#]         β†’ Display top 5 per stage in a rally leg
!compare driver1 vs driver2   β†’ Head-to-head comparison
!points                       β†’ Show full season points from DB
!info                         β†’ Rally name, password, and info URL
!sync                         β†’ Pull new config & data from Google Sheets
!recalpoints                  β†’ Recalculate points for all previous weeks
!dbcheck                      β†’ Check DB connection and row counts
!restart                      β†’ Restart the bot
!cmd                          β†’ List available commands
!skillissue                   β†’ Shows who finished last this week with a motivational quote

🧠 You can also run !stats, !history, !search, !trend, or !progress without a name to use a dropdown menu.


πŸ“Έ Example Bot Output

πŸ“Έ Click to view example bot output

General Leaderboard Points Standings Search Leg Stats


βš™οΈ How It Works

  • Scraping: Uses requests, selenium, and BeautifulSoup to gather leaderboard data.
  • Storage: Logs results to MySQL (leaderboard_log, leaderboard_log_left, general_leaderboard_log, season_points, assigned_points_weeks, previous_leaders).
  • Discord Integration: discord.py with rich embeds and dropdown menus.
  • Dynamic Week/Season Handling: URLs and settings pulled from .env and synced via Google Sheets.
  • Data Retry: All DB operations retry if MySQL is temporarily down.
  • Leader Change Loop: Bot constantly monitors for leader changes in all active rally stages.
  • Reconnect Watchdog: Alerts bot owner if it gets stuck in a loop.

πŸ—“οΈ Installation Guide

1. Install Python

Download it from python.org.
Make sure to check "Add Python to PATH" during installation.


2. Install Required Packages

pip install -r requirements.txt

3. Create a Discord Bot

  1. Visit Discord Developer Portal.
  2. Click "New Application".
  3. Navigate to Bot, click "Add Bot", and enable Message Content Intent.

4. Set Permissions & Invite Bot

  • Permissions Needed:
    • Send Messages
    • Embed Links
    • Read Message History
    • Use External Emojis

Use the OAuth2 URL Generator to get an invite link and add the bot to your server.


5. Configure .env

DISCORD_BOT_TOKEN=your_token
DISCORD_CHANNEL_ID=your_channel_id
BOT_OWNER_ID=your_discord_user_id
ALLOWED_SYNC_USERS=comma_separated_user_ids
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=rbr_user
MYSQL_PASSWORD=your_password
MYSQL_DATABASE=rbr_leaderboards
GOOGLE_SHEET_NAME=RBR_Tracking
LEAGUE_NAME=Your League Name
INFO_URL=https://example.com/info
RALLY_NAME=My Rally
RALLY_PASSWORD=secret
S1W1_LEADERBOARD=https://example.com
S1W1_LEG_1_1=https://example.com
...

πŸ” Never share your bot token publicly!


6. Google Sheets Integration

Allows you to dynamically manage rally config + update .env with a Google Form.

a. Create Google Sheet

  1. Create a new sheet called RBR_Tracking.
  2. Sheet1 should collect rally details:
    • Season Number
    • Week Number
    • Leaderboard URL
    • New Rally Name (optional)
    • LEG 1 STAGE 1 URL, LEG 1 STAGE 2 URL, ...

b. Set Up Service Account

  1. Visit Google Cloud Console.
  2. Create a project and enable Google Sheets API & Google Drive API.
  3. Create a Service Account, download its JSON credentials.
  4. Rename the file to google_creds.json and place it in your bot directory.
  5. Share your Google Sheet with the service account email.

c. Run the Sync

!sync

This will update .env and load rally configuration from the sheet.


7. MySQL Database Setup

CREATE DATABASE rbr_leaderboards CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE rbr_leaderboards;

CREATE TABLE leaderboard_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    track_name VARCHAR(255),
    position INT,
    driver_name VARCHAR(255),
    vehicle VARCHAR(255),
    time VARCHAR(50),
    diff_prev VARCHAR(50),
    diff_first VARCHAR(50),
    season INT,
    week INT,
    scraped_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE leaderboard_log_left (
    id INT AUTO_INCREMENT PRIMARY KEY,
    track_name VARCHAR(255),
    position INT,
    driver_name VARCHAR(255),
    vehicle VARCHAR(255),
    time VARCHAR(50),
    diff_prev VARCHAR(50),
    diff_first VARCHAR(50),
    season INT,
    week INT,
    scraped_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE general_leaderboard_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    driver_name VARCHAR(255),
    position INT,
    vehicle VARCHAR(255),
    time VARCHAR(50),
    diff_prev VARCHAR(50),
    diff_first VARCHAR(50),
    season INT,
    week INT,
    scraped_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE previous_leaders (
    track_name VARCHAR(255) PRIMARY KEY,
    leader_name VARCHAR(255),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE season_points (
    id INT AUTO_INCREMENT PRIMARY KEY,
    driver_name VARCHAR(255),
    season INT,
    points INT
);

CREATE TABLE assigned_points_weeks (
    season INT,
    week INT,
    PRIMARY KEY (season, week)
);

8. Enable Developer Mode in Discord

  • Go to User Settings > Advanced > Developer Mode.
  • Right-click a channel β†’ Copy Channel ID β†’ paste it into .env.

9. Run the Bot

python RBR_Bot.py

The bot will initialize past week scraping, post to Discord, and begin watching for updates.


πŸ“ƒ File Structure

πŸ” your_repo/
πŸ”Ή RBR_Bot.py           # Main bot logic
πŸ”Ή requirements.txt     # Dependencies
πŸ”Ή .env                 # Config from Discord/MySQL/Google
πŸ”Ή google_creds.json    # Service account credentials
πŸ”Ή logs/                # Daily logs (commands, scraping, errors)

🀝 Contribute & Get Support

Want to contribute or report a bug? Pull requests and issue reports are welcome!
Join the support community here:

🌐 Discord Support Server


πŸ“œ License

This project is open-source and licensed under the GNU General Public License v3.
Feel free to modify and distribute it under the terms of the license.

About

This bot is designed to scrape online leaderboards and provide real-time updates by database on rally standings from https://rallysimfans.hu/.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published