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

70 lines
2.7 KiB
Python
Raw Normal View History

2019-11-11 09:34:05 +00:00
import re
# noinspection PyUnresolvedReferences
from royalnet.commands import *
from ..tables import DndCharacter
from ..utils import DndProficiencyType
class DndnewCommand(Command):
name: str = "dndnew"
description: str = "Create a new DnD character."
aliases = ["dn", "dndn", "new", "dnew"]
syntax = "{name}\n{character_sheet}"
def _search_value(self, name: str, string: str):
2019-12-10 12:26:27 +00:00
return re.search(r"\s*" + name + r"\s*([0-9.]+)\s*", string, re.IGNORECASE)
2019-11-11 09:34:05 +00:00
def _parse(self, character_sheet: str) -> dict:
2019-12-04 00:47:00 +00:00
columns = list(self.alchemy.get(DndCharacter).__table__.columns)
2019-11-11 09:34:05 +00:00
column_names = [column.name for column in columns if (not column.primary_key and
not column.foreign_keys and
column.name != "name")]
arguments = {}
for column_name in column_names:
match = self._search_value(column_name, character_sheet)
if match:
if column_name.endswith("_proficiency"):
arguments[column_name] = DndProficiencyType(float(match.group(1)))
else:
arguments[column_name] = match.group(1)
return arguments
def _syntax(self) -> str:
2019-12-04 00:47:00 +00:00
columns = list(self.alchemy.get(DndCharacter).__table__.columns)
2019-11-11 09:34:05 +00:00
column_names = [column.name for column in columns if (not column.primary_key and
not column.foreign_keys and
column.name != "name")]
message = " How to create a new character:\n[p]/dndnew YOUR_CHARACTER_NAME\n"
for column_name in column_names:
message += f"{column_name} _\n"
message += "[/p]"
return message
async def run(self, args: CommandArgs, data: CommandData) -> None:
character_sheet = args.joined()
if character_sheet == "":
await data.reply(self._syntax())
return
creator = await data.get_author()
name, rest = character_sheet.split("\n", 1)
2019-12-04 00:47:00 +00:00
character = self.alchemy.get(DndCharacter)(name=name, creator=creator, **self._parse(rest))
2019-11-11 09:34:05 +00:00
data.session.add(character)
try:
await data.session_commit()
except Exception as err:
# THIS IS INTENDED
if err.__class__.__name__ == "IntegrityError":
param_name = re.search(r'in column "(\S+)"', err.args[0]).group(1)
raise CommandError(f"Mandatory parameter '{param_name}' is missing.")
raise
await data.reply(f"✅ Character [b]{character.name}[/b] created!")