diff --git a/royalpack/commands/magickfiorygi.py b/royalpack/commands/magickfiorygi.py index 994d03d9..f9f8f0c7 100644 --- a/royalpack/commands/magickfiorygi.py +++ b/royalpack/commands/magickfiorygi.py @@ -34,34 +34,8 @@ class MagickfiorygiCommand(rc.Command): qty = int(qty_arg) except ValueError: raise rc.InvalidInputError("La quantità specificata non è un numero!") - if qty == 0: - raise rc.InvalidInputError("La quantità non può essere 0!") if reason_arg == "": raise rc.InvalidInputError("Non hai specificato un motivo!") - transaction = self.alchemy.get(FiorygiTransaction)( - user_id=user.uid, - change=qty, - reason=reason_arg - ) - data.session.add(transaction) - user.fiorygi.fiorygi += qty - await data.session_commit() - - if len(user.telegram) > 0: - user_str = user.telegram[0].mention() - else: - user_str = user.username - - if qty > 0: - msg = f"💰 [b]{user_str}[/b] ha ottenuto [b]{qty}[/b] fioryg{'i' if qty != 1 else ''} per [i]{reason_arg}[/i]!" - else: - msg = f"💸 [b]{user_str}[/b] ha perso [b]{-qty}[/b] fioryg{'i' if qty != -1 else ''} per [i]{reason_arg}[/i]." - - client = self.serf.client - await self.serf.api_call(client.send_message, - chat_id=self.config["Telegram"]["main_group_id"], - text=rt.escape(msg), - parse_mode="HTML", - disable_webpage_preview=True) + await FiorygiTransaction.spawn_fiorygi(data, user, qty, reason_arg) diff --git a/royalpack/events/__init__.py b/royalpack/events/__init__.py index 6637fbe2..380d5cb2 100644 --- a/royalpack/events/__init__.py +++ b/royalpack/events/__init__.py @@ -7,6 +7,7 @@ from .discord_queue import DiscordQueueEvent from .discord_pause import DiscordPauseEvent from .discord_playable import DiscordPlaymodeEvent from .discord_lazy_play import DiscordLazyPlayEvent +from .telegram_message import TelegramMessageEvent # Enter the commands of your Pack here! available_events = [ @@ -18,6 +19,7 @@ available_events = [ DiscordPauseEvent, DiscordPlaymodeEvent, DiscordLazyPlayEvent, + TelegramMessageEvent, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/events/telegram_message.py b/royalpack/events/telegram_message.py new file mode 100644 index 00000000..2e8412f5 --- /dev/null +++ b/royalpack/events/telegram_message.py @@ -0,0 +1,28 @@ +import logging +import telegram +from typing import * +from royalnet.serf.telegram.telegramserf import TelegramSerf, escape +from royalnet.commands import * + + +log = logging.getLogger(__name__) + + +class TelegramMessageEvent(Event): + name = "telegram_message" + + async def run(self, chat_id, text, **kwargs) -> dict: + if not self.interface.name == "telegram": + raise UnsupportedError() + + # noinspection PyTypeChecker + serf: TelegramSerf = self.interface.serf + + log.debug("Forwarding message from Herald to Telegram.") + await serf.api_call(serf.client.send_message, + chat_id=chat_id, + text=escape(text), + parse_mode="HTML", + disable_web_page_preview=True) + + return {} diff --git a/royalpack/tables/fiorygitransactions.py b/royalpack/tables/fiorygitransactions.py index 28fcbf73..41b1f6c7 100644 --- a/royalpack/tables/fiorygitransactions.py +++ b/royalpack/tables/fiorygitransactions.py @@ -1,7 +1,15 @@ +from typing import * + from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declared_attr +from .fiorygi import Fiorygi +from royalnet.utils import asyncify + +if TYPE_CHECKING: + from royalnet.commands import CommandData + class FiorygiTransaction: __tablename__ = "fiorygitransactions" @@ -32,3 +40,38 @@ class FiorygiTransaction: def __repr__(self): return f"<{self.__class__.__name__}: {self.change:+} to {self.user.username} for {self.reason}>" + + @classmethod + async def spawn_fiorygi(cls, data: "CommandData", user, qty: int, reason: str): + if user.fiorygi is None: + data.session.add(data._interface.alchemy.get(Fiorygi)( + user_id=user.uid, + fiorygi=0 + )) + await data.session_commit() + + transaction = data._interface.alchemy.get(FiorygiTransaction)( + user_id=user.uid, + change=qty, + reason=reason + ) + data.session.add(transaction) + + user.fiorygi.fiorygi += qty + await data.session_commit() + + if len(user.telegram) > 0: + user_str = user.telegram[0].mention() + else: + user_str = user.username + + if qty > 0: + msg = f"💰 [b]{user_str}[/b] ha ottenuto [b]{qty}[/b] fioryg{'i' if qty != 1 else ''} per [i]{reason}[/i]!" + elif qty == 0: + msg = f"❓ [b]{user_str}[/b] ha ottenuto [b]{qty}[/b] fioryg{'i' if qty != 1 else ''}...? Per [i]{reason}[/i]...? Cosa?" + else: + msg = f"💸 [b]{user_str}[/b] ha perso [b]{-qty}[/b] fioryg{'i' if qty != -1 else ''} per [i]{reason}[/i]." + + await data._interface.call_herald_event("telegram", "telegram_message", + chat_id=data._interface.config["Telegram"]["main_group_id"], + text=msg)