Skip to content

Commit

Permalink
src: Bot: Switch to a command-handler implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Pratham Dubey <134331217+prathamdby@users.noreply.github.com>
  • Loading branch information
prathamdby committed Apr 14, 2024
1 parent cac175e commit dcc8935
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
34 changes: 31 additions & 3 deletions src/Bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
# Copyright (c) 2024, YeetCode Developers <YeetCode-devs@protonmail.com>

import asyncio
from os import getenv
from os import getenv, listdir
from os.path import abspath, dirname, join

from dotenv import load_dotenv
from pyrogram import filters
from pyrogram.client import Client
from pyrogram.handlers import MessageHandler

from .Module import load_modules
from importlib import import_module


def main() -> None:
Expand Down Expand Up @@ -48,5 +51,30 @@ def main() -> None:
if isinstance(asyncio.get_event_loop_policy(), asyncio.WindowsSelectorEventLoopPolicy):
asyncio.set_event_loop_policy(default_event_loop_policy)

loaded_modules = load_modules(app)
commands_dir_name = "commands"
commands_dir = join(dirname(abspath(__file__)), commands_dir_name)

for file in listdir(commands_dir):
if file.endswith(".py"):
command_name = file[:-3]
command = import_module(f"src.{commands_dir_name}.{command_name}")

if not hasattr(command, "data"):
print(f"Command {command_name} does not have data attribute.")
continue

command_data = getattr(command, "data")

print(f"Registering command {command_data['name']}")

# Register the command function
app.add_handler(MessageHandler(command_data["execute"], filters.command(command_data["name"])))

# Register aliases if provided
if "alias" in command_data:
for alias in command_data["alias"]:
print(f"Registering alias {alias} for command {command_data['name']}")

app.add_handler(MessageHandler(command_data["execute"], filters.command(alias)))

app.run()
File renamed without changes.
25 changes: 12 additions & 13 deletions src/modules/start.py → src/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,20 @@
#
# Copyright (c) 2024, YeetCode Developers <YeetCode-devs@protonmail.com>

from pyrogram import filters
from pyrogram.client import Client
from pyrogram.handlers import MessageHandler
from pyrogram.types import Message
from pyrogram.client import Client

from src.Module import ModuleBase


class Module(ModuleBase):
def on_load(self, app: Client):
app.add_handler(MessageHandler(start, filters.command("start")))

def on_shutdown(self, app: Client):
pass
async def execute(app: Client, message: Message) -> None:
await message.reply("Hello.")


async def start(app: Client, message: Message):
await message.reply("Hello!")
data = {
"name": "start",
"description": "Starts the bot.",
# "alias": ["on"], # Optional
"usage": "/start",
"example": "/start",
"category": "Core",
"execute": execute,
}

0 comments on commit dcc8935

Please sign in to comment.