From 9af6d5cb7c74a5c23bd5fc3ad81fd7a996392084 Mon Sep 17 00:00:00 2001 From: profitroll Date: Mon, 26 Jun 2023 12:45:39 +0200 Subject: [PATCH] Integrated the scheduler --- libbot/pyrogram/classes/client.py | 17 +++++++++++++---- pyproject.toml | 4 ++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libbot/pyrogram/classes/client.py b/libbot/pyrogram/classes/client.py index 2b29842..a122ad1 100644 --- a/libbot/pyrogram/classes/client.py +++ b/libbot/pyrogram/classes/client.py @@ -7,6 +7,8 @@ from typing import List, Union try: import pyrogram + from apscheduler.schedulers.asyncio import AsyncIOScheduler + from apscheduler.schedulers.background import BackgroundScheduler from pyrogram.client import Client from pyrogram.errors import BadRequest from pyrogram.handlers.message_handler import MessageHandler @@ -40,7 +42,9 @@ logger = logging.getLogger(__name__) class PyroClient(Client): - def __init__(self): + def __init__( + self, scheduler: Union[AsyncIOScheduler, BackgroundScheduler, None] = None + ): with open("config.json", "r", encoding="utf-8") as f: self.config: dict = loads(f.read()) super().__init__( @@ -70,7 +74,9 @@ class PyroClient(Client): self.in_all_locales = self.bot_locale.in_all_locales self.in_every_locale = self.bot_locale.in_every_locale - async def start(self, scheduler): + self.scheduler: Union[AsyncIOScheduler, BackgroundScheduler, None] = scheduler + + async def start(self): await super().start() self.start_time = time() @@ -89,14 +95,17 @@ class PyroClient(Client): text=f"Bot started PID `{getpid()}`", ) - scheduler.add_job( + if self.scheduler is None: + return + + self.scheduler.add_job( self.register_commands, trigger="date", run_date=datetime.now() + timedelta(seconds=5), kwargs={"command_sets": await self.collect_commands()}, ) - scheduler.start() + self.scheduler.start() except BadRequest: logger.warning("Unable to send message to report chat.") diff --git a/pyproject.toml b/pyproject.toml index 0ee6d20..8aca659 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,8 +25,8 @@ classifiers = [ dependencies = ["aiofiles~=23.1.0"] [project.optional-dependencies] -pycord = ["py-cord>=2.0.0"] -pyrogram = ["pyrogram>=2.0.0"] +pycord = ["py-cord~=2.4.1"] +pyrogram = ["pyrogram~=2.0.106", "apscheduler~=3.10.1"] speed = ["ujson~=5.8.0"] [project.urls]