[Latex] Initial commit

This commit is contained in:
aikaterna
2020-08-31 14:25:34 -04:00
parent 940b5ea84c
commit 6a2c99143e
4 changed files with 65 additions and 0 deletions

View File

@@ -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.

4
latex/__init__.py Normal file
View File

@@ -0,0 +1,4 @@
from .latex import Latex
def setup(bot):
bot.add_cog(Latex(bot))

10
latex/info.json Normal file
View File

@@ -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."
}

49
latex/latex.py Normal file
View File

@@ -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())