Migrate dev, closes #116

This commit is contained in:
Zeva Rose 2022-02-03 21:16:04 -07:00
parent 487cc6b06b
commit bc94cd716b

View file

@ -8,12 +8,18 @@ from typing import Any, Union
import ulid as ulidpy import ulid as ulidpy
from bson import ObjectId from bson import ObjectId
from discord.ext import commands from dis_snek import InteractionContext, Scale, Snake
from discord_slash import SlashContext, cog_ext from dis_snek.models.discord.embed import EmbedField
from discord_slash.utils.manage_commands import create_choice, create_option 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 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} 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() return hash.hexdigest()
class DevCog(commands.Cog): class DevCog(Scale):
"""J.A.R.V.I.S. Developer Cog.""" """J.A.R.V.I.S. Developer Cog."""
def __init__(self, bot: commands.Bot): @slash_command(name="hash", description="Hash some data")
self.bot = bot @slash_option(
@cog_ext.cog_slash(
name="hash",
description="Hash some data",
options=[
create_option(
name="method", name="method",
description="Hash method", description="Hash method",
option_type=3, option_type=OptionTypes.STRING,
required=True, required=True,
choices=[create_choice(name=x, value=x) for x in supported_hashes], choices=[SlashCommandChoice(name=x, value=x) for x in supported_hashes],
), )
create_option( @slash_option(
name="data", name="data",
description="Data to hash", description="Data to hash",
option_type=3, option_type=OptionTypes.STRING,
required=True, required=True,
),
],
) )
@commands.cooldown(1, 2, commands.BucketType.user) @cooldown(bucket=Buckets.USER, rate=1, interval=2)
async def _hash(self, ctx: SlashContext, method: str, data: str) -> None: async def _hash(self, ctx: InteractionContext, method: str, data: str) -> None:
if not data: if not data:
await ctx.send( await ctx.send(
"No data to hash", "No data to hash",
@ -96,33 +94,28 @@ class DevCog(commands.Cog):
title = data if text else ctx.message.attachments[0].filename title = data if text else ctx.message.attachments[0].filename
description = "Hashed using " + method description = "Hashed using " + method
fields = [ fields = [
Field("Data Size", data_size, False), EmbedField("Data Size", data_size, False),
Field("Hash", f"`{hex}`", False), EmbedField("Hash", f"`{hex}`", False),
] ]
embed = build_embed(title=title, description=description, fields=fields) embed = build_embed(title=title, description=description, fields=fields)
await ctx.send(embed=embed) await ctx.send(embed=embed)
@cog_ext.cog_slash( @slash_command(name="uuid", description="Generate a UUID")
name="uuid", @slash_option(
description="Generate a UUID",
options=[
create_option(
name="version", name="version",
description="UUID version", description="UUID version",
option_type=3, option_type=OptionTypes.STRING,
required=True, required=True,
choices=[create_choice(name=x, value=x) for x in ["3", "4", "5"]], choices=[SlashCommandChoice(name=x, value=x) for x in ["3", "4", "5"]],
), )
create_option( @slash_option(
name="data", name="data",
description="Data for UUID version 3,5", description="Data for UUID version 3,5",
option_type=3, option_type=OptionTypes.STRING,
required=False, required=False,
),
],
) )
async def _uuid(self, ctx: SlashContext, version: str, data: str = None) -> None: async def _uuid(self, ctx: InteractionContext, version: str, data: str = None) -> None:
version = int(version) version = int(version)
if version in [3, 5] and not data: if version in [3, 5] and not data:
await ctx.send(f"UUID{version} requires data.", hidden=True) 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) to_send = UUID_GET[version](uuidpy.NAMESPACE_DNS, data)
await ctx.send(f"UUID{version}: `{to_send}`") await ctx.send(f"UUID{version}: `{to_send}`")
@cog_ext.cog_slash( @slash_command(
name="objectid", name="objectid",
description="Generate an ObjectID", description="Generate an ObjectID",
) )
@commands.cooldown(1, 2, commands.BucketType.user) @cooldown(bucket=Buckets.USER, rate=1, interval=2)
async def _objectid(self, ctx: SlashContext) -> None: async def _objectid(self, ctx: InteractionContext) -> None:
await ctx.send(f"ObjectId: `{str(ObjectId())}`") await ctx.send(f"ObjectId: `{str(ObjectId())}`")
@cog_ext.cog_slash( @slash_command(
name="ulid", name="ulid",
description="Generate a ULID", description="Generate a ULID",
) )
@commands.cooldown(1, 2, commands.BucketType.user) @cooldown(bucket=Buckets.USER, rate=1, interval=2)
async def _ulid(self, ctx: SlashContext) -> None: async def _ulid(self, ctx: InteractionContext) -> None:
await ctx.send(f"ULID: `{ulidpy.new().str}`") await ctx.send(f"ULID: `{ulidpy.new().str}`")
@cog_ext.cog_slash( @slash_command(
name="uuid2ulid", name="uuid2ulid",
description="Convert a UUID to a ULID", description="Convert a UUID to a ULID",
) )
@commands.cooldown(1, 2, commands.BucketType.user) @cooldown(bucket=Buckets.USER, rate=1, interval=2)
async def _uuid2ulid(self, ctx: SlashContext, uuid: str) -> None: async def _uuid2ulid(self, ctx: InteractionContext, uuid: str) -> None:
if UUID_VERIFY.match(uuid): if UUID_VERIFY.match(uuid):
u = ulidpy.parse(uuid) u = ulidpy.parse(uuid)
await ctx.send(f"ULID: `{u.str}`") await ctx.send(f"ULID: `{u.str}`")
else: else:
await ctx.send("Invalid UUID") await ctx.send("Invalid UUID")
@cog_ext.cog_slash( @slash_command(
name="ulid2uuid", name="ulid2uuid",
description="Convert a ULID to a UUID", description="Convert a ULID to a UUID",
) )
@commands.cooldown(1, 2, commands.BucketType.user) @cooldown(bucket=Buckets.USER, rate=1, interval=2)
async def _ulid2uuid(self, ctx: SlashContext, ulid: str) -> None: async def _ulid2uuid(self, ctx: InteractionContext, ulid: str) -> None:
if ULID_VERIFY.match(ulid): if ULID_VERIFY.match(ulid):
ulid = ulidpy.parse(ulid) ulid = ulidpy.parse(ulid)
await ctx.send(f"UUID: `{ulid.uuid}`") await ctx.send(f"UUID: `{ulid.uuid}`")
@ -183,56 +176,46 @@ class DevCog(commands.Cog):
base64_methods = ["b64", "b16", "b32", "a85", "b85"] base64_methods = ["b64", "b16", "b32", "a85", "b85"]
@cog_ext.cog_slash( @slash_command(name="encode", description="Encode some data")
name="encode", @slash_option(
description="Encode some data",
options=[
create_option(
name="method", name="method",
description="Encode method", description="Encode method",
option_type=3, option_type=OptionTypes.STRING,
required=True, required=True,
choices=[create_choice(name=x, value=x) for x in base64_methods], choices=[SlashCommandChoice(name=x, value=x) for x in base64_methods],
), )
create_option( @slash_option(
name="data", name="data",
description="Data to encode", description="Data to encode",
option_type=3, option_type=OptionTypes.STRING,
required=True, required=True,
),
],
) )
async def _encode(self, ctx: SlashContext, method: str, data: str) -> None: async def _encode(self, ctx: InteractionContext, method: str, data: str) -> None:
mstr = method mstr = method
method = getattr(base64, method + "encode") method = getattr(base64, method + "encode")
encoded = method(data.encode("UTF-8")).decode("UTF-8") encoded = method(data.encode("UTF-8")).decode("UTF-8")
fields = [ fields = [
Field(name="Plaintext", value=f"`{data}`", inline=False), EmbedField(name="Plaintext", value=f"`{data}`", inline=False),
Field(name=mstr, value=f"`{encoded}`", inline=False), EmbedField(name=mstr, value=f"`{encoded}`", inline=False),
] ]
embed = build_embed(title="Decoded Data", description="", fields=fields) embed = build_embed(title="Decoded Data", description="", fields=fields)
await ctx.send(embed=embed) await ctx.send(embed=embed)
@cog_ext.cog_slash( @slash_command(name="decode", description="Decode some data")
name="decode", @slash_option(
description="Decode some data",
options=[
create_option(
name="method", name="method",
description="Decode method", description="Decode method",
option_type=3, option_type=OptionTypes.STRING,
required=True, required=True,
choices=[create_choice(name=x, value=x) for x in base64_methods], choices=[SlashCommandChoice(name=x, value=x) for x in base64_methods],
), )
create_option( @slash_option(
name="data", name="data",
description="Data to encode", description="Data to encode",
option_type=3, option_type=OptionTypes.STRING,
required=True, required=True,
),
],
) )
async def _decode(self, ctx: SlashContext, method: str, data: str) -> None: async def _decode(self, ctx: InteractionContext, method: str, data: str) -> None:
mstr = method mstr = method
method = getattr(base64, method + "decode") method = getattr(base64, method + "decode")
decoded = method(data.encode("UTF-8")).decode("UTF-8") decoded = method(data.encode("UTF-8")).decode("UTF-8")
@ -243,24 +226,21 @@ class DevCog(commands.Cog):
) )
return return
fields = [ fields = [
Field(name="Plaintext", value=f"`{data}`", inline=False), EmbedField(name="Plaintext", value=f"`{data}`", inline=False),
Field(name=mstr, value=f"`{decoded}`", inline=False), EmbedField(name=mstr, value=f"`{decoded}`", inline=False),
] ]
embed = build_embed(title="Decoded Data", description="", fields=fields) embed = build_embed(title="Decoded Data", description="", fields=fields)
await ctx.send(embed=embed) await ctx.send(embed=embed)
@cog_ext.cog_slash( @slash_command(name="cloc", description="Get J.A.R.V.I.S. lines of code")
name="cloc", @cooldown(bucket=Buckets.CHANNEL, rate=1, interval=30)
description="Get J.A.R.V.I.S. lines of code", async def _cloc(self, ctx: InteractionContext) -> None:
)
@commands.cooldown(1, 30, commands.BucketType.channel)
async def _cloc(self, ctx: SlashContext) -> None:
output = subprocess.check_output( # noqa: S603, S607 output = subprocess.check_output( # noqa: S603, S607
["tokei", "-C", "--sort", "code"] ["tokei", "-C", "--sort", "code"]
).decode("UTF-8") ).decode("UTF-8")
await ctx.send(f"```\n{output}\n```") 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.""" """Add DevCog to J.A.R.V.I.S."""
bot.add_cog(DevCog(bot)) bot.add_cog(DevCog(bot))