tiamat-cogs/sarenrae/sarenrae.py
2021-09-01 22:02:00 +01:00

64 lines
2.1 KiB
Python

from redbot.core import commands, Config, checks
import discord
import requests
import imgkit
from PIL import Image, ImageOps
import io
UID = 0x181eff7aacab91fa4051d1d995d44050
def image(html):
open_image = imgkit.from_string(html, False)
open_image = Image.open(io.BytesIO(open_image))
image_box = ImageOps.invert(open_image.convert("RGB")).getbbox()
file = io.BytesIO()
open_image.crop(image_box).save(file, "JPEG")
file.seek(0)
return file
class Sarenrae(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.conf = Config.get_conf(self, identifier=UID, force_registration=True)
default_global = {
"token": "",
}
self.conf.register_global(**default_global)
async def api_ping(self, category, name):
headers = {"Authorization": await self.conf.token()}
req = requests.get(f"https://api.pathfinder2.fr/v1/pf2/{category}?name={name}", headers=headers).json()
return req["results"][0]["data"]["description"]["value"]
@commands.command()
async def ancestry(self, ctx, *args):
name = ' '.join(args)
html = await self.api_ping("ancestry", name)
await ctx.send(file=discord.File(image(html), filename=f"{name}.jpg"))
@commands.command()
async def action(self, ctx, *args):
name = ' '.join(args)
html = await self.api_ping("action", name)
await ctx.send(file=discord.File(image(html), filename=f"{name}.jpg"))
@commands.command()
async def sar_spell(self, ctx, *args):
name = ' '.join(args)
html = await self.api_ping("spell", name)
await ctx.send(file=discord.File(image(html), filename=f"{name}.jpg"))
@commands.command()
async def item(self, ctx, *args):
name = ' '.join(args)
html = await self.api_ping("equipment", name)
await ctx.send(file=discord.File(image(html), filename=f"{name}.jpg"))
@commands.command()
@checks.is_owner()
async def sarenrae_token(self, ctx, *args):
await self.conf.token.set(args[0])
return await ctx.send(f"Set token to {await self.conf.token()}")