1
0
Fork 0

Merge pull request #30 from flifloo/tex

Add tex extension
This commit is contained in:
Ethanell 2020-11-01 22:39:44 +01:00
commit 8a22256560
2 changed files with 55 additions and 0 deletions

View file

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

54
extensions/tex.py Normal file
View file

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