1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-30 15:04:18 +00:00
royalnet/rpgpack/commands/dnditem.py

105 lines
3.3 KiB
Python
Raw Normal View History

2019-11-11 09:34:05 +00:00
import aiohttp
import sortedcontainers
2020-02-18 00:15:36 +00:00
import logging
2019-11-11 09:34:05 +00:00
from royalnet.commands import *
2020-02-18 00:15:36 +00:00
from royalnet.utils import sentry_exc
2019-11-11 09:34:05 +00:00
from ..utils import parse_5etools_entry
2020-02-18 00:15:36 +00:00
log = logging.getLogger(__name__)
2019-11-11 09:34:05 +00:00
class DnditemCommand(Command):
name: str = "dnditem"
aliases = ["item"]
description: str = "Ottieni informazioni su un oggetto di D&D5e."
syntax = "{nomeoggetto}"
_dnddata: sortedcontainers.SortedKeyList = None
def __init__(self, interface: CommandInterface):
super().__init__(interface)
2019-12-04 00:47:00 +00:00
self.loop.create_task(self._fetch_dnddata())
2019-11-11 09:34:05 +00:00
async def _fetch_dnddata(self):
self._dnddata = self._dnddata = sortedcontainers.SortedKeyList([], key=lambda i: i["name"].lower())
async with aiohttp.ClientSession() as session:
async with session.get("https://5e.tools/data/items.json") as response:
j = await response.json()
for item in j["item"]:
self._dnddata.add(item)
async with session.get("https://5e.tools/data/fluff-items.json") as response:
j = await response.json()
2020-02-18 00:15:36 +00:00
for item in j["itemFluff"]:
2019-11-11 09:34:05 +00:00
self._dnddata.add(item)
async with session.get("https://5e.tools/data/items-base.json") as response:
j = await response.json()
for item in j["baseitem"]:
self._dnddata.add(item)
2020-02-18 00:15:36 +00:00
self._test_all()
@staticmethod
def _parse_item(item: dict) -> str:
string = [f'📦 [b]{item["name"]}[/b]\n']
# Source (manual, page)
if "source" in item:
if "page" in item:
string.append(f'[i]{item["source"]}, page {item["page"]}[/i]\n')
else:
string.append(f'[i]{item["source"]}[/i]\n')
string.append("\n")
# Type
item_type = item.get("type")
if item_type:
2020-02-22 01:16:54 +00:00
string.append(f"Type: [b]{item_type}[/b]\n")
2020-02-18 00:15:36 +00:00
# Value
item_value = item.get("value")
if item_value:
2020-02-22 01:16:54 +00:00
string.append(f"Value: [b]{item_value}[/b]\n")
2020-02-18 00:15:36 +00:00
# Weight
item_weight = item.get("weight")
if item_weight:
2020-02-22 01:16:54 +00:00
string.append(f"Value: [b]{item_weight}[/b]\n")
2020-02-18 00:15:36 +00:00
# Rarity
item_rarity = item.get("rarity")
if item_rarity:
2020-02-22 01:16:54 +00:00
string.append(f"Rarity: [b]{item_rarity}[/b]\n")
2020-02-18 00:15:36 +00:00
else:
2020-02-22 01:16:54 +00:00
string.append(f"Rarity: [b]Mundane[/b]\n")
string.append("\n")
2020-02-18 00:15:36 +00:00
# Text entries
for entry in item.get("entries", []):
string += parse_5etools_entry(entry)
string += "\n\n"
return "".join(string)
2019-11-11 09:34:05 +00:00
async def run(self, args: CommandArgs, data: CommandData) -> None:
if self._dnddata is None:
await data.reply("⚠️ Il database degli oggetti di D&D non è ancora stato scaricato.")
return
search = args.joined().lower()
result = self._dnddata[self._dnddata.bisect_key_left(search)]
2020-02-18 00:15:36 +00:00
await data.reply(self._parse_item(result))
def _test_all(self):
for item in self._dnddata:
try:
log.debug(f"Testing: {item['name']}")
self._parse_item(item)
except Exception as e:
log.error(f"Failed: {item['name']}")
sentry_exc(e)
breakpoint()
log.info(f"All item tests complete!")