Skip to content

🖥️Bot structure

The bot is based on the pyrogram framework, the documentation for which you can read PyroTGFork.

The most important component of the bot is the ModuleLoader class (base.loader.ModuleLoader), which loads and manages modules, interacts with the ModuleManager for installing, updating, and uninstalling them, and distributes events between modules. At startup, the ModuleLoader scans the modules/directory in the root of the source code and attempts to load modules found there, respecting their auto_load setting (defined in config.yaml or info.yaml).

Module loading takes place in several stages within the load_module method:

  1. Preparation: Changes to the module directory, checks/installs dependencies (requirements.txt) via ModuleManager, reads module metadata.

  2. Instantiation: Imports the module code and creates an instance of the BaseModule class.

  3. Checks & Setup: Verifies Python version compatibility (optional), checks permissions (e.g., database requirements against bot config), sets up database connection (set_db) if requested and enabled, and provides the loader instance (loader) if requested.

  4. Extension Application: Iterates through loaded BaseExtension instances and calls their on_module method, allowing extensions to modify the module instance before final initialization.

  5. Stage 2 Initialization: Calls the module's stage2 method, which registers Pyrogram handlers (commands, callbacks, messages) and loads any ModuleExtensions defined within the module.

  6. Final Steps: Calls the module's on_init method for custom initialization logic.

INFO

  • BaseExtension (base.base_ext.BaseExtension): These are loaded once at bot startup from the extensions/ directory. They can influence the loading process of all modules via the on_module hook, running between Stage 1 (Instantiation/Setup) and Stage 2 (Handler Registration). Useful for adding cross-cutting features.
  • ModuleExtension (base.mod_ext.ModuleExtension): These are defined within a specific module and loaded during that module's stage2 initialization. They allow splitting a module's own code into multiple files while sharing access to the main module's attributes (like bot, S, db).

The ModuleLoader also works closely with the ModuleManager (base.mod_manager.ModuleManager) which handles the lifecycle operations: installing from Git, checking for/applying updates (including backups, rollbacks, and DB migrations), managing dependencies, and uninstalling modules.