From f22b81cf1af29d4441faed1c3f4221c967d574e5 Mon Sep 17 00:00:00 2001 From: penguinboi Date: Sat, 26 Apr 2025 17:38:11 -0400 Subject: [PATCH] Added scheduler --- grace/bot.py | 16 ++++++++++------ pyproject.toml | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/grace/bot.py b/grace/bot.py index 45cb1de..a7adb46 100644 --- a/grace/bot.py +++ b/grace/bot.py @@ -1,4 +1,5 @@ from logging import info, warning, critical +from apscheduler.schedulers.asyncio import AsyncIOScheduler from discord import Intents, LoginFailure from discord.ext.commands import Bot as DiscordBot, when_mentioned_or from discord.ext.commands.errors import ( @@ -6,7 +7,7 @@ ExtensionAlreadyLoaded ) from grace.application import Application, SectionProxy -from grace.watcher import Watcher, Observer +from grace.watcher import Watcher # make discord.ext.commands importable from this module from discord.ext.commands import * @@ -22,9 +23,10 @@ class Bot(DiscordBot): The bot is instantiated with the application object and the intents. """ - def __init__(self, app: Application, **kwargs): + def __init__(self, app: Application, **kwargs) -> None: self.app: Application = app self.config: SectionProxy = self.app.client + self.scheduler: AsyncIOScheduler = AsyncIOScheduler() self.watcher: Watcher = Watcher(self.on_reload) command_prefix = kwargs.pop( @@ -47,12 +49,12 @@ def __init__(self, app: Application, **kwargs): **kwargs ) - async def _load_extensions(self) -> None: + async def load_extensions(self) -> None: for module in self.app.extension_modules: info(f"Loading module '{module}'") await self.load_extension(module) - async def _sync_commands(self) -> None: + async def sync_commands(self) -> None: warning("Syncing application commands. This may take some time.") if guild_id := self.config.get("guild"): @@ -66,14 +68,16 @@ async def invoke(self, ctx): await super().invoke(ctx) async def setup_hook(self) -> None: - await self._load_extensions() + await self.load_extensions() if self.app.command_sync: - await self._sync_commands() + await self.sync_commands() if self.app.watch: self.watcher.start() + self.scheduler.start() + async def load_extension(self, name: str) -> None: try: await super().load_extension(name) diff --git a/pyproject.toml b/pyproject.toml index faeaac8..8ffb0d0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,8 @@ dependencies = [ "flake8", "pytest-mock", "coverage", - "watchdog" + "watchdog", + "apscheduler" ] [project.urls]