diff options
Diffstat (limited to 'bot/exts/fun/snakes')
| -rw-r--r-- | bot/exts/fun/snakes/_snakes_cog.py | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/bot/exts/fun/snakes/_snakes_cog.py b/bot/exts/fun/snakes/_snakes_cog.py index eca462c6..82496629 100644 --- a/bot/exts/fun/snakes/_snakes_cog.py +++ b/bot/exts/fun/snakes/_snakes_cog.py @@ -11,8 +11,8 @@ from functools import partial from io import BytesIO from typing import Any -import async_timeout from PIL import Image, ImageDraw, ImageFont +from aiohttp import ClientTimeout from discord import Colour, Embed, File, Member, Message, Reaction from discord.errors import HTTPException from discord.ext.commands import Cog, CommandError, Context, bot_has_permissions, group @@ -241,8 +241,11 @@ class Snakes(Cog): # Draw the text onto the final image draw = ImageDraw.Draw(full_image) for line in textwrap.wrap(description, 36): - draw.text([margin + 4, offset], line, font=CARD["font"]) - offset += CARD["font"].getsize(line)[1] + draw.text((margin + 4, offset), line, font=CARD["font"]) + + _left, top, _right, bottom = CARD["font"].getbbox(line) + # Height of the text + 4px spacing + offset += bottom - top + 4 # Get the image contents as a BufferIO object buffer = BytesIO() @@ -279,8 +282,7 @@ class Snakes(Cog): if params is None: params = {} - async with async_timeout.timeout(10): - async with self.bot.http_session.get(url, params=params) as response: + async with self.bot.http_session.get(url, params=params, timeout=ClientTimeout(total=10)) as response: return await response.json() def _get_random_long_message(self, messages: list[str], retries: int = 10) -> str: @@ -846,7 +848,7 @@ class Snakes(Cog): await self._validate_answer(ctx, quiz, answer, options) @snakes_group.command(name="name", aliases=("name_gen",)) - async def name_command(self, ctx: Context, *, name: str = None) -> None: + async def name_command(self, ctx: Context, *, name: str | None = None) -> None: """ Snakifies a username. @@ -987,23 +989,29 @@ class Snakes(Cog): """ # Get the snake data we need if not name: - name_obj = await self._get_snake_name() - name = name_obj["scientific"] - content = await self._get_snek(name) + for _ in range(3): + name_obj = await self._get_snake_name() + name = name_obj["scientific"] + content = await self._get_snek(name) + if len(content["image_list"]) > 0: + break elif isinstance(name, dict): content = name - else: content = await self._get_snek(name) + try: + image_url = content["image_list"][0] + except IndexError: + await ctx.send("No images found for this snake.") + return + # Make the card async with ctx.typing(): - stream = BytesIO() - async with async_timeout.timeout(10): - async with self.bot.http_session.get(content["image_list"][0]) as response: - stream.write(await response.read()) + async with self.bot.http_session.get(image_url, timeout=ClientTimeout(total=10)) as response: + stream.write(await response.read()) stream.seek(0) @@ -1033,7 +1041,7 @@ class Snakes(Cog): await ctx.send(embed=embed) @snakes_group.command(name="snakify") - async def snakify_command(self, ctx: Context, *, message: str = None) -> None: + async def snakify_command(self, ctx: Context, *, message: str | None = None) -> None: """ How would I talk if I were a snake? @@ -1068,7 +1076,7 @@ class Snakes(Cog): await ctx.send(embed=embed) @snakes_group.command(name="video", aliases=("get_video",)) - async def video_command(self, ctx: Context, *, search: str = None) -> None: + async def video_command(self, ctx: Context, *, search: str | None = None) -> None: """ Gets a YouTube video about snakes. |