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) @commands.command() async def ancestry(self, ctx, *args): name = args[0] headers = {"Authorization": await self.conf.token()} req = requests.get(f"https://api.pathfinder2.fr/v1/pf2/ancestry?name={name}", headers=headers).json() html = req["results"][0]["data"]["description"]["value"] 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()}")