Skip to content

Commit

Permalink
Implement type hints and satisfy type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
pranayadmn committed Jul 24, 2024
1 parent 2ea68ab commit 7eedd01
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/Bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
# Load environment variables from .env file
load_dotenv(override=True)

API_ID = int(os.getenv("API_ID"))
API_HASH = os.getenv("API_HASH")
BOT_TOKEN = os.getenv("BOT_TOKEN")
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("app", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
app: Client = Client("app", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)


def main():
def main() -> None:
load_modules(app)
app.run()
13 changes: 8 additions & 5 deletions src/Module.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@
import logging
import os
from importlib import import_module
from typing import Any

from pyrogram import Client

log: logging.Logger = logging.getLogger(__name__)


def load_modules(app):
module_dir = os.path.join(os.path.dirname(__file__), "modules")
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 = filename[:-3]
module_name: str = filename[:-3]
try:
log.info(f"Loading module: {module_name}")
module = import_module(f"src.modules.{module_name}")
module: Any = import_module(f"src.modules.{module_name}")
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 = import_module("src.modules.help")
help_module: Any = import_module("src.modules.help")
help_module.register(app)
except Exception as e:
log.error(f"Error loading help module: {e}")
12 changes: 7 additions & 5 deletions src/modules/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

from typing import Dict

from pyrogram import Client, filters

# This dictionary will store commands and their descriptions
cmds = {}
cmds: Dict[str, str] = {}


def register(app):
def register(app: Client) -> None:
# Register the /help command
@app.on_message(filters.command("help"))
async def help_handler(client, message):
help_text = "Available commands:\n"
async def help_handler(client: Client, message) -> None:
help_text: str = "Available commands:\n"
for command, description in cmds.items():
help_text += f"/{command}: {description}\n"
await message.reply_text(help_text)


def add_cmd(cmd, desc):
def add_cmd(cmd: str, desc: str) -> None:
"""Add command to the help module."""
cmds[cmd] = desc
4 changes: 2 additions & 2 deletions src/modules/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from .help import add_cmd


def register(app: Client):
def register(app: Client) -> None:
# Register the /start command
@app.on_message(filters.command("start"))
async def start(client, message):
async def start(client: Client, message) -> None:
await message.reply_text(
"'Ssup! What would you like me to do?\nUse /help to get a list of all commands and their usage."
)
Expand Down

0 comments on commit 7eedd01

Please sign in to comment.