Compare commits

..

4 Commits

Author SHA1 Message Date
a458788841
Added device info and bot prefix
All checks were successful
Analysis / SonarCloud (pull_request) Successful in 41s
Tests / Build and Test (3.11) (pull_request) Successful in 1m17s
Tests / Build and Test (3.12) (pull_request) Successful in 1m58s
Tests / Build and Test (3.13) (pull_request) Successful in 1m21s
2025-01-04 18:01:49 +01:00
b76f727263
Replaced MatrixBot.config with MatrixBot.bot_config because .config is already used by smbl
All checks were successful
Analysis / SonarCloud (pull_request) Successful in 1m52s
Tests / Build and Test (3.11) (pull_request) Successful in 1m18s
Tests / Build and Test (3.12) (pull_request) Successful in 1m22s
Tests / Build and Test (3.13) (pull_request) Successful in 1m23s
2025-01-04 17:49:44 +01:00
kku
5a244f603d TEST: Publishing Action
All checks were successful
Analysis / SonarCloud (pull_request) Successful in 40s
Tests / Build and Test (3.11) (pull_request) Successful in 1m18s
Tests / Build and Test (3.12) (pull_request) Successful in 1m28s
Tests / Build and Test (3.13) (pull_request) Successful in 1m24s
2025-01-02 13:51:36 +01:00
kku
9bc4d0348d WIP: Matrix support (with SMBL) 2025-01-01 22:35:17 +01:00
8 changed files with 140 additions and 46 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" }

View File

@ -3,7 +3,7 @@ build==1.2.2.post1
isort==5.13.2
mypy==1.14.1
pylint==3.3.3
pytest-asyncio==0.25.1
pytest-asyncio==0.25.0
pytest-cov==6.0.0
pytest==8.3.4
tox==4.23.2

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,80 @@
import logging
from logging import Logger
from pathlib import Path
from typing import Dict, Any
from typing_extensions import override
from ... import __version__ as __libbot_version__
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.bot_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.bot_config["bot"]["homeserver"],
username=self.bot_config["bot"]["username"],
password=self.bot_config["bot"]["password"],
device_name=(
f"LibBotUniversal v{__libbot_version__}"
if "device_name" not in self.bot_config["bot"]
else self.bot_config["bot"]["device_name"]
),
)
),
config=smbl_config,
)
self.bot_prefix: str = (
"!" if "prefix" not in self.bot_config["bot"] else self.bot_config["bot"]["prefix"]
)
self.bot_locale: BotLocale = BotLocale(
default_locale=self.bot_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)

View File

@ -1,13 +1,9 @@
import logging
from logging import Logger
from pathlib import Path
from typing import Any, Dict
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
@ -15,7 +11,14 @@ try:
except ImportError as exc:
raise ImportError("You need to install libbot[pycord] in order to use this class.") from exc
logger: Logger = logging.getLogger(__name__)
try:
from ujson import loads
except ImportError:
from json import loads
from ...i18n.classes import BotLocale
logger = logging.getLogger(__name__)
class PycordBot(Bot):
@ -29,7 +32,11 @@ class PycordBot(Bot):
scheduler: AsyncIOScheduler | BackgroundScheduler | None = None,
**kwargs,
):
self.config: Dict[str, Any] = config if config is not None else json_read(config_path)
if config is None:
with open(config_path, "r", encoding="utf-8") as f:
self.config: dict = loads(f.read())
else:
self.config = config
super().__init__(
debug_guilds=(self.config["bot"]["debug_guilds"] if self.config["debug"] else None),

View File

@ -2,7 +2,6 @@ import asyncio
import logging
import sys
from datetime import datetime, timedelta
from logging import Logger
from os import cpu_count, getpid
from pathlib import Path
from time import time
@ -10,12 +9,6 @@ from typing import Any, Dict, List
from typing_extensions import override
from .command import PyroCommand
from .commandset import CommandSet
from ...i18n import _
from ...i18n.classes import BotLocale
from ...utils import json_read
try:
import pyrogram
from apscheduler.schedulers.asyncio import AsyncIOScheduler
@ -42,7 +35,12 @@ try:
except ImportError:
from json import dumps, loads
logger: Logger = logging.getLogger(__name__)
from ...i18n.classes import BotLocale
from ...i18n import _
from .command import PyroCommand
from .commandset import CommandSet
logger = logging.getLogger(__name__)
class PyroClient(Client):
@ -68,7 +66,11 @@ class PyroClient(Client):
scheduler: AsyncIOScheduler | BackgroundScheduler | None = None,
**kwargs,
):
self.config: Dict[str, Any] = config if config is not None else json_read(config_path)
if config is None:
with open(config_path, "r", encoding="utf-8") as f:
self.config: dict = loads(f.read())
else:
self.config = config
super().__init__(
name=name,