From bc94cd716b9370f18857d99dfe815ea4a22f9c33 Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Thu, 3 Feb 2022 21:16:04 -0700 Subject: [PATCH] Migrate dev, closes #116 --- jarvis/cogs/dev.py | 200 ++++++++++++++++++++------------------------- 1 file changed, 90 insertions(+), 110 deletions(-) diff --git a/jarvis/cogs/dev.py b/jarvis/cogs/dev.py index 7df8699..2997901 100644 --- a/jarvis/cogs/dev.py +++ b/jarvis/cogs/dev.py @@ -8,12 +8,18 @@ from typing import Any, Union import ulid as ulidpy from bson import ObjectId -from discord.ext import commands -from discord_slash import SlashContext, cog_ext -from discord_slash.utils.manage_commands import create_choice, create_option +from dis_snek import InteractionContext, Scale, Snake +from dis_snek.models.discord.embed import EmbedField +from dis_snek.models.snek.application_commands import ( + OptionTypes, + SlashCommandChoice, + slash_command, + slash_option, +) +from dis_snek.models.snek.command import cooldown +from dis_snek.models.snek.cooldowns import Buckets from jarvis.utils import build_embed, convert_bytesize -from jarvis.utils.field import Field supported_hashes = {x for x in hashlib.algorithms_guaranteed if "shake" not in x} @@ -55,33 +61,25 @@ def hash_obj(hash: Any, data: Union[str, bytes], text: bool = True) -> str: return hash.hexdigest() -class DevCog(commands.Cog): +class DevCog(Scale): """J.A.R.V.I.S. Developer Cog.""" - def __init__(self, bot: commands.Bot): - self.bot = bot - - @cog_ext.cog_slash( - name="hash", - description="Hash some data", - options=[ - create_option( - name="method", - description="Hash method", - option_type=3, - required=True, - choices=[create_choice(name=x, value=x) for x in supported_hashes], - ), - create_option( - name="data", - description="Data to hash", - option_type=3, - required=True, - ), - ], + @slash_command(name="hash", description="Hash some data") + @slash_option( + name="method", + description="Hash method", + option_type=OptionTypes.STRING, + required=True, + choices=[SlashCommandChoice(name=x, value=x) for x in supported_hashes], ) - @commands.cooldown(1, 2, commands.BucketType.user) - async def _hash(self, ctx: SlashContext, method: str, data: str) -> None: + @slash_option( + name="data", + description="Data to hash", + option_type=OptionTypes.STRING, + required=True, + ) + @cooldown(bucket=Buckets.USER, rate=1, interval=2) + async def _hash(self, ctx: InteractionContext, method: str, data: str) -> None: if not data: await ctx.send( "No data to hash", @@ -96,33 +94,28 @@ class DevCog(commands.Cog): title = data if text else ctx.message.attachments[0].filename description = "Hashed using " + method fields = [ - Field("Data Size", data_size, False), - Field("Hash", f"`{hex}`", False), + EmbedField("Data Size", data_size, False), + EmbedField("Hash", f"`{hex}`", False), ] embed = build_embed(title=title, description=description, fields=fields) await ctx.send(embed=embed) - @cog_ext.cog_slash( - name="uuid", - description="Generate a UUID", - options=[ - create_option( - name="version", - description="UUID version", - option_type=3, - required=True, - choices=[create_choice(name=x, value=x) for x in ["3", "4", "5"]], - ), - create_option( - name="data", - description="Data for UUID version 3,5", - option_type=3, - required=False, - ), - ], + @slash_command(name="uuid", description="Generate a UUID") + @slash_option( + name="version", + description="UUID version", + option_type=OptionTypes.STRING, + required=True, + choices=[SlashCommandChoice(name=x, value=x) for x in ["3", "4", "5"]], ) - async def _uuid(self, ctx: SlashContext, version: str, data: str = None) -> None: + @slash_option( + name="data", + description="Data for UUID version 3,5", + option_type=OptionTypes.STRING, + required=False, + ) + async def _uuid(self, ctx: InteractionContext, version: str, data: str = None) -> None: version = int(version) if version in [3, 5] and not data: await ctx.send(f"UUID{version} requires data.", hidden=True) @@ -141,40 +134,40 @@ class DevCog(commands.Cog): to_send = UUID_GET[version](uuidpy.NAMESPACE_DNS, data) await ctx.send(f"UUID{version}: `{to_send}`") - @cog_ext.cog_slash( + @slash_command( name="objectid", description="Generate an ObjectID", ) - @commands.cooldown(1, 2, commands.BucketType.user) - async def _objectid(self, ctx: SlashContext) -> None: + @cooldown(bucket=Buckets.USER, rate=1, interval=2) + async def _objectid(self, ctx: InteractionContext) -> None: await ctx.send(f"ObjectId: `{str(ObjectId())}`") - @cog_ext.cog_slash( + @slash_command( name="ulid", description="Generate a ULID", ) - @commands.cooldown(1, 2, commands.BucketType.user) - async def _ulid(self, ctx: SlashContext) -> None: + @cooldown(bucket=Buckets.USER, rate=1, interval=2) + async def _ulid(self, ctx: InteractionContext) -> None: await ctx.send(f"ULID: `{ulidpy.new().str}`") - @cog_ext.cog_slash( + @slash_command( name="uuid2ulid", description="Convert a UUID to a ULID", ) - @commands.cooldown(1, 2, commands.BucketType.user) - async def _uuid2ulid(self, ctx: SlashContext, uuid: str) -> None: + @cooldown(bucket=Buckets.USER, rate=1, interval=2) + async def _uuid2ulid(self, ctx: InteractionContext, uuid: str) -> None: if UUID_VERIFY.match(uuid): u = ulidpy.parse(uuid) await ctx.send(f"ULID: `{u.str}`") else: await ctx.send("Invalid UUID") - @cog_ext.cog_slash( + @slash_command( name="ulid2uuid", description="Convert a ULID to a UUID", ) - @commands.cooldown(1, 2, commands.BucketType.user) - async def _ulid2uuid(self, ctx: SlashContext, ulid: str) -> None: + @cooldown(bucket=Buckets.USER, rate=1, interval=2) + async def _ulid2uuid(self, ctx: InteractionContext, ulid: str) -> None: if ULID_VERIFY.match(ulid): ulid = ulidpy.parse(ulid) await ctx.send(f"UUID: `{ulid.uuid}`") @@ -183,56 +176,46 @@ class DevCog(commands.Cog): base64_methods = ["b64", "b16", "b32", "a85", "b85"] - @cog_ext.cog_slash( - name="encode", - description="Encode some data", - options=[ - create_option( - name="method", - description="Encode method", - option_type=3, - required=True, - choices=[create_choice(name=x, value=x) for x in base64_methods], - ), - create_option( - name="data", - description="Data to encode", - option_type=3, - required=True, - ), - ], + @slash_command(name="encode", description="Encode some data") + @slash_option( + name="method", + description="Encode method", + option_type=OptionTypes.STRING, + required=True, + choices=[SlashCommandChoice(name=x, value=x) for x in base64_methods], ) - async def _encode(self, ctx: SlashContext, method: str, data: str) -> None: + @slash_option( + name="data", + description="Data to encode", + option_type=OptionTypes.STRING, + required=True, + ) + async def _encode(self, ctx: InteractionContext, method: str, data: str) -> None: mstr = method method = getattr(base64, method + "encode") encoded = method(data.encode("UTF-8")).decode("UTF-8") fields = [ - Field(name="Plaintext", value=f"`{data}`", inline=False), - Field(name=mstr, value=f"`{encoded}`", inline=False), + EmbedField(name="Plaintext", value=f"`{data}`", inline=False), + EmbedField(name=mstr, value=f"`{encoded}`", inline=False), ] embed = build_embed(title="Decoded Data", description="", fields=fields) await ctx.send(embed=embed) - @cog_ext.cog_slash( - name="decode", - description="Decode some data", - options=[ - create_option( - name="method", - description="Decode method", - option_type=3, - required=True, - choices=[create_choice(name=x, value=x) for x in base64_methods], - ), - create_option( - name="data", - description="Data to encode", - option_type=3, - required=True, - ), - ], + @slash_command(name="decode", description="Decode some data") + @slash_option( + name="method", + description="Decode method", + option_type=OptionTypes.STRING, + required=True, + choices=[SlashCommandChoice(name=x, value=x) for x in base64_methods], ) - async def _decode(self, ctx: SlashContext, method: str, data: str) -> None: + @slash_option( + name="data", + description="Data to encode", + option_type=OptionTypes.STRING, + required=True, + ) + async def _decode(self, ctx: InteractionContext, method: str, data: str) -> None: mstr = method method = getattr(base64, method + "decode") decoded = method(data.encode("UTF-8")).decode("UTF-8") @@ -243,24 +226,21 @@ class DevCog(commands.Cog): ) return fields = [ - Field(name="Plaintext", value=f"`{data}`", inline=False), - Field(name=mstr, value=f"`{decoded}`", inline=False), + EmbedField(name="Plaintext", value=f"`{data}`", inline=False), + EmbedField(name=mstr, value=f"`{decoded}`", inline=False), ] embed = build_embed(title="Decoded Data", description="", fields=fields) await ctx.send(embed=embed) - @cog_ext.cog_slash( - name="cloc", - description="Get J.A.R.V.I.S. lines of code", - ) - @commands.cooldown(1, 30, commands.BucketType.channel) - async def _cloc(self, ctx: SlashContext) -> None: + @slash_command(name="cloc", description="Get J.A.R.V.I.S. lines of code") + @cooldown(bucket=Buckets.CHANNEL, rate=1, interval=30) + async def _cloc(self, ctx: InteractionContext) -> None: output = subprocess.check_output( # noqa: S603, S607 ["tokei", "-C", "--sort", "code"] ).decode("UTF-8") await ctx.send(f"```\n{output}\n```") -def setup(bot: commands.Bot) -> None: +def setup(bot: Snake) -> None: """Add DevCog to J.A.R.V.I.S.""" bot.add_cog(DevCog(bot))