Skip to content

Commit

Permalink
LaTeX: Added error checking for the equation (#141)
Browse files Browse the repository at this point in the history
* LaTeX: Added error checking for the equation

* LaTeX: Fix formatting

* Fixed LaTeX embed title being too long

* Fixed formatting

---------

Co-authored-by: Andrew Brown <[email protected]>
  • Loading branch information
49Indium and andrewj-brown authored Jul 26, 2023
1 parent 41b1bd4 commit 42ac75a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion uqcsbot/latex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from urllib.parse import quote
import requests

import discord
from discord import app_commands
Expand All @@ -20,9 +21,29 @@ async def latex(self, interaction: discord.Interaction, input: str):
"%5Cdpi%7B200%7D%5Cbg%7B36393f%7D%5Cfg%7Bwhite%7D"
f"{quote(input)}"
)

# Check that the image can be found, otherwise it is likely that the equation is invalid
status_code = requests.get(url).status_code
if status_code == requests.codes.bad_request:
await interaction.edit_original_response(
content=f"Invalid equation: {input}"
)
return
elif status_code != requests.codes.ok:
await interaction.edit_original_response(
content=f"Could not reach CodeCogs to render LaTeX"
)
return

# Will error if embed title is greater than 256 characters
if len(input) >= 256 - len('LaTeX render for ""...'):
title = f'LaTeX render for "{input[:220]}..."'
else:
title = f'LaTeX render for "{input}"'

embed = discord.Embed(
colour=discord.Colour.blue(),
title=f'Latex render for "{input}"',
title=title,
).set_image(url=f"{url}")

await interaction.edit_original_response(embed=embed)
Expand Down

0 comments on commit 42ac75a

Please sign in to comment.