from redbot.core import commands, Config, checks import discord import requests import imgkit from PIL import Image, ImageOps import os UID = 0x181eff7aacab91fa4051d1d995d44050 def image(name, html): open_image = imgkit.from_string(html, False) open_image = Image.open(open_image) image_box = ImageOps.invert(open_image.convert("RGB")).getbbox() open_image.crop(image_box).save(f"{name}_cropped.png") 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"] filename = f"ancestry-{name}" image(filename, html) await ctx.send(file=discord.File(open(f"ancestry/{name}.png", "rb"))) os.remove(f"{filename}.png") os.remove(f"{filename}_cropped.png") @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()}")