diff --git a/pyproject.toml b/pyproject.toml index c71a9560..0ef12dfe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ [tool.poetry] name = "royalpack" - version = "5.3.5" + version = "5.3.6" description = "A Royalnet command pack for the Royal Games community" authors = ["Stefano Pigozzi "] license = "AGPL-3.0+" diff --git a/royalpack/commands/__init__.py b/royalpack/commands/__init__.py index 6826c821..85ca8b39 100644 --- a/royalpack/commands/__init__.py +++ b/royalpack/commands/__init__.py @@ -34,6 +34,7 @@ from .trivia import TriviaCommand from .steampowered import SteampoweredCommand from .steammatch import SteammatchCommand from .dota import DotaCommand +from .magickfiorygi import MagickfiorygiCommand # Enter the commands of your Pack here! available_commands = [ @@ -72,6 +73,7 @@ available_commands = [ SteampoweredCommand, SteammatchCommand, DotaCommand, + MagickfiorygiCommand, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/commands/magickfiorygi.py b/royalpack/commands/magickfiorygi.py new file mode 100644 index 00000000..f63e8d1c --- /dev/null +++ b/royalpack/commands/magickfiorygi.py @@ -0,0 +1,66 @@ +from typing import * +import royalnet +import royalnet.commands as rc +import royalnet.serf.telegram as rt +from ..tables import Alias, Fiorygi, FiorygiTransaction + + +class MagickfiorygiCommand(rc.Command): + name: str = "magickfiorygi" + + description: str = "Crea fiorygi dal nulla." + + syntax: str = "{destinatario} {quantità} {motivo}" + + async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None: + author = await data.get_author(error_if_none=True) + if author.role != "Admin": + raise rc.UserError("Non hai permessi sufficienti per eseguire questo comando.") + + user_arg = args[0] + qty_arg = args[1] + reason_arg = " ".join(args[2:]) + + if user_arg is None: + raise rc.InvalidInputError("Non hai specificato un destinatario!") + user = Alias.find_by_alias(self.alchemy, data.session, user_arg) + if user is None: + raise rc.InvalidInputError("L'utente specificato non esiste!") + + if qty_arg is None: + raise rc.InvalidInputError("Non hai specificato una quantità!") + try: + 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) diff --git a/royalpack/tables/__init__.py b/royalpack/tables/__init__.py index f6a90b78..2e5a1eba 100644 --- a/royalpack/tables/__init__.py +++ b/royalpack/tables/__init__.py @@ -12,6 +12,7 @@ from .leagueoflegends import LeagueOfLegends from .fiorygi import Fiorygi from .steam import Steam from .dota import Dota +from .fiorygitransactions import FiorygiTransaction # Enter the tables of your Pack here! available_tables = [ @@ -28,6 +29,7 @@ available_tables = [ Fiorygi, Steam, Dota, + FiorygiTransaction, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/tables/fiorygi.py b/royalpack/tables/fiorygi.py index 65392d2f..81120b9d 100644 --- a/royalpack/tables/fiorygi.py +++ b/royalpack/tables/fiorygi.py @@ -19,7 +19,7 @@ class Fiorygi: return Column(Integer, nullable=False, default=0) def __repr__(self): - return f"" + return f"<{self.__class__.__name__} di {self.user}: {self.fiorygi}>" def __str__(self): return f"{self.fiorygi} fioryg" + ("i" if self.fiorygi != 1 else "") diff --git a/royalpack/tables/fiorygitransactions.py b/royalpack/tables/fiorygitransactions.py new file mode 100644 index 00000000..75701d0d --- /dev/null +++ b/royalpack/tables/fiorygitransactions.py @@ -0,0 +1,30 @@ +from sqlalchemy import * +from sqlalchemy.orm import * +from sqlalchemy.ext.declarative import declared_attr + + +class FiorygiTransaction: + __tablename__ = "fiorygitransactions" + + @declared_attr + def id(self): + return Column(Integer, primary_key=True) + + @declared_attr + def change(self): + return Column(Integer, nullable=False) + + @declared_attr + def user_id(self): + return Column(Integer, ForeignKey("fiorygi.user_id"), nullable=False) + + @declared_attr + def user(self): + return relationship("Fiorygi", backref=backref("transactions")) + + @declared_attr + def reason(self): + return Column(String, nullable=False, default="") + + def __repr__(self): + return f"<{self.__class__.__name__}: {self.change:+} to {self.user.username} for {self.reason}>" diff --git a/royalpack/version.py b/royalpack/version.py index d2947091..d4ff852a 100644 --- a/royalpack/version.py +++ b/royalpack/version.py @@ -1 +1 @@ -semantic = "5.3.5" +semantic = "5.3.6"