Skip to content

Commit

Permalink
Fully utilise asynchronous methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pranayadmn committed Jul 24, 2024
1 parent 7eedd01 commit 672ffbf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 37 deletions.
30 changes: 0 additions & 30 deletions src/Bot.py

This file was deleted.

6 changes: 3 additions & 3 deletions src/Module.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
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":
module_name: str = filename[:-3]
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}")

# Load the help module after other modules
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}")
30 changes: 28 additions & 2 deletions src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion src/modules/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/modules/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 672ffbf

Please sign in to comment.