mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 11:40:12 +01:00
chore: ruff formatting
This commit is contained in:
@@ -6,8 +6,10 @@ from PIL import Image
|
||||
import subprocess
|
||||
import shutil
|
||||
from typing import Optional
|
||||
|
||||
try:
|
||||
import pillow_heif
|
||||
|
||||
pillow_heif.register_heif_opener()
|
||||
except Exception:
|
||||
pass
|
||||
@@ -20,7 +22,7 @@ async def send_error_message(context, description: str):
|
||||
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():
|
||||
@@ -30,6 +32,7 @@ async def send_error_message(context, description: str):
|
||||
else:
|
||||
await context.send(embed=embed, ephemeral=True)
|
||||
|
||||
|
||||
def img2gif_command():
|
||||
@commands.hybrid_command(
|
||||
name="img2gif",
|
||||
@@ -39,14 +42,30 @@ def img2gif_command():
|
||||
async def img2gif(self, context, attachment: Optional[discord.Attachment] = None):
|
||||
resolved_attachment = attachment
|
||||
if resolved_attachment is None:
|
||||
if context.message and context.message.reference and context.message.reference.resolved:
|
||||
if (
|
||||
context.message
|
||||
and context.message.reference
|
||||
and context.message.reference.resolved
|
||||
):
|
||||
ref_msg = context.message.reference.resolved
|
||||
if isinstance(ref_msg, discord.Message) and ref_msg.attachments:
|
||||
resolved_attachment = ref_msg.attachments[0]
|
||||
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]
|
||||
if resolved_attachment is None or not resolved_attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tiff", ".heic", ".heif")):
|
||||
await send_error_message(context, "Provide or reply to an image (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")
|
||||
)
|
||||
):
|
||||
await send_error_message(
|
||||
context,
|
||||
"Provide or reply to an image (png/jpg/jpeg/webp/bmp/tiff/heic/heif).",
|
||||
)
|
||||
return
|
||||
|
||||
interaction = getattr(context, "interaction", None)
|
||||
@@ -59,12 +78,16 @@ def img2gif_command():
|
||||
description="<a:mariospin:1423677027013103709> Converting image...",
|
||||
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"
|
||||
)
|
||||
processing_msg = await context.send(embed=processing_embed)
|
||||
|
||||
tmp_dir = tempfile.mkdtemp()
|
||||
src_path = os.path.join(tmp_dir, resolved_attachment.filename)
|
||||
out_path = os.path.join(tmp_dir, os.path.splitext(resolved_attachment.filename)[0] + ".gif")
|
||||
out_path = os.path.join(
|
||||
tmp_dir, os.path.splitext(resolved_attachment.filename)[0] + ".gif"
|
||||
)
|
||||
|
||||
try:
|
||||
await resolved_attachment.save(src_path)
|
||||
@@ -75,20 +98,42 @@ def img2gif_command():
|
||||
img = img.convert("RGBA")
|
||||
duration_ms = 100
|
||||
loop = 0
|
||||
img.save(out_path, format="GIF", save_all=True, optimize=True, duration=duration_ms, loop=loop)
|
||||
img.save(
|
||||
out_path,
|
||||
format="GIF",
|
||||
save_all=True,
|
||||
optimize=True,
|
||||
duration=duration_ms,
|
||||
loop=loop,
|
||||
)
|
||||
except Exception:
|
||||
if resolved_attachment.filename.lower().endswith((".heic", ".heif")) and shutil.which("ffmpeg"):
|
||||
png_path = os.path.join(tmp_dir, os.path.splitext(resolved_attachment.filename)[0] + ".png")
|
||||
if resolved_attachment.filename.lower().endswith(
|
||||
(".heic", ".heif")
|
||||
) and shutil.which("ffmpeg"):
|
||||
png_path = os.path.join(
|
||||
tmp_dir,
|
||||
os.path.splitext(resolved_attachment.filename)[0] + ".png",
|
||||
)
|
||||
try:
|
||||
subprocess.run([
|
||||
"ffmpeg", "-y", "-i", src_path, png_path
|
||||
], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
subprocess.run(
|
||||
["ffmpeg", "-y", "-i", src_path, png_path],
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
with Image.open(png_path) as img:
|
||||
if img.mode not in ("RGB", "RGBA"):
|
||||
img = img.convert("RGBA")
|
||||
duration_ms = 100
|
||||
loop = 0
|
||||
img.save(out_path, format="GIF", save_all=True, optimize=True, duration=duration_ms, loop=loop)
|
||||
img.save(
|
||||
out_path,
|
||||
format="GIF",
|
||||
save_all=True,
|
||||
optimize=True,
|
||||
duration=duration_ms,
|
||||
loop=loop,
|
||||
)
|
||||
except Exception as conv_err:
|
||||
raise conv_err
|
||||
else:
|
||||
@@ -103,18 +148,30 @@ def img2gif_command():
|
||||
await context.send(file=file)
|
||||
except Exception as e:
|
||||
if interaction is not None:
|
||||
await interaction.followup.send(embed=discord.Embed(
|
||||
title="Error",
|
||||
description=f"Failed to convert image: {e}",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"), ephemeral=True)
|
||||
await interaction.followup.send(
|
||||
embed=discord.Embed(
|
||||
title="Error",
|
||||
description=f"Failed to convert image: {e}",
|
||||
color=0xE02B2B,
|
||||
).set_author(
|
||||
name="Media",
|
||||
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
|
||||
),
|
||||
ephemeral=True,
|
||||
)
|
||||
else:
|
||||
await processing_msg.delete()
|
||||
await context.send(embed=discord.Embed(
|
||||
title="Error",
|
||||
description=f"Failed to convert image: {e}",
|
||||
color=0xE02B2B,
|
||||
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"), ephemeral=True)
|
||||
await context.send(
|
||||
embed=discord.Embed(
|
||||
title="Error",
|
||||
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:
|
||||
try:
|
||||
for f in os.listdir(tmp_dir):
|
||||
@@ -127,5 +184,3 @@ def img2gif_command():
|
||||
pass
|
||||
|
||||
return img2gif
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user