Pyrogram: Command modulation like discord.py

Posted by Dull_Caregiver_6883@reddit | Python | View on Reddit | 2 comments

Hi everyone!

I’ve been working with discord.py for a while, and one of the features I really love is the dynamic command loading system (Cogs). It keeps the codebase clean and scalable by organizing commands into separate files/modules.

Now, I’ve started working with Pyrogram, and I find having all the bot’s logic in a single file quite messy. I’m looking for a way to dynamically load commands in main.py from separate files within a commands folder, similar to how Cogs work in Discord.py.

Here’s my current project structure:

project/ │ ├── .venv/ ├── secrets/ │ ├── .env │ └── config.py ├── commands/ │ ├── __init__.py │ ├── help.py │ ├── start.py │ ├── example.py ├── main.py

What I’m Looking For:

  1. A way to dynamically discover and load all commands from the commands folder into main.py.

  2. Ideally, commands should be added to the bot without modifying main.py directly.

If anyone has experience with this or can point me toward resources/examples, I’d appreciate it!