|
15 | 15 | # Copyright (c) 2024, YeetCode Developers <YeetCode-devs@protonmail.com>
|
16 | 16 |
|
17 | 17 | import asyncio
|
18 |
| -from os import getenv |
| 18 | +from os import getenv, listdir |
| 19 | +from os.path import abspath, dirname, join |
19 | 20 |
|
20 | 21 | from dotenv import load_dotenv
|
| 22 | +from pyrogram import filters |
21 | 23 | from pyrogram.client import Client
|
| 24 | +from pyrogram.handlers import MessageHandler |
22 | 25 |
|
23 |
| -from .Module import load_modules |
| 26 | +from importlib import import_module |
24 | 27 |
|
25 | 28 |
|
26 | 29 | def main() -> None:
|
@@ -48,5 +51,30 @@ def main() -> None:
|
48 | 51 | if isinstance(asyncio.get_event_loop_policy(), asyncio.WindowsSelectorEventLoopPolicy):
|
49 | 52 | asyncio.set_event_loop_policy(default_event_loop_policy)
|
50 | 53 |
|
51 |
| - loaded_modules = load_modules(app) |
| 54 | + commands_dir_name = "commands" |
| 55 | + commands_dir = join(dirname(abspath(__file__)), commands_dir_name) |
| 56 | + |
| 57 | + for file in listdir(commands_dir): |
| 58 | + if file.endswith(".py"): |
| 59 | + command_name = file[:-3] |
| 60 | + command = import_module(f"src.{commands_dir_name}.{command_name}") |
| 61 | + |
| 62 | + if not hasattr(command, "data"): |
| 63 | + print(f"Command {command_name} does not have data attribute.") |
| 64 | + continue |
| 65 | + |
| 66 | + command_data = getattr(command, "data") |
| 67 | + |
| 68 | + print(f"Registering command {command_data['name']}") |
| 69 | + |
| 70 | + # Register the command function |
| 71 | + app.add_handler(MessageHandler(command_data["execute"], filters.command(command_data["name"]))) |
| 72 | + |
| 73 | + # Register aliases if provided |
| 74 | + if "alias" in command_data: |
| 75 | + for alias in command_data["alias"]: |
| 76 | + print(f"Registering alias {alias} for command {command_data['name']}") |
| 77 | + |
| 78 | + app.add_handler(MessageHandler(command_data["execute"], filters.command(alias))) |
| 79 | + |
52 | 80 | app.run()
|
0 commit comments