From 46646f752d10a436570685a02eb182ea36a8bcf5 Mon Sep 17 00:00:00 2001 From: marksverdhei <> Date: Sat, 29 Apr 2023 10:50:09 +0200 Subject: [PATCH] Make all arguments optional --- src/cogs/the.py | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/src/cogs/the.py b/src/cogs/the.py index e46428e..97feb4c 100644 --- a/src/cogs/the.py +++ b/src/cogs/the.py @@ -1,3 +1,4 @@ +import traceback from typing import Any import discord from discord import app_commands @@ -6,6 +7,7 @@ from PIL import Image, ImageDraw, ImageFont import aiohttp import io + class The(commands.Cog): """ This cog lets you make a "the" barnacle boy laser eyes meme @@ -29,7 +31,9 @@ class The(commands.Cog): ---------- (io.BytesIO): data buffer """ - + if not url: + return None + async with aiohttp.ClientSession() as session: async with session.get(url) as resp: if resp.status != 200: @@ -56,9 +60,8 @@ class The(commands.Cog): draw.text((x + dx, y + dy), text, font=font, fill="black") draw.text((x, y), text, font=font, fill="white") - @app_commands.command() - async def the(self, interaction: discord.Interaction, caption: str, image_url: str, bottom_text: str = ""): + async def the(self, interaction: discord.Interaction, top_text: str = "", image_url: str = "", bottom_text: str = ""): """ Responds with a barnacle boy laser eyes THE meme with the given captions @@ -73,42 +76,28 @@ class The(commands.Cog): ---------- (discord.File): an image """ - await interaction.response.defer() try: - image_data = await self.fetch_image(image_url) bg_data = await self.fetch_image(self.background_url) - - if not image_data or not bg_data: - await interaction.followup.send("Invalid image URLs provided.") - return - - image = Image.open(image_data) background = Image.open(bg_data) + + image_data = await self.fetch_image(image_url) - fixed_size = (200, 200) # Set your desired maximum width and height - image.thumbnail(fixed_size, Image.ANTIALIAS) + if image_data: + image = Image.open(image_data).resize((200, 200), Image.ANTIALIAS) + background.paste(image, (self.x, self.y)) - background.paste(image, (self.x, self.y)) + draw = ImageDraw.Draw(background) + font = ImageFont.truetype("./src/assets/fonts/impact.ttf", 120) - if caption: - draw = ImageDraw.Draw(background) - font = ImageFont.truetype("./src/assets/fonts/impact.ttf", 120) - text = caption.upper() - draw.textsize(text, font=font) + if top_text: text_x, text_y = (210, -10) - self.outline_text(draw, text, font, text_x, text_y, thickness=5) - draw.text((text_x, text_y), text, font=font, fill="white") + self.outline_text(draw, top_text.upper(), font, text_x, text_y, thickness=5) if bottom_text: - draw = ImageDraw.Draw(background) - font = ImageFont.truetype("./src/assets/fonts/impact.ttf", 120) - text = bottom_text.upper() - draw.textsize(text, font=font) text_x, text_y = (150, 550) - self.outline_text(draw, text, font, text_x, text_y, thickness=5) - draw.text((text_x, text_y), text, font=font, fill="white") + self.outline_text(draw, bottom_text.upper(), font, text_x, text_y, thickness=5) with io.BytesIO() as output: background.save(output, format="PNG") @@ -116,7 +105,7 @@ class The(commands.Cog): await interaction.followup.send(file=discord.File(output, "result.png")) except Exception as e: - print(e) + traceback.print_exc() await interaction.followup.send("An error occurred while processing the images.")