WIP: Matrix support #175

Draft
profitroll wants to merge 4 commits from profitroll/matrix-support into dev
6 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,90 @@
name: Upload Python Package
on:
release:
types: [ published ]
permissions:
contents: read
jobs:
release-build:
runs-on: ubuntu-latest
container: catthehacker/ubuntu:act-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Build release distributions
run: |
python -m pip install build
python -m build
- name: Upload distributions
uses: christopherhx/gitea-upload-artifact@v4
with:
name: release-dists
path: dist/
gitea-publish:
runs-on: ubuntu-latest
container: catthehacker/ubuntu:act-latest
needs:
- release-build
permissions:
id-token: write
environment:
name: gitea
url: https://git.end-play.xyz/profitroll/-/packages/pypi/libbot
env:
GITHUB_WORKFLOW_REF: ${{ gitea.workflow_ref }}
INPUT_REPOSITORY_URL: https://git.end-play.xyz/api/packages/profitroll/pypi
steps:
- name: Retrieve release distributions
uses: christopherhx/gitea-download-artifact@v4
with:
name: release-dists
path: dist/
- name: Publish package distributions to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_GITEA_API_TOKEN }}
repository-url: https://git.end-play.xyz/api/packages/profitroll/pypi
pypi-publish:
runs-on: ubuntu-latest
container: catthehacker/ubuntu:act-latest
needs:
- release-build
permissions:
id-token: write
environment:
name: pypi
env:
GITHUB_WORKFLOW_REF: ${{ gitea.workflow_ref }}
steps:
- name: Retrieve release distributions
uses: christopherhx/gitea-download-artifact@v4
with:
name: release-dists
path: dist/
- name: Publish package distributions to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_PYPI_API_TOKEN }}

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