From a667bcb9bbb3f6599d13257d7e65d8044fde83f8 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 1 Nov 2020 22:39:15 +0100 Subject: [PATCH] Add tex extension --- extensions/__init__.py | 1 + extensions/tex.py | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 extensions/tex.py diff --git a/extensions/__init__.py b/extensions/__init__.py index 50583da..f26c4db 100644 --- a/extensions/__init__.py +++ b/extensions/__init__.py @@ -10,3 +10,4 @@ bot.load_extension("extensions.presentation") bot.load_extension("extensions.rorec") bot.load_extension("extensions.warn") bot.load_extension("extensions.pcp") +bot.load_extension("extensions.tex") diff --git a/extensions/tex.py b/extensions/tex.py new file mode 100644 index 0000000..f94620d --- /dev/null +++ b/extensions/tex.py @@ -0,0 +1,54 @@ +from urllib.parse import urlencode + +from discord import Embed +from discord.ext import commands + +from administrator.logger import logger + + +extension_name = "TeX" + + +class TeX(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + self.polls = {} + + def description(self): + return "Render TeX formula" + + @commands.group("tex", pass_context=True) + async def tex(self, ctx: commands.Context): + if ctx.message.content.count("`") == 2: + start = ctx.message.content.find("`") + end = ctx.message.content.find("`", start+1) + command = ctx.message.content[start+1:end] + await ctx.send(f"https://chart.apis.google.com/chart?cht=tx&chs=40&{urlencode({'chl': command})}") + elif ctx.invoked_subcommand is None: + await ctx.invoke(self.tex_help) + + @tex.group("help", pass_context=True) + async def tex_help(self, ctx: commands.Context): + embed = Embed(title="TeX help") + embed.add_field(name="tex \`formula\`", value="Render a TeX formula", inline=False) + await ctx.send(embed=embed) + + +def setup(bot): + logger.info(f"Loading...") + try: + bot.add_cog(TeX(bot)) + except Exception as e: + logger.error(f"Error loading: {e}") + else: + logger.info(f"Load successful") + + +def teardown(bot): + logger.info(f"Unloading...") + try: + bot.remove_cog("TeX") + except Exception as e: + logger.error(f"Error unloading: {e}") + else: + logger.info(f"Unload successful")