Integrated the scheduler

This commit is contained in:
Profitroll 2023-06-26 12:45:39 +02:00
parent 7f054a6d93
commit 9af6d5cb7c
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
2 changed files with 15 additions and 6 deletions

View File

@ -7,6 +7,8 @@ from typing import List, Union
try: try:
import pyrogram import pyrogram
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.schedulers.background import BackgroundScheduler
from pyrogram.client import Client from pyrogram.client import Client
from pyrogram.errors import BadRequest from pyrogram.errors import BadRequest
from pyrogram.handlers.message_handler import MessageHandler from pyrogram.handlers.message_handler import MessageHandler
@ -40,7 +42,9 @@ logger = logging.getLogger(__name__)
class PyroClient(Client): 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: with open("config.json", "r", encoding="utf-8") as f:
self.config: dict = loads(f.read()) self.config: dict = loads(f.read())
super().__init__( super().__init__(
@ -70,7 +74,9 @@ class PyroClient(Client):
self.in_all_locales = self.bot_locale.in_all_locales self.in_all_locales = self.bot_locale.in_all_locales
self.in_every_locale = self.bot_locale.in_every_locale 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() await super().start()
self.start_time = time() self.start_time = time()
@ -89,14 +95,17 @@ class PyroClient(Client):
text=f"Bot started PID `{getpid()}`", text=f"Bot started PID `{getpid()}`",
) )
scheduler.add_job( if self.scheduler is None:
return
self.scheduler.add_job(
self.register_commands, self.register_commands,
trigger="date", trigger="date",
run_date=datetime.now() + timedelta(seconds=5), run_date=datetime.now() + timedelta(seconds=5),
kwargs={"command_sets": await self.collect_commands()}, kwargs={"command_sets": await self.collect_commands()},
) )
scheduler.start() self.scheduler.start()
except BadRequest: except BadRequest:
logger.warning("Unable to send message to report chat.") logger.warning("Unable to send message to report chat.")

View File

@ -25,8 +25,8 @@ classifiers = [
dependencies = ["aiofiles~=23.1.0"] dependencies = ["aiofiles~=23.1.0"]
[project.optional-dependencies] [project.optional-dependencies]
pycord = ["py-cord>=2.0.0"] pycord = ["py-cord~=2.4.1"]
pyrogram = ["pyrogram>=2.0.0"] pyrogram = ["pyrogram~=2.0.106", "apscheduler~=3.10.1"]
speed = ["ujson~=5.8.0"] speed = ["ujson~=5.8.0"]
[project.urls] [project.urls]