Skip to content

Commit dcc8935

Browse files
committed
src: Bot: Switch to a command-handler implementation
Signed-off-by: Pratham Dubey <134331217+prathamdby@users.noreply.github.com>
1 parent cac175e commit dcc8935

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

src/Bot.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
# Copyright (c) 2024, YeetCode Developers <YeetCode-devs@protonmail.com>
1616

1717
import asyncio
18-
from os import getenv
18+
from os import getenv, listdir
19+
from os.path import abspath, dirname, join
1920

2021
from dotenv import load_dotenv
22+
from pyrogram import filters
2123
from pyrogram.client import Client
24+
from pyrogram.handlers import MessageHandler
2225

23-
from .Module import load_modules
26+
from importlib import import_module
2427

2528

2629
def main() -> None:
@@ -48,5 +51,30 @@ def main() -> None:
4851
if isinstance(asyncio.get_event_loop_policy(), asyncio.WindowsSelectorEventLoopPolicy):
4952
asyncio.set_event_loop_policy(default_event_loop_policy)
5053

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+
5280
app.run()
File renamed without changes.

src/modules/start.py renamed to src/commands/start.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@
1414
#
1515
# Copyright (c) 2024, YeetCode Developers <YeetCode-devs@protonmail.com>
1616

17-
from pyrogram import filters
18-
from pyrogram.client import Client
19-
from pyrogram.handlers import MessageHandler
2017
from pyrogram.types import Message
18+
from pyrogram.client import Client
2119

22-
from src.Module import ModuleBase
23-
24-
25-
class Module(ModuleBase):
26-
def on_load(self, app: Client):
27-
app.add_handler(MessageHandler(start, filters.command("start")))
2820

29-
def on_shutdown(self, app: Client):
30-
pass
21+
async def execute(app: Client, message: Message) -> None:
22+
await message.reply("Hello.")
3123

3224

33-
async def start(app: Client, message: Message):
34-
await message.reply("Hello!")
25+
data = {
26+
"name": "start",
27+
"description": "Starts the bot.",
28+
# "alias": ["on"], # Optional
29+
"usage": "/start",
30+
"example": "/start",
31+
"category": "Core",
32+
"execute": execute,
33+
}

0 commit comments

Comments
 (0)