From 765815ec5dd0b81b8984ea494d25db059dfcb153 Mon Sep 17 00:00:00 2001 From: nottherealtar Date: Fri, 20 Dec 2024 20:52:26 +0200 Subject: [PATCH] Improve icon generation error handling and add user feedback --- howcracked/howcracked.py | 4 +++ howcracked/icon_generator.py | 51 +++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/howcracked/howcracked.py b/howcracked/howcracked.py index c4f6395..5534cdf 100644 --- a/howcracked/howcracked.py +++ b/howcracked/howcracked.py @@ -1,3 +1,4 @@ +import os from redbot.core import commands, Config from discord import Embed, User, File import random @@ -167,6 +168,9 @@ async def howcracked(self, ctx, user: User = None): # Generate or retrieve the icon icon_path = generate_icon(power_level) + if not icon_path: + await ctx.send("Failed to generate icon.") + return # Build the embed embed = Embed(title=f"How Cracked is {target_user.name}?", color=0x00ff00) diff --git a/howcracked/icon_generator.py b/howcracked/icon_generator.py index d011f9b..7aecb73 100644 --- a/howcracked/icon_generator.py +++ b/howcracked/icon_generator.py @@ -8,32 +8,41 @@ def generate_icon(level): """ Generate an RPG-style icon for the given level. """ - # Define icon size and colors - size = (100, 100) - background_color = (255, 255, 255) - text_color = (0, 0, 0) + try: + # Define icon size and colors + size = (100, 100) + background_color = (255, 255, 255) + text_color = (0, 0, 0) - # Create a blank image - image = Image.new('RGB', size, background_color) - draw = ImageDraw.Draw(image) + # Create a blank image + image = Image.new('RGB', size, background_color) + draw = ImageDraw.Draw(image) - # Load a font - font = ImageFont.load_default() + # Load a font + try: + font = ImageFont.load_default() + except IOError: + raise Exception("Font file not found.") - # Draw text - text = level[:2] # Use first 2 letters of the level for simplicity - text_width, text_height = draw.textsize(text, font=font) - text_position = ((size[0] - text_width) / 2, (size[1] - text_height) / 2) - draw.text(text_position, text, fill=text_color, font=font) + # Draw text + text = level[:2] # Use first 2 letters of the level for simplicity + text_bbox = draw.textbbox((0, 0), text, font=font) + text_width = text_bbox[2] - text_bbox[0] + text_height = text_bbox[3] - text_bbox[1] + text_position = ((size[0] - text_width) / 2, (size[1] - text_height) / 2) + draw.text(text_position, text, fill=text_color, font=font) - # Save the image to a temporary file - temp_dir = "icons_cache" - os.makedirs(temp_dir, exist_ok=True) - timestamp = datetime.utcnow().strftime("%Y%m%d%H%M%S%f") - file_path = os.path.join(temp_dir, f"{level}_{timestamp}.png") - image.save(file_path) + # Save the image to a temporary file + temp_dir = "icons_cache" + os.makedirs(temp_dir, exist_ok=True) + timestamp = datetime.utcnow().strftime("%Y%m%d%H%M%S%f") + file_path = os.path.join(temp_dir, f"{level}_{timestamp}.png") + image.save(file_path) - return file_path + return file_path + except Exception as e: + print(f"Error generating icon for level {level}: {e}") + return None def clear_icon_cache(): """