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

55 lines
1.8 KiB
Python
Raw Normal View History

2020-05-21 13:16:03 +00:00
from typing import *
import royalnet
import royalnet.commands as rc
import royalnet.utils as ru
from ..tables import Treasure
2020-05-21 16:56:20 +00:00
class MagicktreasureCommand(rc.Command):
name: str = "magicktreasure"
2020-05-21 13:16:03 +00:00
2020-05-21 16:56:20 +00:00
description: str = "Crea un nuovo Treasure di Fiorygi (senza spendere i tuoi)."
2020-05-21 13:16:03 +00:00
syntax: str = "{codice} {valore}"
2020-08-20 01:20:53 +00:00
async def _permission_check(self, author, code, value, data, session):
2020-05-21 13:16:03 +00:00
if "banker" not in author.roles:
raise rc.UserError("Non hai permessi sufficienti per eseguire questo comando.")
2020-05-21 16:56:20 +00:00
return author
2020-05-21 13:16:03 +00:00
2020-08-20 01:20:53 +00:00
async def _create_treasure(self, author, code, value, data, session):
2020-05-21 13:16:03 +00:00
TreasureT = self.alchemy.get(Treasure)
2020-08-20 01:20:53 +00:00
treasure = await ru.asyncify(session.query(TreasureT).get, code)
2020-05-21 13:16:03 +00:00
if treasure is not None:
raise rc.UserError("Esiste già un Treasure con quel codice.")
treasure = TreasureT(
code=code,
value=value,
redeemed_by=None
)
2020-05-21 16:56:20 +00:00
return treasure
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
await data.delete_invoking()
2020-08-20 01:20:53 +00:00
async with data.session_acm() as session:
2020-08-23 22:13:38 +00:00
author = await data.find_author(session=session, required=True)
code = args[0].lower()
try:
value = int(args[1])
except ValueError:
raise rc.InvalidInputError("Il valore deve essere maggiore o uguale a 0.")
if value < 0:
raise rc.InvalidInputError("Il valore deve essere maggiore o uguale a 0.")
2020-08-20 01:20:53 +00:00
await self._permission_check(author, code, value, data, session)
treasure = await self._create_treasure(author, code, value, data, session)
session.add(treasure)
await ru.asyncify(session.commit)
2020-05-21 13:16:03 +00:00
2020-05-21 16:56:20 +00:00
await data.reply("✅ Treasure creato!")