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

54 lines
1.7 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-05-21 16:56:20 +00:00
async def _permission_check(self, author, code, value, data):
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-05-21 16:56:20 +00:00
async def _create_treasure(self, author, code, value, data):
2020-05-21 13:16:03 +00:00
TreasureT = self.alchemy.get(Treasure)
treasure = await ru.asyncify(data.session.query(TreasureT).get, code)
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()
author = await data.get_author(error_if_none=True)
2020-05-24 09:36:10 +00:00
code = args[0].lower()
2020-05-21 16:56:20 +00:00
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.")
await self._permission_check(author, code, value, data)
treasure = await self._create_treasure(author, code, value, data)
2020-05-21 13:16:03 +00:00
data.session.add(treasure)
await data.session_commit()
2020-05-21 16:56:20 +00:00
await data.reply("✅ Treasure creato!")