1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Refactor fiorygi spawning

This commit is contained in:
Steffo 2020-04-26 23:13:13 +02:00
parent 6022e2eea3
commit 8c2c49af71
4 changed files with 74 additions and 27 deletions

View file

@ -34,34 +34,8 @@ class MagickfiorygiCommand(rc.Command):
qty = int(qty_arg) qty = int(qty_arg)
except ValueError: except ValueError:
raise rc.InvalidInputError("La quantità specificata non è un numero!") raise rc.InvalidInputError("La quantità specificata non è un numero!")
if qty == 0:
raise rc.InvalidInputError("La quantità non può essere 0!")
if reason_arg == "": if reason_arg == "":
raise rc.InvalidInputError("Non hai specificato un motivo!") raise rc.InvalidInputError("Non hai specificato un motivo!")
transaction = self.alchemy.get(FiorygiTransaction)( await FiorygiTransaction.spawn_fiorygi(data, user, qty, reason_arg)
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)

View file

@ -7,6 +7,7 @@ from .discord_queue import DiscordQueueEvent
from .discord_pause import DiscordPauseEvent from .discord_pause import DiscordPauseEvent
from .discord_playable import DiscordPlaymodeEvent from .discord_playable import DiscordPlaymodeEvent
from .discord_lazy_play import DiscordLazyPlayEvent from .discord_lazy_play import DiscordLazyPlayEvent
from .telegram_message import TelegramMessageEvent
# Enter the commands of your Pack here! # Enter the commands of your Pack here!
available_events = [ available_events = [
@ -18,6 +19,7 @@ available_events = [
DiscordPauseEvent, DiscordPauseEvent,
DiscordPlaymodeEvent, DiscordPlaymodeEvent,
DiscordLazyPlayEvent, DiscordLazyPlayEvent,
TelegramMessageEvent,
] ]
# Don't change this, it should automatically generate __all__ # Don't change this, it should automatically generate __all__

View file

@ -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 {}

View file

@ -1,7 +1,15 @@
from typing import *
from sqlalchemy import * from sqlalchemy import *
from sqlalchemy.orm import * from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declared_attr 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: class FiorygiTransaction:
__tablename__ = "fiorygitransactions" __tablename__ = "fiorygitransactions"
@ -32,3 +40,38 @@ class FiorygiTransaction:
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__name__}: {self.change:+} to {self.user.username} for {self.reason}>" 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)