add img tot gif command
This commit is contained in:
parent
0ea4d4264e
commit
dd485c1551
1 changed files with 35 additions and 0 deletions
|
@ -12,11 +12,13 @@ import shutil
|
|||
import sys
|
||||
import tempfile
|
||||
import typing
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
|
||||
import discord
|
||||
import httpx
|
||||
import PIL.Image
|
||||
from PIL import Image
|
||||
from discord.ext import commands
|
||||
|
||||
from conf import VERSION
|
||||
|
@ -324,6 +326,39 @@ class FFMeta(commands.Cog):
|
|||
for page in paginator.pages:
|
||||
await ctx.respond(page, ephemeral=True)
|
||||
|
||||
@commands.message_command(name="Convert Image to GIF")
|
||||
async def convert_image_to_gif(self, ctx: discord.ApplicationContext, message: discord.Message):
|
||||
await ctx.defer()
|
||||
for attachment in message.attachments:
|
||||
if attachment.content_type.startswith("image/"):
|
||||
break
|
||||
else:
|
||||
return await ctx.respond("No image found.")
|
||||
image = attachment
|
||||
image: discord.Attachment
|
||||
with tempfile.TemporaryFile("wb+") as f:
|
||||
await image.save(f)
|
||||
f.seek(0)
|
||||
img = await self.bot.loop.run_in_executor(None, Image.open, f)
|
||||
if img.format.upper() not in ("PNG", "JPEG", "WEBP", "HEIF", "BMP", "TIFF"):
|
||||
return await ctx.respond("Image must be PNG, JPEG, WEBP, or HEIF.")
|
||||
|
||||
with tempfile.TemporaryFile("wb+") as f2:
|
||||
caller = partial(img.save, f2, format="GIF")
|
||||
await self.bot.loop.run_in_executor(None, caller)
|
||||
f2.seek(0)
|
||||
try:
|
||||
await ctx.respond(file=discord.File(f2, filename="image.gif"))
|
||||
except discord.HTTPException as e:
|
||||
if e.code == 40005:
|
||||
return await ctx.respond("Image is too large.")
|
||||
return await ctx.respond(f"Failed to upload: `{e}`")
|
||||
try:
|
||||
f2.seek(0)
|
||||
await ctx.user.send(file=discord.File(f2, filename="image.gif"))
|
||||
except discord.Forbidden:
|
||||
return await ctx.respond("Unable to mirror to your DM - am I blocked?", ephemeral=True)
|
||||
|
||||
|
||||
def setup(bot: commands.Bot):
|
||||
bot.add_cog(FFMeta(bot))
|
||||
|
|
Loading…
Reference in a new issue