Skip to content

Commit

Permalink
Improve icon generation error handling and add user feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nottherealtar committed Dec 20, 2024
1 parent 3de920b commit 765815e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
4 changes: 4 additions & 0 deletions howcracked/howcracked.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from redbot.core import commands, Config
from discord import Embed, User, File
import random
Expand Down Expand Up @@ -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)
Expand Down
51 changes: 30 additions & 21 deletions howcracked/icon_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down

0 comments on commit 765815e

Please sign in to comment.