diff --git a/pyproject.toml b/pyproject.toml index 5748f8d..b9f673f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ dependencies = { file = "requirements/_.txt" } [tool.setuptools.dynamic.optional-dependencies] dev = { file = "requirements/dev.txt" } +matrix = { file = "requirements/matrix.txt" } pycord = { file = "requirements/pycord.txt" } pyrogram = { file = "requirements/pyrogram.txt" } speed = { file = "requirements/speed.txt" } diff --git a/requirements/matrix.txt b/requirements/matrix.txt new file mode 100644 index 0000000..244adad --- /dev/null +++ b/requirements/matrix.txt @@ -0,0 +1 @@ +simplematrixbotlib~=2.12.1 \ No newline at end of file diff --git a/src/libbot/matrix/__init__.py b/src/libbot/matrix/__init__.py new file mode 100644 index 0000000..a64b6f3 --- /dev/null +++ b/src/libbot/matrix/__init__.py @@ -0,0 +1,2 @@ +# This file is left empty on purpose +# Adding imports here will cause import errors when libbot[matrix] is not installed diff --git a/src/libbot/matrix/classes/__init__.py b/src/libbot/matrix/classes/__init__.py new file mode 100644 index 0000000..39a0ed6 --- /dev/null +++ b/src/libbot/matrix/classes/__init__.py @@ -0,0 +1 @@ +from .bot import MatrixBot diff --git a/src/libbot/matrix/classes/bot.py b/src/libbot/matrix/classes/bot.py new file mode 100644 index 0000000..4004e40 --- /dev/null +++ b/src/libbot/matrix/classes/bot.py @@ -0,0 +1,63 @@ +import logging +from logging import Logger +from pathlib import Path +from typing import Dict, Any + +from typing_extensions import override + +from ...i18n.classes import BotLocale +from ...utils import json_read + +try: + from apscheduler.schedulers.asyncio import AsyncIOScheduler + from apscheduler.schedulers.background import BackgroundScheduler + from simplematrixbotlib import Bot, Creds, Config +except ImportError as exc: + raise ImportError("You need to install libbot[matrix] in order to use this class.") from exc + +logger: Logger = logging.getLogger(__name__) + + +class MatrixBot(Bot): + @override + def __init__( + self, + config: Dict[str, Any] | None = None, + config_path: str | Path = Path("config.json"), + locales_root: str | Path | None = None, + scheduler: AsyncIOScheduler | BackgroundScheduler | None = None, + smbl_creds: Creds = None, + smbl_config: Config = None, + ): + self.config: Dict[str, Any] = config if config is not None else json_read(config_path) + + super().__init__( + creds=smbl_creds if smbl_creds is not None else Creds(homeserver=self.config["homeserver"], + username=self.config["username"], + password=self.config["password"], + device_name=self.config["device_name"]), + config=smbl_config, + ) + + self.bot_locale: BotLocale = BotLocale( + default_locale=self.config["locale"], + locales_root=(Path("locale") if locales_root is None else locales_root), + ) + self.default_locale: str = self.bot_locale.default + self.locales: Dict[str, Any] = self.bot_locale.locales + + self._ = self.bot_locale._ + self.in_all_locales = self.bot_locale.in_all_locales + self.in_every_locale = self.bot_locale.in_every_locale + + self.scheduler: AsyncIOScheduler | BackgroundScheduler | None = scheduler + + @override + def run(self, scheduler_start: bool = True, scheduler_shutdown: bool = True, scheduler_wait: bool = True) -> None: + if self.scheduler is not None and scheduler_start: + self.scheduler.start() + + super().run() + + if self.scheduler is not None and scheduler_shutdown: + self.scheduler.shutdown(scheduler_wait)