diff --git a/pyproject.toml b/pyproject.toml index 9a90424c..c5f4ecbb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ [tool.poetry] name = "royalnet" -version = "5.11.10" +version = "5.11.11" description = "A multipurpose bot and web framework" authors = ["Stefano Pigozzi "] license = "AGPL-3.0+" diff --git a/royalnet/__main__.py b/royalnet/__main__.py index 2fc1a11e..81f6da0f 100644 --- a/royalnet/__main__.py +++ b/royalnet/__main__.py @@ -95,8 +95,7 @@ def run(config_file: str): else: log.debug("__serfs__: Configured") - def configure_serf(n: str, module): - class_ = module.__getattribute__(f"{n}Serf") + def configure_serf(n: str, module, class_: Type[rs.Serf]): serf_cfg = serfs_cfg.get(n) if module is None: log.info(f"Serf.{n}: Not installed") @@ -123,8 +122,10 @@ def run(config_file: str): processes[f"Serf.{n}"] = ru.RoyalnetProcess(serf_constructor, None) log.info(f"Serf.{n}: Enabled") - configure_serf("Telegram", rst) - configure_serf("Discord", rsd) + if rst is not None: + configure_serf("Telegram", rst, rst.TelegramSerf) + if rsd is not None: + configure_serf("Discord", rsd, rsd.DiscordSerf) # Constellation constellation_cfg = config.get("Constellation") diff --git a/royalnet/backpack/commands/royalnetsync.py b/royalnet/backpack/commands/royalnetsync.py index 2ccec038..841ba4a6 100644 --- a/royalnet/backpack/commands/royalnetsync.py +++ b/royalnet/backpack/commands/royalnetsync.py @@ -1,6 +1,15 @@ import royalnet.commands as rc -import royalnet.serf.discord as rsd -import royalnet.serf.telegram as rst + +try: + import royalnet.serf.telegram as rst +except ImportError: + rst = None + +try: + import royalnet.serf.discord as rsd +except ImportError: + rsd = None + import royalnet.utils as ru from ..tables.discord import Discord from ..tables.telegram import Telegram @@ -14,25 +23,25 @@ class RoyalnetsyncCommand(rc.Command): syntax: str = "{username} {password}" async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None: - author = await data.get_author(error_if_none=False) - if author is not None: - raise rc.UserError(f"This account is already connected to {author}!") - - username = args[0] - password = " ".join(args[1:]) - - user = await data.find_user(username) - if user is None: - raise rc.UserError("No such user.") - try: - successful = user.test_password(password) - except ValueError: - raise rc.UserError(f"User {user} has no password set!") - if not successful: - raise rc.InvalidInputError(f"Invalid password!") - async with data.session_acm() as session: - if isinstance(self.serf, rst.TelegramSerf): + author = await data.find_author(session=session, required=False) + if author is not None: + raise rc.UserError(f"This account is already connected to {author}!") + + username = args[0] + password = " ".join(args[1:]) + + user = await data.find_user(session=session, identifier=username) + if user is None: + raise rc.UserError("No such user.") + try: + successful = user.test_password(password) + except ValueError: + raise rc.UserError(f"User {user} has no password set!") + if not successful: + raise rc.InvalidInputError(f"Invalid password!") + + if rst is not None and isinstance(self.serf, rst.TelegramSerf): import telegram message: telegram.Message = data.message from_user: telegram.User = message.from_user @@ -58,7 +67,7 @@ class RoyalnetsyncCommand(rc.Command): await ru.asyncify(session.commit) await data.reply(f"↔️ Account {tg_user} synced to {user}!") - elif isinstance(self.serf, rsd.DiscordSerf): + elif rsd is not None and isinstance(self.serf, rsd.DiscordSerf): import discord message: discord.Message = data.message ds_author: discord.User = message.author