From a23c28e5512c381f1d1604f108abccbce8bb8acc Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Thu, 3 Feb 2022 06:07:18 -0700 Subject: [PATCH] Migrate image, closes #97 --- jarvis/cogs/image.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/jarvis/cogs/image.py b/jarvis/cogs/image.py index 0b8a342..45fc0c8 100644 --- a/jarvis/cogs/image.py +++ b/jarvis/cogs/image.py @@ -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))