Migrate image, closes #97

This commit is contained in:
Zeva Rose 2022-02-03 06:07:18 -07:00
parent c69c21e9d4
commit a23c28e551

View file

@ -5,21 +5,23 @@ from io import BytesIO
import aiohttp
import cv2
import numpy as np
from discord import File
from discord.ext import commands
from dis_snek import MessageContext, Scale, Snake, message_command
from dis_snek.models.discord.embed import EmbedField
from dis_snek.models.discord.file import File
from dis_snek.models.snek.command import cooldown
from dis_snek.models.snek.cooldowns import Buckets
from jarvis.utils import build_embed, convert_bytesize, unconvert_bytesize
from jarvis.utils.field import Field
class ImageCog(commands.Cog):
class ImageCog(Scale):
"""
Image processing functions for J.A.R.V.I.S.
May be categorized under util later
"""
def __init__(self, bot: commands.Bot):
def __init__(self, bot: Snake):
self.bot = bot
self._session = aiohttp.ClientSession()
self.tgt_match = re.compile(r"([0-9]*\.?[0-9]*?) ?([KMGTP]?B)", re.IGNORECASE)
@ -27,7 +29,7 @@ class ImageCog(commands.Cog):
def __del__(self):
self._session.close()
async def _resize(self, ctx: commands.Context, target: str, url: str = None) -> None:
async def _resize(self, ctx: MessageContext, target: str, url: str = None) -> None:
if not target:
await ctx.send("Missing target size, i.e. 200KB.")
return
@ -84,23 +86,23 @@ class ImageCog(commands.Cog):
bufio = BytesIO(file)
accuracy = (len(file) / tgt_size) * 100
fields = [
Field("Original Size", convert_bytesize(size), False),
Field("New Size", convert_bytesize(len(file)), False),
Field("Accuracy", f"{accuracy:.02f}%", False),
EmbedField("Original Size", convert_bytesize(size), False),
EmbedField("New Size", convert_bytesize(len(file)), False),
EmbedField("Accuracy", f"{accuracy:.02f}%", False),
]
embed = build_embed(title=filename, description="", fields=fields)
embed.set_image(url="attachment://resized.png")
await ctx.send(
embed=embed,
file=File(bufio, filename="resized.png"),
file=File(file=bufio, filename="resized.png"),
)
@commands.command(name="resize", help="Resize an image")
@commands.cooldown(1, 60, commands.BucketType.user)
async def _resize_pref(self, ctx: commands.Context, target: str, url: str = None) -> None:
@message_command(name="resize")
@cooldown(bucket=Buckets.USER, rate=1, interval=60)
async def _resize_pref(self, ctx: MessageContext, target: str, url: str = None) -> None:
await self._resize(ctx, target, url)
def setup(bot: commands.Bot) -> None:
def setup(bot: Snake) -> None:
"""Add ImageCog to J.A.R.V.I.S."""
bot.add_cog(ImageCog(bot))