WIP: Matrix support (with SMBL)

This commit is contained in:
kku 2025-01-01 22:35:17 +01:00
parent ae54bd5cce
commit 9bc4d0348d
5 changed files with 68 additions and 0 deletions

View File

@ -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" }

1
requirements/matrix.txt Normal file
View File

@ -0,0 +1 @@
simplematrixbotlib~=2.12.1

View File

@ -0,0 +1,2 @@
# This file is left empty on purpose
# Adding imports here will cause import errors when libbot[matrix] is not installed

View File

@ -0,0 +1 @@
from .bot import MatrixBot

View File

@ -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)