diff options
Diffstat (limited to 'bot/exts/fun')
| -rw-r--r-- | bot/exts/fun/latex.py | 41 | ||||
| -rw-r--r-- | bot/exts/fun/uwu.py | 2 | 
2 files changed, 29 insertions, 14 deletions
| diff --git a/bot/exts/fun/latex.py b/bot/exts/fun/latex.py index 12f2d0b6..b7bd708e 100644 --- a/bot/exts/fun/latex.py +++ b/bot/exts/fun/latex.py @@ -9,7 +9,7 @@ from typing import BinaryIO  import discord  from PIL import Image -from aiohttp import web +from aiohttp import client_exceptions, web  from discord.ext import commands  from bot.bot import Bot @@ -72,6 +72,11 @@ class InvalidLatexError(Exception):          self.logs = logs +class LatexServerError(Exception): +    """Represents an error raised from Latex rendering server.""" + + +  class Latex(commands.Cog):      """Renders latex.""" @@ -81,8 +86,11 @@ class Latex(commands.Cog):      async def _generate_image(self, query: str, out_file: BinaryIO) -> None:          """Make an API request and save the generated image to cache."""          payload = {"code": query, "format": "png"} -        async with self.bot.http_session.post(LATEX_API_URL, data=payload, raise_for_status=True) as response: -            response_json = await response.json() +        try: +            async with self.bot.http_session.post(LATEX_API_URL, data=payload, raise_for_status=True) as response: +                response_json = await response.json() +        except client_exceptions.ClientResponseError: +            raise LatexServerError          if response_json["status"] != "success":              raise InvalidLatexError(logs=response_json.get("log"))          async with self.bot.http_session.get( @@ -105,6 +113,21 @@ class Latex(commands.Cog):          except web.HTTPClientError as e:              log.info("Error when uploading latex output to pastebin. %s", e) +    async def _prepare_error_embed(self, err: InvalidLatexError | LatexServerError | None) -> discord.Embed: +        title = "Server encountered an issue, please retry later." +        if isinstance(err, InvalidLatexError): +            title = "Failed to render input." + +        embed = discord.Embed(title=title) +        embed.description = "No logs available." +        logs = getattr(err, "logs", None) +        if logs: +            logs_paste_url = await self._upload_to_pastebin(logs) +            embed.description = "Couldn't upload logs." +            if logs_paste_url: +                embed.description = f"[View Logs]({logs_paste_url})" +        return embed +      @commands.command()      @commands.max_concurrency(1, commands.BucketType.guild, wait=True)      @whitelist_override(channels=LATEX_ALLOWED_CHANNNELS) @@ -120,16 +143,8 @@ class Latex(commands.Cog):                  try:                      with open(image_path, "wb") as out_file:                          await self._generate_image(TEMPLATE.substitute(text=query), out_file) -                except InvalidLatexError as err: -                    embed = discord.Embed(title="Failed to render input.") -                    if err.logs is None: -                        embed.description = "No logs available." -                    else: -                        logs_paste_url = await self._upload_to_pastebin(err.logs) -                        if logs_paste_url: -                            embed.description = f"[View Logs]({logs_paste_url})" -                        else: -                            embed.description = "Couldn't upload logs." +                except (InvalidLatexError, LatexServerError) as err: +                    embed = await self._prepare_error_embed(err)                      await ctx.send(embed=embed)                      image_path.unlink()                      return diff --git a/bot/exts/fun/uwu.py b/bot/exts/fun/uwu.py index 488c68f3..f548d24b 100644 --- a/bot/exts/fun/uwu.py +++ b/bot/exts/fun/uwu.py @@ -30,7 +30,7 @@ EMOJIS = [      "o.O",      "-.-",      ">w<", -    "σωσ",  # noqa: RUF001 +    "σωσ",      "òωó",      "ʘwʘ",      ":3", | 
