diff options
| -rw-r--r-- | .snekrc | 6 | ||||
| -rw-r--r-- | bot/__main__.py | 1 | ||||
| -rw-r--r-- | bot/cogs/math.py | 95 | ||||
| -rw-r--r-- | requirements.txt | 1 | 
4 files changed, 103 insertions, 0 deletions
diff --git a/.snekrc b/.snekrc new file mode 100644 index 000000000..9f582f477 --- /dev/null +++ b/.snekrc @@ -0,0 +1,6 @@ +[all] +linters = flake8, safety, dodgy + +[flake8] +max-line-length=120 +application_import_names=bot diff --git a/bot/__main__.py b/bot/__main__.py index 88991ac53..e04aaef5c 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -33,6 +33,7 @@ bot.load_extension("bot.cogs.cogs")  bot.load_extension("bot.cogs.clickup")  bot.load_extension("bot.cogs.deployment")  bot.load_extension("bot.cogs.eval") +bot.load_extension("bot.cogs.math")  bot.load_extension("bot.cogs.fun")  bot.load_extension("bot.cogs.tags")  bot.load_extension("bot.cogs.verification") diff --git a/bot/cogs/math.py b/bot/cogs/math.py new file mode 100644 index 000000000..94024e8ae --- /dev/null +++ b/bot/cogs/math.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +from io import BytesIO +from re import search + + +from aiohttp import ClientSession + +from discord import File +from discord.ext.commands import command + +from sympy import latex +from sympy.parsing.sympy_parser import parse_expr + + +LATEX_URL = "http://latex2png.com" + + +class Math: +    def __init__(self, bot): +        self.bot = bot + +    @command() +    async def latexify(self, ctx, *, expr: str): +        fixed_expr = expr.replace('^', '**').strip('`').replace("__", "") +        try: +            parsed = parse_expr(fixed_expr, evaluate=False) + +        except SyntaxError: +            await ctx.send("Invalid expression!") + +        else: +            ltx = latex(parsed) + +            data = { +                "latex": ltx, +                "res": 300, +                "color": 808080 +            } + +            async with ClientSession() as session: +                async with session.post(LATEX_URL, data=data) as resp: +                    html = await resp.text() + +                name = search(r'hist\.request\.basename = "(?P<url>[^"]+)"', html).group('url') + +                async with session.get(f"{LATEX_URL}/output/{name}.png") as resp: +                    bytes_img = await resp.read() + +            file = File(fp=BytesIO(bytes_img), filename="latex.png") + +            await ctx.send(file=file) + +    @command() +    async def calc(self, ctx, *, expr: str): +        fixed_expr = expr.replace('^', '**').strip('`') + +        if any(x in fixed_expr for x in ("**", "__")): +            return await ctx.send( +                "You used an expression that has been disabled for security, our apologies") + +        try: +            parsed = parse_expr(fixed_expr, evaluate=False) +            result = parsed.doit() + +        except SyntaxError: +            await ctx.send("Invalid expression!") + +        else: +            ltx = latex(result) + +            data = { +                "latex": ltx, +                "res": 300, +                "color": 808080 +            } + +            async with ClientSession() as session: +                async with session.post(LATEX_URL, data=data) as resp: +                    html = await resp.text() + +                name = search(r'hist\.request\.basename = "(?P<url>[^"]+)"', html).group('url') + +                async with session.get(f"{LATEX_URL}/output/{name}.png") as resp: +                    bytes_img = await resp.read() + +            file = File(fp=BytesIO(bytes_img), filename="latex.png") + +            await ctx.send(file=file) + + +def setup(bot): +    bot.add_cog(Math(bot)) diff --git a/requirements.txt b/requirements.txt index 9e191988d..941d7f2b6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@  https://github.com/Rapptz/discord.py/archive/rewrite.zip#egg=discord.py[voice]  dulwich  multidict +sympy  |