Skip to content

Commit dd2c5e8

Browse files
committed
Add image size reduction functionality for beemoji generation. Update prompts for clarity and enhance error logging for server issues. Modify emoji name generation logic and improve user feedback in the response message.
1 parent 5a66812 commit dd2c5e8

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

cogs/beemoji_cog.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import re
88
import io
99
from urllib.parse import quote
10+
from PIL import Image
11+
import asyncio
1012

1113
from config import config
1214
from utils.embed_utils import SafeEmbed
@@ -27,6 +29,19 @@ def __init__(
2729
self.emoji2_name = emoji2_name
2830
self.generated_name = generated_name
2931

32+
async def reduce_image_size(
33+
self, image_data: bytes, width: int, height: int
34+
) -> bytes:
35+
"""Reduce the size of the image to the given width and height"""
36+
loop = asyncio.get_event_loop()
37+
image = await loop.run_in_executor(None, Image.open, io.BytesIO(image_data))
38+
image = await loop.run_in_executor(
39+
None, lambda: image.resize((width, height), Image.Resampling.LANCZOS)
40+
)
41+
buffer = io.BytesIO()
42+
await loop.run_in_executor(None, lambda: image.save(buffer, format="PNG"))
43+
return buffer.getvalue()
44+
3045
@discord.ui.button(
3146
label="Edit",
3247
style=discord.ButtonStyle.secondary,
@@ -140,20 +155,15 @@ async def add_to_server(
140155
)
141156

142157
image_data = await response.read()
158+
# reduce the size of the image to 80x80 pixels
159+
image_data = await self.reduce_image_size(image_data, 80, 80)
160+
161+
emoji_name = (
162+
interaction.message.embeds[0].fields[1].value.split("```")[1].strip()
163+
)
143164

144-
# Use the generated emoji name or create a fallback
145-
if self.generated_name:
165+
if not emoji_name:
146166
emoji_name = self.generated_name
147-
else:
148-
# Fallback: Create emoji name from the two input emojis
149-
emoji_name = f"beemoji_{self.emoji1_name}_{self.emoji2_name}".replace(
150-
" ", "_"
151-
)[:32]
152-
# Remove any non-alphanumeric characters except underscores
153-
emoji_name = re.sub(r"[^a-zA-Z0-9_]", "", emoji_name)
154-
# Ensure it doesn't start with a number
155-
if emoji_name and emoji_name[0].isdigit():
156-
emoji_name = f"beemoji_{emoji_name}"
157167

158168
# Add emoji to server
159169
emoji = await interaction.guild.create_custom_emoji(
@@ -162,6 +172,10 @@ async def add_to_server(
162172
reason=f"Beemoji created by {interaction.user}",
163173
)
164174

175+
# disable the add to server button
176+
button.disabled = True
177+
await interaction.message.edit(view=self)
178+
165179
discord_logger.log_bot_event(
166180
action="beemoji_add_to_server",
167181
status="success",
@@ -175,7 +189,7 @@ async def add_to_server(
175189
await interaction.followup.send(
176190
embed=SafeEmbed(
177191
title="✅ Emoji Added Successfully!",
178-
description=f"The beemoji has been added to the server as {emoji}",
192+
description=f"The beemoji has been added to the server as {emoji} `:{emoji_name}:`",
179193
color=int(config.ui.colors.success, 16),
180194
),
181195
ephemeral=True,
@@ -342,7 +356,7 @@ async def generate_emoji_name(emoji1_name: str, emoji2_name: str) -> str:
342356
"""Generate a creative name for the beemoji using text.pollinations.ai"""
343357
try:
344358
# Create a prompt for generating a creative emoji name
345-
prompt = f"Create a single creative emoji name (maximum 32 characters, no spaces, use underscores) that combines {emoji1_name} and {emoji2_name}. Only return the name, nothing else."
359+
prompt = f"Create a single creative emoji name (maximum 32 characters, no spaces, use underscores) that combines {emoji1_name} and {emoji2_name}. Only return the name, nothing else. It can be a single word, and it should not be too long."
346360

347361
# URL encode the prompt
348362
encoded_prompt = quote(prompt, safe="")
@@ -396,7 +410,7 @@ async def generate_beemoji(
396410
"""Generate a remixed emoji using the gptimage model"""
397411

398412
# Create a prompt for mixing the two emojis
399-
prompt = f"Create a small cute remix combining elements of {emoji1_name} and {emoji2_name} emojis, 80x80 pixels, clean simple style, keep the background completely transparent. dont add fake transparent background"
413+
prompt = f"Create a small remix combining elements of {emoji1_name} and {emoji2_name} emojis, 80x80 pixels, try to preserve the styles of the original emojis by making a blend of the two."
400414

401415
# Use the first emoji as the base image and the second as reference in the prompt
402416
encoded_image = quote(emoji1_url, safe="")
@@ -408,6 +422,7 @@ async def generate_beemoji(
408422
url += "&width=80&height=80"
409423
url += "&nologo=true"
410424
url += f"&referer={config.image_generation.referer}"
425+
url += "&transparent=true"
411426

412427
dic = {
413428
"prompt": prompt,
@@ -430,6 +445,14 @@ async def generate_beemoji(
430445
url, allow_redirects=True, headers=headers
431446
) as response:
432447
if response.status >= 500:
448+
discord_logger.log_error(
449+
error_type="server_error",
450+
error_message=f"Server error occurred while generating beemoji with status code: {response.status}\nPlease try again later",
451+
context={
452+
"url": url,
453+
"headers": headers,
454+
},
455+
)
433456
raise APIError(
434457
f"Server error occurred while generating beemoji with status code: {response.status}\nPlease try again later"
435458
)

0 commit comments

Comments
 (0)