Entry Point (main.py)¶
Overview¶
main.py is the execution entry point for the PigPig Discord Bot. It handles environment setup, bot instantiation, and the initial connection to Discord.
Startup Workflow¶
- Environment Loading: Uses
python-dotenvto load secrets and configuration from.env. - Intent Configuration: Sets up Discord Intents (Message Content, Members, Presences) based on the
base_config. - Bot Instantiation: Creates the
PigPigbot object with customCommandTreefor guild-only command validation. - Version Check: Spawns a background thread to check for project updates without blocking the bot's connection.
- Execution: Calls
bot.run()using the token fromaddons.tokens.
Custom Components¶
CommandCheck¶
A custom app_commands.CommandTree that overrides interaction_check to ensure that all slash commands are executed within a server (guild) context, preventing errors in Direct Messages.
Background Services¶
Version Checker Thread¶
To ensure the bot is always up to date, a dedicated daemon thread runs VersionChecker at startup. It logs warnings if a newer version is available on GitHub and provides the update command.
Error Handling & Shutdown¶
The entry point includes a try...except...finally block to handle:
- KeyboardInterrupt: Catches Ctrl+C for manual stops.
- Graceful Shutdown: Ensures bot.close() is called to clean up resources, close database connections, and cancel pending asyncio tasks.
For daily operation, simply run python main.py. For update management, refer to the Update Guide.