refactor(img2gif): now works in dms and priv channels

This commit is contained in:
neoarz
2025-10-17 23:13:35 -04:00
parent 352067d51d
commit 025df159f7

View File

@@ -13,6 +13,23 @@ except Exception:
pass pass
async def send_error_message(context, description: str):
embed = discord.Embed(
title="Error",
description=description,
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
await interaction.response.send_message(embed=embed, ephemeral=True)
else:
await interaction.followup.send(embed=embed, ephemeral=True)
else:
await context.send(embed=embed, ephemeral=True)
def img2gif_command(): def img2gif_command():
@commands.hybrid_command( @commands.hybrid_command(
name="img2gif", name="img2gif",
@@ -29,36 +46,20 @@ def img2gif_command():
if resolved_attachment is None and context.message and context.message.attachments: if resolved_attachment is None and context.message and context.message.attachments:
resolved_attachment = context.message.attachments[0] resolved_attachment = context.message.attachments[0]
if resolved_attachment is None or not resolved_attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tiff", ".heic", ".heif")): if resolved_attachment is None or not resolved_attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tiff", ".heic", ".heif")):
embed = discord.Embed( await send_error_message(context, "Provide or reply to an image (png/jpg/jpeg/webp/bmp/tiff/heic/heif).")
title="Error", return
description="Provide or reply to an image (png/jpg/jpeg/webp/bmp/tiff/heic/heif).",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
interaction = getattr(context, "interaction", None) interaction = getattr(context, "interaction", None)
if interaction is not None: if interaction is not None:
if not interaction.response.is_done(): if not interaction.response.is_done():
await interaction.response.send_message(embed=embed, ephemeral=True) await interaction.response.defer(ephemeral=False)
else: else:
await interaction.followup.send(embed=embed, ephemeral=True)
else:
await context.send(embed=embed, ephemeral=True)
return
processing_embed = discord.Embed( processing_embed = discord.Embed(
title="Image to GIF (Processing)", title="Image to GIF (Processing)",
description="<a:mariospin:1423677027013103709> Converting image...", description="<a:mariospin:1423677027013103709> Converting image...",
color=0x7289DA, color=0x7289DA,
) )
processing_embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp") processing_embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
await interaction.response.send_message(embed=processing_embed, ephemeral=True)
else:
await interaction.followup.send(embed=processing_embed, ephemeral=True)
else:
processing_msg = await context.send(embed=processing_embed) processing_msg = await context.send(embed=processing_embed)
tmp_dir = tempfile.mkdtemp() tmp_dir = tempfile.mkdtemp()
@@ -93,47 +94,27 @@ def img2gif_command():
else: else:
raise raise
original_ext = os.path.splitext(resolved_attachment.filename)[1].lstrip('.').upper() or "IMAGE"
embed = discord.Embed(
title="Image to GIF",
description=f"Converted {original_ext} to GIF.",
color=0x7289DA,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_footer(text=f"Requested by {context.author.name}", icon_url=context.author.display_avatar.url)
with open(out_path, "rb") as f: with open(out_path, "rb") as f:
file = discord.File(f, filename=os.path.basename(out_path)) file = discord.File(f, filename=os.path.basename(out_path))
if interaction is not None: if interaction is not None:
await context.channel.send(embed=embed) await interaction.followup.send(file=file)
await context.channel.send(file=file)
try:
await interaction.delete_original_response()
except:
pass
else: else:
await processing_msg.delete() await processing_msg.delete()
await context.channel.send(embed=embed) await context.send(file=file)
await context.channel.send(file=file)
except Exception as e: except Exception as e:
embed = discord.Embed( if interaction is not None:
await interaction.followup.send(embed=discord.Embed(
title="Error", title="Error",
description=f"Failed to convert image: {e}", description=f"Failed to convert image: {e}",
color=0xE02B2B, color=0xE02B2B,
) ).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"), ephemeral=True)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
if interaction is not None:
try:
await interaction.delete_original_response()
except:
pass
await interaction.followup.send(embed=embed, ephemeral=True)
else: else:
try:
await processing_msg.delete() await processing_msg.delete()
except: await context.send(embed=discord.Embed(
pass title="Error",
await context.send(embed=embed, ephemeral=True) description=f"Failed to convert image: {e}",
color=0xE02B2B,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"), ephemeral=True)
finally: finally:
try: try:
for f in os.listdir(tmp_dir): for f in os.listdir(tmp_dir):