From 6a2c99143ee820e47b9c3ed269fcdfb9e8f7ab1d Mon Sep 17 00:00:00 2001 From: aikaterna <20862007+aikaterna@users.noreply.github.com> Date: Mon, 31 Aug 2020 14:25:34 -0400 Subject: [PATCH] [Latex] Initial commit --- README.md | 2 ++ latex/__init__.py | 4 ++++ latex/info.json | 10 ++++++++++ latex/latex.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 latex/__init__.py create mode 100644 latex/info.json create mode 100644 latex/latex.py diff --git a/README.md b/README.md index 8e6dfc5..cdd0000 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ inspirobot - Fetch "inspirational" messages from inspirobot.me with [p]inspireme invites - Display invites that are available on the server and the information those invites contain. The bot must have the administrator permission granted on the guild to be able to use this cog. +latex - A simple cog originally by Stevy for v2 that displayes LaTeX expressions in an image. + luigipoker - Play the Luigi Poker minigame from New Super Mario Brothers. Ported from the v2 version written by themario30. noflippedtables - A v3 port of irdumb's v2 cog with a little extra surprise included. Unflip all the tables. diff --git a/latex/__init__.py b/latex/__init__.py new file mode 100644 index 0000000..24fa5c9 --- /dev/null +++ b/latex/__init__.py @@ -0,0 +1,4 @@ +from .latex import Latex + +def setup(bot): + bot.add_cog(Latex(bot)) diff --git a/latex/info.json b/latex/info.json new file mode 100644 index 0000000..200f56e --- /dev/null +++ b/latex/info.json @@ -0,0 +1,10 @@ +{ + "author": ["aikaterna", "Stevy"], + "description": "Generates an image for a LaTeX expression.", + "install_msg": "Thanks for installing, have fun.", + "short": "Generates an image for a LaTeX expression.", + "tags": ["latex"], + "requirements": ["pillow"], + "type": "COG", + "end_user_data_statement": "This cog does not persistently store data or metadata about users." +} \ No newline at end of file diff --git a/latex/latex.py b/latex/latex.py new file mode 100644 index 0000000..df0cbab --- /dev/null +++ b/latex/latex.py @@ -0,0 +1,49 @@ +import aiohttp +import discord +import io +import logging +from PIL import Image, ImageOps +from redbot.core import commands + + +log = logging.getLogger("red.aikaterna.latex") + + +class Latex(commands.Cog): + """LaTeX expressions via an image.""" + + async def red_delete_data_for_user(self, **kwargs): + """Nothing to delete.""" + return + + def __init__(self, bot): + self.bot = bot + self.session = aiohttp.ClientSession() + + @commands.guild_only() + @commands.command() + async def latex(self, ctx, *, equation): + """Takes a LaTeX expression and makes it pretty.""" + base_url = "https://latex.codecogs.com/gif.latex?%5Cbg_white%20%5CLARGE%20" + url = f"{base_url}{equation}" + + try: + async with self.session.get(url) as r: + image = await r.read() + image = Image.open(io.BytesIO(image)).convert("RGBA") + except Exception as exc: + log.exception("Something went wrong while trying to read the image:\n ", exc_info=exc) + image = None + + if not image: + return await ctx.send("I can't get the image from the website, check your console for more information.") + + image = ImageOps.expand(image, border=10, fill="white") + image_file_object = io.BytesIO() + image.save(image_file_object, format="png") + image_file_object.seek(0) + image_final = discord.File(fp=image_file_object, filename="latex.png") + await ctx.send(file=image_final) + + def cog_unload(self): + self.bot.loop.create_task(self.session.close())