Twitch Bot Documentation
Complete feature list, command reference, and architecture overview.
1. Overview
A custom-built Twitch bot utilizing Python's twitchio library. It features a persistent SQLite database, interactive setup and message configuration scripts, automated OBS WebSocket controls, and local audio playback via Pygame.
2. Automated Features
XP & Leveling System
Awards XP to viewers at customized intervals defined in the bot's configuration. Users level up dynamically based on a linear XP scaling formula. Automated chat alerts trigger when active users or lurkers level up.
Stream Status Tracking
Checks if the stream is live every minute. If the stream remains offline for 2 consecutive checks, the bot automatically sends a goodbye message, creates a database backup, and initiates a safe shutdown.
Raid Handling
Detects incoming raids, drops a randomized welcome message containing the game the raider was playing, and grants the raider a 1-hour "Double XP" buff. Additionally, it triggers a "Raid Party Bonus," awarding 100 flat XP to everyone currently in chat.
Viewer Session Tracking
Silently monitors chatters every minute, tracking when they first joined and their total watch time for the current session. This data is automatically exported to a secure text file for analytics and moderation.
Utility Scripts
Includes a dedicated setup script for securely fetching and saving Twitch credentials to the environment file. It also features a Message Editor utility to easily update, add, or remove custom chat announcements without touching the raw JSON data. All scripts can be compiled into executables simultaneously using the provided batch file.
3. EventSub Integrations
OBS Hype Train System
- Begin: Connects to OBS via WebSocket, unhides a designated scene source to play the video on stream, and awards the starter an instant level up.
- Progress: Awards an instant level-up to any user who contributes to the train.
- End: Hides the OBS video source and awards every active chatter a flat coin bonus calculated by the train's final level.
Tiered Slot Machine (Channel Points)
Listens for specific channel point redemptions named exactly "Play 10", "Play 20", or "Play 50". Features dynamic emoji reels (🍒, 🍋, 💎, etc.) and tiered payouts (Small, Medium, Big) based on the bet amount:
- Play 10: 35% chance (x2), 20% chance (x3), 10% chance (x5).
- Play 20: 25% chance (x2), 15% chance (x4), 10% chance (x10).
- Play 50 (High Roller): 15% chance (x2), 10% chance (x5), 5% base chance (x20). Includes a Progressive Jackpot mechanic stored in the database where every loss increases the Big Win odds by 1% until hit!
4. Commands
Toggles offline checks on and off. When enabled, the bot will not automatically shut down if the stream goes offline, allowing for safe testing.
Triggers the OBS WebSocket connection to display the Hype Train video for exactly 10 seconds before hiding it again. Used for testing the live stream view.
Simulates the visual and audio OBS triggers for an incoming raid for testing purposes.
Flags a user in the database so they no longer passively earn XP during the payout intervals, and wipes their current XP and coins to zero.
A bulk maintenance command that resets the XP and coins to zero for every single user currently flagged as banned in the database.
Removes the XP ban flag from a user.
Completely purges a user's record (XP, Level, Coins) from the SQLite database.
Instantly deposits a specific amount of virtual coins into a user's account.
Deducts a specific amount of virtual coins from a user's account.
Gives a shout out to a specified user, automatically fetching their last played game and linking their channel in chat.
Manually resets the High Roller progressive jackpot odds back to the default 5% and updates the database.
Checks and displays the current win percentage for the High Roller jackpot in chat.
Displays the user's current level and total accumulated XP.
Displays the user's current virtual coin balance.
Displays the top leaderboard of viewers based on highest Level and XP.
Saves a custom quote to the vault (text file), automatically appending the current date, stream channel, and the username of the person who captured it.
Retrieves and displays a random quote from the vault.
Provides a direct link to the interactive web shop where viewers can browse and copy commands for video and audio rewards.
Deducts coins from the user's balance and plays an item. If the item is an audio file, it plays via Pygame. If it is a video file, it toggles the designated source in OBS. Video redemptions share a strict 60-second global cooldown to prevent visual spam. Note: The Broadcaster and specific super-users bypass all coin costs and cooldowns.
Uses OBS WebSockets to trigger a specific hotkey sequence (Ctrl + Alt + T), interacting with the on-screen stream timer script.
5. File Structure & Dependencies
- / (Root Directory)
- bot.exe: The compiled main executable logic.
- setup.exe: Interactive wizard for configuring tokens and initial environment parameters.
- message_editor.exe: Command-line interface for safely editing custom bot chat responses.
- BUILD_ALL.bat: Batch script used to compile all Python scripts into standalone executables.
- botShop.html: An interactive frontend webpage that dynamically builds a clickable shop interface by reading
shop.json.
- /Config/
- .env: Secure storage for tokens, OBS variables, and interval parameters.
- messages.json: Contains customizable text variations for startups, level-ups, and raid announcements.
- shop.json: Configures shop items, short IDs, display names, and OBS routing for videos.
- shoutouts.json: Maps specific usernames to custom welcome messages.
- /Data/
- Bot_XP_data.db: The SQLite database file handling the economy and levels.
- quotes.txt: Flat text file storing user-generated quotes with timestamps.
- session_viewers.txt: A secure log tracking when viewers join and their total watch time for the current session.
- /backups/: Subfolder where automatic database backups are saved upon safe bot shutdown.
- /Logs/
- bot_log.txt: Actively updated log file tracking bot events, errors, and system status.
- /Bot_Shop/
- Directory containing the `.mp3` or `.wav` audio files requested by the bot's audio shop commands via Pygame.