1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Improve dnditem command

This commit is contained in:
Steffo 2019-08-27 14:09:29 +02:00
parent fcc3bbe111
commit 2bdb6c7729
2 changed files with 22 additions and 11 deletions

View file

@ -21,10 +21,20 @@ class DnditemCommand(Command):
interface.loop.create_task(self._fetch_dnddata()) interface.loop.create_task(self._fetch_dnddata())
async def _fetch_dnddata(self): 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 aiohttp.ClientSession() as session:
async with session.get("https://scaleway.steffo.eu/dnd/items.json") as response: async with session.get("https://5e.tools/data/items.json") as response:
j = await response.json() j = await response.json()
self._dnddata = sortedcontainers.SortedKeyList(j["item"], key=lambda i: i["name"].lower()) 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()
for item in j["item"]:
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)
def _parse_entry(self, entry): def _parse_entry(self, entry):
if isinstance(entry, str): if isinstance(entry, str):
@ -58,13 +68,14 @@ class DnditemCommand(Command):
return return
search = args.joined().lower() search = args.joined().lower()
result = self._dnddata[self._dnddata.bisect_key_left(search)] result = self._dnddata[self._dnddata.bisect_key_left(search)]
string = f'[b]{result["name"]}[/b]\n' \ string = f'[b]{result["name"]}[/b]\n'
f'[i]{result["source"]}, page {result["page"]}[/i]\n' \ if "source" in result:
f'\n' \ string += f'[i]{result["source"]}, page {result["page"]}[/i]\n'
string += f'\n' \
f'Type: [b]{result.get("type", "None")}[/b]\n' \ f'Type: [b]{result.get("type", "None")}[/b]\n' \
f'Value: [b]{result.get("value", "Priceless")}[/b]\n' \ f'Value: [b]{result.get("value", "Priceless")}[/b]\n' \
f'Weight: [b]{result.get("weight", "0")} lb[/b]\n' \ f'Weight: [b]{result.get("weight", "0")} lb[/b]\n' \
f'Rarity: [b]{result["rarity"] if result["rarity"] != "None" else "Mundane"}[/b]\n' \ f'Rarity: [b]{result["rarity"] if result.get("rarity", "None") != "None" else "Mundane"}[/b]\n' \
f'\n' f'\n'
for entry in result.get("entries", []): for entry in result.get("entries", []):
string += self._parse_entry(entry) string += self._parse_entry(entry)

View file

@ -11,7 +11,7 @@ from royalnet.database.tables import Royal, Telegram, Discord
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
log = logging.root log = logging.getLogger("royalnet.bots")
stream_handler = logging.StreamHandler() stream_handler = logging.StreamHandler()
stream_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{") stream_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{")
log.addHandler(stream_handler) log.addHandler(stream_handler)