Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaTeX: Added error checking for the equation #141

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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