Finish jpegify command

This commit is contained in:
Nexus 2024-02-07 16:18:09 +00:00
parent c9881c089f
commit 31aba5ce6e

View file

@ -1,6 +1,8 @@
import asyncio import asyncio
import io import io
import logging import logging
import typing
import PIL.Image import PIL.Image
import discord import discord
@ -58,7 +60,31 @@ class FFMeta(commands.Cog):
await ctx.respond(page) await ctx.respond(page)
@commands.slash_command() @commands.slash_command()
async def jpegify(self, ctx: discord.ApplicationContext, url: str = None, attachment: discord.Attachment = None): async def jpegify(
self,
ctx: discord.ApplicationContext,
url: str = None,
attachment: discord.Attachment = None,
quality: typing.Annotated[
int,
discord.Option(
int,
description="The quality of the resulting image from 1%-100%",
default=50,
min_value=1,
max_value=100
)
] = 50,
image_format: typing.Annotated[
str,
discord.Option(
str,
description="The format of the resulting image",
choices=["jpeg", "png", "webp", "avif"],
default="jpeg"
)
] = "jpeg"
):
"""Converts a given URL or attachment to a JPEG""" """Converts a given URL or attachment to a JPEG"""
if url is None: if url is None:
if attachment is None: if attachment is None:
@ -74,18 +100,14 @@ class FFMeta(commands.Cog):
return return
src.write(response.content) src.write(response.content)
paginator = commands.Paginator(prefix="```", suffix="```") try:
for line in stdout.splitlines(): dst = await asyncio.to_thread(self.jpegify_image, src, quality, image_format)
if stderr: except Exception as e:
paginator.add_line(f"[OUT] {line}"[:2000]) await ctx.respond(f"Failed to convert image: `{e}`.")
else: self.log.error("Failed to convert image %r: %r", url, e)
paginator.add_line(line[:2000]) return
else:
for line in stderr.splitlines(): await ctx.respond(file=discord.File(dst, filename=f"jpegified.{image_format}"))
paginator.add_line(f"[ERR] {line}"[:2000])
for page in paginator.pages:
await ctx.respond(page)
def setup(bot: commands.Bot): def setup(bot: commands.Bot):