Skip to content

Commit 13ab550

Browse files
committed
Refactor embed handling across multiple commands to utilize SafeEmbed for consistent user footer management. Introduce set_user_footer and set_user_footer_with_text methods for improved avatar handling and clarity in embed creation.
1 parent ea5b6a8 commit 13ab550

File tree

5 files changed

+84
-47
lines changed

5 files changed

+84
-47
lines changed

cogs/cross_pollinate_cog.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,7 @@ async def generate_cross_pollinate_embed(
450450
embed.set_image(url=f"attachment://{file_name}")
451451

452452
if not private:
453-
embed.set_footer(
454-
text=f"🐝 Cross-pollinated by {interaction.user.display_name}",
455-
icon_url=interaction.user.avatar.url
456-
if interaction.user.avatar
457-
else interaction.user.default_avatar.url,
458-
)
453+
embed.set_user_footer(interaction, "🐝 Cross-pollinated by")
459454

460455
return embed
461456

cogs/multi_pollinate_cog.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ async def generate_for_model(i, model):
263263
value=f"```{time_taken.total_seconds():.2f}s```",
264264
)
265265

266-
embeds[0].set_footer(text=f"Generated by {interaction.user}")
266+
embeds[0].set_user_footer(interaction, "Generated by")
267267

268268
if private:
269269
await interaction.followup.send(embeds=embeds, files=files, ephemeral=True)
@@ -331,4 +331,6 @@ async def multiimagine_command_error(
331331

332332
async def setup(bot) -> None:
333333
await bot.add_cog(Multi_pollinate(bot))
334-
print("Multi-Imagine cog loaded")
334+
discord_logger.log_bot_event(
335+
action="cog_setup", status="success", details={"cog": "Multi_pollinate"}
336+
)

cogs/random_cog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ async def random_image_command(
104104

105105
embed.set_image(url="attachment://image.png")
106106

107-
embed.set_footer(text=f"Generated by {interaction.user}")
107+
if not private:
108+
embed.set_user_footer(interaction, "🎲 Generated by")
108109

109110
if private:
110111
await interaction.followup.send(embed=embed, ephemeral=True)

main.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from utils.logger import discord_logger, logger
1010
from utils.fs import list_py_files
1111
from utils.models import fetch_and_log_models
12+
from utils.embed_utils import SafeEmbed
1213
import traceback
1314

1415
load_dotenv(override=True)
@@ -126,7 +127,7 @@ async def on_message(message) -> None:
126127
return
127128

128129
if bot.user in message.mentions and message.type is not discord.MessageType.reply:
129-
embed = discord.Embed(
130+
embed = SafeEmbed(
130131
description=f"Hello, I am the Pollinations.ai Bot. I am here to help you with your AI needs. **To Generate Images click </pollinate:{config.bot.commands['pollinate_id']}> or </multi-pollinate:{config.bot.commands['multi_pollinate_id']}>, to Cross-Pollinate Images click </cross-pollinate:{config.bot.commands['cross_pollinate_id']}>, or type `/help` for more commands**.",
131132
color=int(config.ui.colors.success, 16),
132133
)
@@ -212,7 +213,7 @@ async def on_command_error(ctx, error):
212213

213214
# Send error message to user
214215
await ctx.send(
215-
embed=discord.Embed(
216+
embed=SafeEmbed(
216217
title="Error", description=str(error), color=int(config.ui.colors.error, 16)
217218
),
218219
)
@@ -227,7 +228,7 @@ async def before_invoke(ctx) -> None:
227228
@bot.command()
228229
async def ping(ctx) -> None:
229230
try:
230-
embed = discord.Embed(title="Pong!", color=int(config.ui.colors.success, 16))
231+
embed = SafeEmbed(title="Pong!", color=int(config.ui.colors.success, 16))
231232
message = await ctx.send(embed=embed)
232233

233234
end: float = time.perf_counter()
@@ -256,10 +257,7 @@ async def ping(ctx) -> None:
256257
value=f"{hours} hours {minutes} minutes {seconds} seconds",
257258
inline=False,
258259
)
259-
embed.set_footer(
260-
text=f"Information requested by: {ctx.author.name}",
261-
icon_url=ctx.author.avatar.url,
262-
)
260+
embed.set_user_footer_with_text(ctx, f"Information requested by: {ctx.author.name}")
263261
embed.set_thumbnail(
264262
url="https://uploads.poxipage.com/7q5iw7dwl5jc3zdjaergjhpat27tws8bkr9fgy45_938843265627717703-webp"
265263
)
@@ -274,7 +272,7 @@ async def ping(ctx) -> None:
274272
context={"command": "ping"},
275273
)
276274
await ctx.send(
277-
embed=discord.Embed(
275+
embed=SafeEmbed(
278276
title="Error",
279277
description="An error occurred while processing the command.",
280278
color=int(config.ui.colors.error, 16),
@@ -290,7 +288,7 @@ async def help(ctx) -> None:
290288
except AttributeError:
291289
profilePicture = config.bot.avatar_url
292290

293-
embed = discord.Embed(
291+
embed = SafeEmbed(
294292
title="Pollinations.ai Bot Commands",
295293
description="Here is the list of the available commands:",
296294
color=int(config.ui.colors.success, 16),
@@ -300,27 +298,21 @@ async def help(ctx) -> None:
300298
for i in commands_.keys():
301299
embed.add_field(name=i, value=commands_[i], inline=False)
302300

303-
embed.set_footer(
304-
text=f"Information requested by: {ctx.author.name}",
305-
icon_url=ctx.author.avatar.url,
306-
)
301+
embed.set_user_footer_with_text(ctx, f"Information requested by: {ctx.author.name}")
307302

308303
await ctx.send(embed=embed)
309304

310305

311306
@bot.hybrid_command(name="invite", description="Invite the bot to your server")
312307
async def invite(ctx) -> None:
313-
embed = discord.Embed(
308+
embed = SafeEmbed(
314309
title="Invite the bot to your server",
315310
url=config.ui.bot_invite_url,
316311
description="Click the link above to invite the bot to your server",
317312
color=int(config.ui.colors.success, 16),
318313
)
319314

320-
embed.set_footer(
321-
text=f"Information requested by: {ctx.author.name}",
322-
icon_url=ctx.author.avatar.url,
323-
)
315+
embed.set_user_footer_with_text(ctx, f"Information requested by: {ctx.author.name}")
324316

325317
await ctx.send(embed=embed)
326318

@@ -333,7 +325,7 @@ async def about(ctx) -> None:
333325
except AttributeError:
334326
profilePicture = config.bot.avatar_url
335327

336-
embed = discord.Embed(
328+
embed = SafeEmbed(
337329
title="About Pollinations.ai Bot 🙌",
338330
url=config.ui.api_provider_url,
339331
description="I am the official Pollinations.ai Bot. I can generate AI Images from your prompts ✨.",
@@ -391,6 +383,8 @@ async def about(ctx) -> None:
391383
icon_url=config.ui.bot_creator_avatar,
392384
)
393385

386+
embed.set_user_footer_with_text(ctx, f"Information requested by: {ctx.author.name}")
387+
394388
await ctx.send(embed=embed)
395389

396390

@@ -402,7 +396,7 @@ async def models(ctx) -> None:
402396
except AttributeError:
403397
profilePicture = config.bot.avatar_url
404398

405-
embed = discord.Embed(
399+
embed = SafeEmbed(
406400
title="🤖 Available AI Models",
407401
description="Here are all the AI models currently available for image generation:",
408402
color=int(config.ui.colors.success, 16),
@@ -450,12 +444,7 @@ async def models(ctx) -> None:
450444
inline=False,
451445
)
452446

453-
embed.set_footer(
454-
text=f"Information requested by: {ctx.author.name} • Models last updated",
455-
icon_url=ctx.author.avatar.url
456-
if ctx.author.avatar
457-
else ctx.author.default_avatar.url,
458-
)
447+
embed.set_user_footer_with_text(ctx, f"Information requested by: {ctx.author.name} • Models last updated")
459448

460449
await ctx.send(embed=embed)
461450

utils/embed_utils.py

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,65 @@ def set_footer(
120120
super().set_footer(text=text, icon_url=icon_url)
121121
return self
122122

123+
def set_user_footer(
124+
self,
125+
interaction: discord.Interaction,
126+
prefix: str = "Generated by"
127+
) -> "SafeEmbed":
128+
"""
129+
Set footer with user information and proper avatar handling.
130+
131+
Args:
132+
interaction: Discord interaction object
133+
prefix: Text prefix before the username (default: "Generated by")
134+
135+
Example:
136+
embed.set_user_footer(interaction, "🐝 Cross-pollinated by")
137+
"""
138+
footer_text = f"{prefix} {interaction.user.display_name}"
139+
140+
# Handle avatar URL with proper fallback
141+
icon_url = (
142+
interaction.user.avatar.url
143+
if interaction.user.avatar
144+
else interaction.user.default_avatar.url
145+
)
146+
147+
return self.set_footer(text=footer_text, icon_url=icon_url)
148+
149+
def set_user_footer_with_text(
150+
self,
151+
interaction_or_ctx,
152+
text: str
153+
) -> "SafeEmbed":
154+
"""
155+
Set footer with custom text and user avatar.
156+
157+
Args:
158+
interaction_or_ctx: Discord interaction object or command context
159+
text: Custom footer text
160+
161+
Example:
162+
embed.set_user_footer_with_text(interaction, f"{interaction.user} used /{interaction.command.name}")
163+
embed.set_user_footer_with_text(ctx, f"Information requested by: {ctx.author.name}")
164+
"""
165+
# Handle both interaction and context objects
166+
if hasattr(interaction_or_ctx, 'user'):
167+
# It's an interaction
168+
user = interaction_or_ctx.user
169+
else:
170+
# It's a context
171+
user = interaction_or_ctx.author
172+
173+
# Handle avatar URL with proper fallback
174+
icon_url = (
175+
user.avatar.url
176+
if user.avatar
177+
else user.default_avatar.url
178+
)
179+
180+
return self.set_footer(text=text, icon_url=icon_url)
181+
123182
@_truncate_args(name=256)
124183
def set_author(
125184
self, *, name: str, url: Optional[str] = None, icon_url: Optional[str] = None
@@ -217,8 +276,8 @@ async def generate_pollinate_embed(
217276
else:
218277
embed.set_image(url=dic["url"])
219278

220-
embed.set_footer(text=f"Generated by {interaction.user}")
221-
279+
embed.set_user_footer(interaction, "Generated by")
280+
222281
return embed
223282

224283

@@ -249,16 +308,7 @@ async def generate_error_message(
249308
inline=False,
250309
)
251310

252-
try:
253-
embed.set_footer(
254-
text=f"{interaction.user} used /{interaction.command.name}",
255-
icon_url=interaction.user.avatar,
256-
)
257-
except Exception:
258-
embed.set_footer(
259-
text=f"{interaction.user} used /{interaction.command.name}",
260-
icon_url=interaction.user.default_avatar,
261-
)
311+
embed.set_user_footer_with_text(interaction, f"{interaction.user} used /{interaction.command.name}")
262312

263313
return embed
264314

0 commit comments

Comments
 (0)