tiamat-cogs/tiamat/tiamat.py

116 lines
6.6 KiB
Python

from redbot.core import commands
import discord
import requests
from itertools import islice
class Tiamat(commands.Cog):
""" Get information from D&D 5e's srd """
def __init__(self, bot):
self.bot = bot
@commands.command()
async def monster(self, ctx, *args):
name = '-'.join(args).lower()
data = requests.get(f"https://www.dnd5eapi.co/api/monsters/{name}").json()
if "error" in data:
await ctx.send(f"{' '.join(args)} not found, check that the name is spelled right and that the creature "
f"is in the System Reference Document")
else:
initial_embed = discord.Embed(title=data["name"])
initial_embed.add_field(name="HP", value=data["hit_points"])
initial_embed.add_field(name="AC", value=data["armor_class"])
for ability in ["strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma"]:
initial_embed.add_field(name=ability, value=data[ability], inline=True)
attack_embeds = []
for i, action_group in enumerate(chunk(data["actions"], 3)):
attack_embed = discord.Embed(title=f"Actions [{i}]")
for action in action_group:
attack_embed.add_field(name=action["name"], value=action["desc"])
attack_embeds.append(attack_embed)
legendary_embeds = []
if "legendary_actions" in data:
for i, action_group in enumerate(chunk(data["legendary_actions"], 3)):
attack_embed = discord.Embed(title=f"Legendary Actions [{i}]")
for action in action_group:
attack_embed.add_field(name=action["name"], value=action["desc"])
legendary_embeds.append(attack_embed)
await ctx.send(embed=initial_embed)
for attack_embed in attack_embeds:
await ctx.send(embed=attack_embed)
for legendary_embed in legendary_embeds:
await ctx.send(embed=legendary_embed)
@commands.command()
async def spell(self, ctx, *args):
name = '-'.join(args).lower()
data = requests.get(f"https://www.dnd5eapi.co/api/spells/{name}").json()
if "error" in data:
await ctx.send(f"{' '.join(args)} not found, check that the name is spelled right and that the spell "
f"is in the System Reference Document")
else:
embed = discord.Embed(title=data["name"])
embed.add_field(name="Level", value=data["level"], inline=True)
embed.add_field(name="Range", value=data["range"], inline=True)
embed.add_field(name="Components", value=', '.join(data["components"]), inline=False)
if "material" in data:
embed.add_field(name="Material Components", value=data["material"], inline=False)
embed.add_field(name="Description", value=data["desc"][0], inline=False)
embed.add_field(name="Upcasting", value=data["higher_level"][0], inline=False)
embed.add_field(name="School", value=data["school"]["name"], inline=True)
embed.add_field(name="Ritual", value=data["ritual"], inline=True)
embed.add_field(name="Classes", value=', '.join(i["name"] for i in data["classes"]), inline=False)
embed.add_field(name="Subclasses", value=', '.join(i["name"] for i in data["subclasses"]), inline=False)
await ctx.send(embed=embed)
@commands.command()
async def armour(self, ctx, *args):
name = '-'.join(args).lower().replace("-armor", "").replace("-armour", "")
data = requests.get(f"https://www.dnd5eapi.co/api/equipment/{name}").json()
if "error" in data:
await ctx.send(f"{' '.join(args)} not found, check that the name is spelled right and that the spell "
f"is in the System Reference Document")
try:
embed = discord.Embed(title=data["name"])
embed.add_field(name="Category", value=data["armor_category"], inline=False)
embed.add_field(name="Base", value=data["armor_class"]["base"], inline=True)
if data["armor_class"]["max_bonus"] is not None:
embed.add_field(name="Max Dex Bonus", value=data["armor_class"]["max_bonus"], inline=True)
embed.add_field(name="Cost", value=f"""{data["cost"]["quantity"]} {data["cost"]["unit"]}""", inline=False)
embed.add_field(name="Strength Minimum", value=data["str_minimum"], inline=True)
embed.add_field(name="Stealth Disadvantage", value=data["stealth_disadvantage"], inline=True)
embed.add_field(name="Weight", value=data["weight"], inline=True)
await ctx.send(embed=embed)
except KeyError:
await ctx.send("Error: item is not armour")
@commands.command()
async def weapon(self, ctx, *args):
name = '-'.join(args).lower().replace("-armor", "").replace("-armour", "")
data = requests.get(f"https://www.dnd5eapi.co/api/equipment/{name}").json()
if "error" in data:
await ctx.send(f"{' '.join(args)} not found, check that the name is spelled right and that the spell "
f"is in the System Reference Document")
try:
embed = discord.Embed(title=data["name"])
embed.add_field(name="Category", value=data["weapon_category"], inline=False)
embed.add_field(name="Range", value=data["weapon_range"], inline=True)
embed.add_field(name="Damage", value=f'{data["damage"]["damage_dice"]} ' +
f'{data["damage"]["damage_type"]["name"]}', inline=True)
if "two_handed_damage" in data:
embed.add_field(name="Two Handed Damage", value=data["two_handed_damage"]["damage_dice"], inline=True)
embed.add_field(name="Properties", value=', '.join(i["name"] for i in data["properties"]), inline=False)
if data["weapon_range"] != "Melee":
embed.add_field(name="Normal Range", value=data["range"]["normal"], inline=True)
embed.add_field(name="Long Range", value=data["range"]["Long"], inline=True)
embed.add_field(name="Cost", value=f"""{data["cost"]["quantity"]} {data["cost"]["unit"]}""", inline=False)
embed.add_field(name="Weight", value=data["weight"], inline=False)
await ctx.send(embed=embed)
except KeyError:
await ctx.send("Error: item is not a weapon")
def chunk(it, size):
it = iter(it)
return iter(lambda: tuple(islice(it, size)), ())