From 2b1bea5464e61d5afea883d0ea8724951c7a4610 Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Sat, 22 Jul 2023 23:30:54 +1000 Subject: [PATCH 1/4] LaTeX: Added error checking for the equation --- uqcsbot/latex.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/uqcsbot/latex.py b/uqcsbot/latex.py index aebb492..3bd284d 100644 --- a/uqcsbot/latex.py +++ b/uqcsbot/latex.py @@ -1,4 +1,5 @@ from urllib.parse import quote +import requests import discord from discord import app_commands @@ -20,6 +21,16 @@ 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 + embed = discord.Embed( colour=discord.Colour.blue(), title=f'Latex render for "{input}"', From 361a886f1158a04064e999a8e6943a849dbee9a3 Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Sat, 22 Jul 2023 23:35:25 +1000 Subject: [PATCH 2/4] LaTeX: Fix formatting --- uqcsbot/latex.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/uqcsbot/latex.py b/uqcsbot/latex.py index 3bd284d..27aaf91 100644 --- a/uqcsbot/latex.py +++ b/uqcsbot/latex.py @@ -25,12 +25,16 @@ async def latex(self, interaction: discord.Interaction, input: str): # 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}") + 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") + await interaction.edit_original_response( + content=f"Could not reach CodeCogs to render LaTeX" + ) return - + embed = discord.Embed( colour=discord.Colour.blue(), title=f'Latex render for "{input}"', From 66a033aeda2a119d10a4f72c0791962c192447af Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Wed, 26 Jul 2023 23:30:21 +1000 Subject: [PATCH 3/4] Fixed LaTeX embed title being too long --- uqcsbot/latex.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/uqcsbot/latex.py b/uqcsbot/latex.py index 27aaf91..696fe0c 100644 --- a/uqcsbot/latex.py +++ b/uqcsbot/latex.py @@ -35,9 +35,15 @@ async def latex(self, interaction: discord.Interaction, input: str): ) 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) From d22aae64a62374b228af5c0f97f0387daed2d20f Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Wed, 26 Jul 2023 23:33:27 +1000 Subject: [PATCH 4/4] Fixed formatting --- uqcsbot/latex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uqcsbot/latex.py b/uqcsbot/latex.py index 696fe0c..8c79f58 100644 --- a/uqcsbot/latex.py +++ b/uqcsbot/latex.py @@ -40,7 +40,7 @@ async def latex(self, interaction: discord.Interaction, input: str): title = f'LaTeX render for "{input[:220]}..."' else: title = f'LaTeX render for "{input}"' - + embed = discord.Embed( colour=discord.Colour.blue(), title=title,