From 672ffbfbdd2d4f0e329b60d5c5cebe5dbe136f42 Mon Sep 17 00:00:00 2001 From: Pranaya Deomani Date: Wed, 24 Jul 2024 22:37:34 +0530 Subject: [PATCH] Fully utilise asynchronous methods --- src/Bot.py | 30 ------------------------------ src/Module.py | 6 +++--- src/__main__.py | 30 ++++++++++++++++++++++++++++-- src/modules/help.py | 2 +- src/modules/start.py | 2 +- 5 files changed, 33 insertions(+), 37 deletions(-) delete mode 100644 src/Bot.py diff --git a/src/Bot.py b/src/Bot.py deleted file mode 100644 index 77cbbe9..0000000 --- a/src/Bot.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright 2024 StatiXOS -# -# Use of this source code is governed by an MIT-style -# license that can be found in the LICENSE file or at -# https://opensource.org/licenses/MIT. - -import os - -from dotenv import load_dotenv -from pyrogram import Client - -from .Module import load_modules - -# Load environment variables from .env file -load_dotenv(override=True) - -API_ID: int = int(os.getenv("API_ID", "0")) -API_HASH: str = os.getenv("API_HASH", "") -BOT_TOKEN: str = os.getenv("BOT_TOKEN", "") - -if not all([API_ID, API_HASH, BOT_TOKEN]): - raise ValueError("API_ID, API_HASH, and BOT_TOKEN must be set in the .env file.") - -# Initialize the Client -app: Client = Client("app", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN) - - -def main() -> None: - load_modules(app) - app.run() diff --git a/src/Module.py b/src/Module.py index efbaff7..f47e223 100644 --- a/src/Module.py +++ b/src/Module.py @@ -14,7 +14,7 @@ log: logging.Logger = logging.getLogger(__name__) -def load_modules(app: Client) -> None: +async def load_modules(app: Client) -> None: module_dir: str = os.path.join(os.path.dirname(__file__), "modules") for filename in os.listdir(module_dir): if filename.endswith(".py") and filename != "help.py": @@ -22,7 +22,7 @@ def load_modules(app: Client) -> None: try: log.info(f"Loading module: {module_name}") module: Any = import_module(f"src.modules.{module_name}") - module.register(app) + await module.register(app) except Exception as e: log.error(f"Error loading module {module_name}: {e}") @@ -30,6 +30,6 @@ def load_modules(app: Client) -> None: try: log.info("Loading help module") help_module: Any = import_module("src.modules.help") - help_module.register(app) + await help_module.register(app) except Exception as e: log.error(f"Error loading help module: {e}") diff --git a/src/__main__.py b/src/__main__.py index cc72e39..bcdd72a 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -4,8 +4,34 @@ # license that can be found in the LICENSE file or at # https://opensource.org/licenses/MIT. +import os + +from dotenv import load_dotenv +from pyrogram import Client, idle + from . import Logging -from .Bot import main +from .Module import load_modules + +# Load environment variables from .env file +load_dotenv(override=True) + +API_ID: int = int(os.getenv("API_ID", "0")) +API_HASH: str = os.getenv("API_HASH", "") +BOT_TOKEN: str = os.getenv("BOT_TOKEN", "") + +if not all([API_ID, API_HASH, BOT_TOKEN]): + raise ValueError("API_ID, API_HASH, and BOT_TOKEN must be set in the .env file.") + +# Initialize the Client +app: Client = Client("app", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN) + + +async def main() -> None: + await load_modules(app) + await app.start() + await idle() + await app.stop() + if __name__ == "__main__": - main() + app.run(main()) diff --git a/src/modules/help.py b/src/modules/help.py index 57430d6..9262bc9 100644 --- a/src/modules/help.py +++ b/src/modules/help.py @@ -12,7 +12,7 @@ cmds: Dict[str, str] = {} -def register(app: Client) -> None: +async def register(app: Client) -> None: # Register the /help command @app.on_message(filters.command("help")) async def help_handler(client: Client, message) -> None: diff --git a/src/modules/start.py b/src/modules/start.py index 752322e..371696f 100644 --- a/src/modules/start.py +++ b/src/modules/start.py @@ -9,7 +9,7 @@ from .help import add_cmd -def register(app: Client) -> None: +async def register(app: Client) -> None: # Register the /start command @app.on_message(filters.command("start")) async def start(client: Client, message) -> None: