Skip to content

Commit ea5b6a8

Browse files
committed
Add the edit feature to the random_image command too
1 parent e9b4e7c commit ea5b6a8

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

cogs/cross_pollinate_cog.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ async def on_submit(self, interaction: discord.Interaction):
3535
try:
3636
# Validate the prompt
3737
validate_prompt(self.edit_prompt.value)
38-
38+
3939
await interaction.response.defer(thinking=True)
40-
40+
4141
start = datetime.datetime.now()
4242
discord_logger.log_image_generation(
4343
action="edit_start",
@@ -52,9 +52,9 @@ async def on_submit(self, interaction: discord.Interaction):
5252
dic, edited_image = await generate_cross_pollinate(
5353
prompt=self.edit_prompt.value,
5454
image_url=self.original_image_url,
55-
nologo=config.image_generation.defaults.nologo
55+
nologo=config.image_generation.defaults.nologo,
5656
)
57-
57+
5858
time_taken = (datetime.datetime.now() - start).total_seconds()
5959
discord_logger.log_image_generation(
6060
action="edit_complete",
@@ -72,7 +72,13 @@ async def on_submit(self, interaction: discord.Interaction):
7272

7373
time_taken_delta = datetime.datetime.now() - start
7474
embed = await generate_cross_pollinate_embed(
75-
interaction, False, dic, time_taken_delta, self.edit_prompt.value, self.original_image_url, image_file.filename
75+
interaction,
76+
False,
77+
dic,
78+
time_taken_delta,
79+
self.edit_prompt.value,
80+
self.original_image_url,
81+
image_file.filename,
7682
)
7783

7884
# Send the edited image with cross-pollinate buttons
@@ -153,7 +159,10 @@ async def edit(self, interaction: discord.Interaction, button: discord.ui.Button
153159
return
154160

155161
# Get the original image URL from the message embed
156-
if not interaction.message.embeds or not interaction.message.embeds[0].image:
162+
if (
163+
not interaction.message.embeds
164+
or not interaction.message.embeds[0].image
165+
):
157166
await interaction.response.send_message(
158167
embed=SafeEmbed(
159168
title="🎨 No Image Found",
@@ -163,27 +172,27 @@ async def edit(self, interaction: discord.Interaction, button: discord.ui.Button
163172
ephemeral=True,
164173
)
165174
return
166-
175+
167176
original_image_url = interaction.message.embeds[0].image.url
168-
177+
169178
# Get the original prompt based on the embed fields
170179
interaction_data: dict = interaction.message.embeds[0].to_dict()
171-
180+
172181
# Check if it's a cross-pollinate embed or regular pollinate embed
173182
prompt_field = None
174183
for field in interaction_data.get("fields", []):
175184
if field["name"] in ["Cross-Pollinate Prompt 🐝", "Prompt"]:
176185
prompt_field = field
177186
break
178-
187+
179188
original_prompt: str = (
180189
prompt_field["value"][3:-3] if prompt_field else "Unknown prompt"
181190
)
182-
191+
183192
# Show the edit modal
184193
modal = EditImageModal(original_image_url, original_prompt)
185194
await interaction.response.send_modal(modal)
186-
195+
187196
except Exception as e:
188197
discord_logger.log_error(
189198
error_type="edit_button_error",
@@ -552,7 +561,13 @@ async def cross_pollinate_command(
552561

553562
time_taken_delta: datetime.timedelta = datetime.datetime.now() - start
554563
embed: SafeEmbed = await generate_cross_pollinate_embed(
555-
interaction, private, dic, time_taken_delta, prompt, image_url, image_file.filename
564+
interaction,
565+
private,
566+
dic,
567+
time_taken_delta,
568+
prompt,
569+
image_url,
570+
image_file.filename,
556571
)
557572

558573
if private:

cogs/imagine_cog.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ async def edit(self, interaction: discord.Interaction, button: discord.ui.Button
150150
return
151151

152152
# Get the original image URL from the message embed
153-
if not interaction.message.embeds or not interaction.message.embeds[0].image:
153+
if (
154+
not interaction.message.embeds
155+
or not interaction.message.embeds[0].image
156+
):
154157
await interaction.response.send_message(
155158
embed=SafeEmbed(
156159
title="🎨 No Image Found",
@@ -160,17 +163,17 @@ async def edit(self, interaction: discord.Interaction, button: discord.ui.Button
160163
ephemeral=True,
161164
)
162165
return
163-
166+
164167
original_image_url = interaction.message.embeds[0].image.url
165-
168+
166169
# Get the original prompt
167170
interaction_data: dict = interaction.message.embeds[0].to_dict()
168171
original_prompt: str = interaction_data["fields"][0]["value"][3:-3]
169-
172+
170173
# Show the edit modal
171174
modal = EditImageModal(original_image_url, original_prompt)
172175
await interaction.response.send_modal(modal)
173-
176+
174177
except Exception as e:
175178
discord_logger.log_error(
176179
error_type="edit_button_error",

cogs/multi_pollinate_cog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ async def generate_for_model(i, model):
259259

260260
embeds[0].add_field(name="Prompt", value=f"```{prompt}```", inline=False)
261261
embeds[0].add_field(
262-
name="Total Processing Time", value=f"```{time_taken.total_seconds():.2f}s```"
262+
name="Total Processing Time",
263+
value=f"```{time_taken.total_seconds():.2f}s```",
263264
)
264265

265266
embeds[0].set_footer(text=f"Generated by {interaction.user}")

cogs/random_cog.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@
77
from utils.image_gen_utils import generate_image, validate_dimensions
88
from utils.embed_utils import generate_error_message, SafeEmbed
99
from utils.error_handler import send_error_embed
10+
from utils.logger import discord_logger
1011
from exceptions import DimensionTooSmallError, APIError
1112

13+
# Import cross-pollinate button view for edit functionality
14+
from cogs.cross_pollinate_cog import CrossPollinateButtonView
15+
1216

1317
class RandomImage(commands.Cog):
1418
def __init__(self, bot) -> None:
1519
self.bot = bot
1620
self.command_config = config.commands["random"]
1721

22+
async def cog_load(self) -> None:
23+
await self.bot.wait_until_ready()
24+
self.bot.add_view(CrossPollinateButtonView())
25+
discord_logger.log_bot_event(
26+
action="cog_load", status="success", details={"cog": "RandomImage"}
27+
)
28+
1829
@app_commands.command(name="random", description="Generate Random AI Images")
1930
@app_commands.choices(
2031
model=[
@@ -81,7 +92,6 @@ async def random_image_command(
8192
else "",
8293
timestamp=datetime.datetime.now(datetime.timezone.utc),
8394
url=dic["url"],
84-
color=int(config.ui.colors.success, 16),
8595
)
8696

8797
embed.add_field(name="Seed", value=f"```{dic['seed']}```", inline=True)
@@ -100,7 +110,9 @@ async def random_image_command(
100110
await interaction.followup.send(embed=embed, ephemeral=True)
101111
return
102112
else:
103-
await interaction.followup.send(embed=embed, file=image_file)
113+
# Use CrossPollinateButtonView for public images to get edit, delete, and bookmark functionality
114+
view = CrossPollinateButtonView()
115+
await interaction.followup.send(embed=embed, view=view, file=image_file)
104116

105117
@random_image_command.error
106118
async def random_image_command_error(
@@ -140,4 +152,6 @@ async def random_image_command_error(
140152

141153
async def setup(bot) -> None:
142154
await bot.add_cog(RandomImage(bot))
143-
print("Random Image cog loaded")
155+
discord_logger.log_bot_event(
156+
action="cog_setup", status="success", details={"cog": "RandomImage"}
157+
)

0 commit comments

Comments
 (0)