From 6f2fdc55d7025360a491cf5e89f73150c386a0ab Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Fri, 9 Sep 2022 22:23:13 -0600 Subject: [PATCH] Condense slash commands --- jarvis/client/events/__init__.py | 30 +++++++++++++++++++ jarvis/cogs/dev.py | 38 ++++++++++++------------ jarvis/cogs/image.py | 6 ++-- jarvis/cogs/remindme.py | 7 ++--- jarvis/cogs/rolegiver.py | 6 ++-- jarvis/cogs/starboard.py | 50 -------------------------------- jarvis/cogs/util.py | 29 +++++++++++------- 7 files changed, 77 insertions(+), 89 deletions(-) diff --git a/jarvis/client/events/__init__.py b/jarvis/client/events/__init__.py index f6f2b34..4c68ce7 100644 --- a/jarvis/client/events/__init__.py +++ b/jarvis/client/events/__init__.py @@ -8,6 +8,7 @@ from jarvis_core.util.ansi import RESET, Fore, Format, fmt from naff import listen from naff.models.discord.channel import DMChannel from naff.models.discord.embed import EmbedField +from naff.models.naff.application_commands import ContextMenu from naff.models.naff.context import Context, InteractionContext, PrefixedContext from jarvis import const @@ -61,6 +62,35 @@ class EventMixin(MemberEventMixin, MessageEventMixin, ComponentEventMixin): "{}&permissions=8&scope=bot%20applications.commands".format(self.user.id) ) + global_base_commands = {} + guild_base_commands = {} + global_context_menus = [] + guild_context_menus = [] + for cid in self.interactions: + commands = self.interactions[cid] + to_update = global_base_commands if cid == 0 else guild_base_commands + for command in commands: + if isinstance(commands[command], ContextMenu): + if cid == 0: + global_context_menus.append(command) + else: + guild_context_menus.append(command) + continue + full = command.split(" ") + base = full[0] + if base not in to_update: + to_update[base] = {} + if len(full) == 3: + to_update[base][full[1]] = full[2] + elif len(full) == 2: + to_update[base][full[1]] = None + + self.logger.info( + "Loaded {:>2} global base slash commands".format(len(global_base_commands)) + ) + self.logger.info("Loaded {:>2} global context menus".format(len(global_context_menus))) + self.logger.info("Loaded {:>2} guild base slash commands".format(len(guild_base_commands))) + self.logger.info("Loaded {:>2} guild context menus".format(len(guild_context_menus))) self.logger.debug("Hitting Reminders for faster loads") _ = await Reminder.find().to_list(None) diff --git a/jarvis/cogs/dev.py b/jarvis/cogs/dev.py index cad6dab..561dee0 100644 --- a/jarvis/cogs/dev.py +++ b/jarvis/cogs/dev.py @@ -23,8 +23,8 @@ from naff.models.discord.file import File from naff.models.discord.message import Attachment from naff.models.naff.application_commands import ( OptionTypes, + SlashCommand, SlashCommandChoice, - slash_command, slash_option, ) from naff.models.naff.command import cooldown @@ -60,7 +60,9 @@ class DevCog(Extension): self.bot = bot self.logger = logging.getLogger(__name__) - @slash_command(name="hash", description="Hash some data") + dev = SlashCommand(name="dev", description="Developer utilities") + + @dev.subcommand(sub_cmd_name="hash", sub_cmd_description="Hash some data") @slash_option( name="method", description="Hash method", @@ -126,7 +128,7 @@ class DevCog(Extension): ) await ctx.send(embeds=embed, components=components) - @slash_command(name="uuid", description="Generate a UUID") + @dev.subcommand(sub_cmd_name="uuid", sub_cmd_description="Generate a UUID") @slash_option( name="version", description="UUID version", @@ -159,25 +161,25 @@ class DevCog(Extension): to_send = UUID_GET[version](uuidpy.NAMESPACE_DNS, data) await ctx.send(f"UUID{version}: `{to_send}`") - @slash_command( - name="objectid", - description="Generate an ObjectID", + @dev.subcommand( + sub_cmd_name="objectid", + sub_cmd_description="Generate an ObjectID", ) @cooldown(bucket=Buckets.USER, rate=1, interval=2) async def _objectid(self, ctx: InteractionContext) -> None: await ctx.send(f"ObjectId: `{str(ObjectId())}`") - @slash_command( - name="ulid", - description="Generate a ULID", + @dev.subcommand( + sub_cmd_name="ulid", + sub_cmd_description="Generate a ULID", ) @cooldown(bucket=Buckets.USER, rate=1, interval=2) async def _ulid(self, ctx: InteractionContext) -> None: await ctx.send(f"ULID: `{ulidpy.new().str}`") - @slash_command( - name="uuid2ulid", - description="Convert a UUID to a ULID", + @dev.subcommand( + sub_cmd_name="uuid2ulid", + sub_cmd_description="Convert a UUID to a ULID", ) @slash_option( name="uuid", description="UUID to convert", opt_type=OptionTypes.STRING, required=True @@ -190,9 +192,9 @@ class DevCog(Extension): else: await ctx.send("Invalid UUID") - @slash_command( - name="ulid2uuid", - description="Convert a ULID to a UUID", + @dev.subcommand( + sub_cmd_name="ulid2uuid", + sub_cmd_description="Convert a ULID to a UUID", ) @slash_option( name="ulid", description="ULID to convert", opt_type=OptionTypes.STRING, required=True @@ -207,7 +209,7 @@ class DevCog(Extension): base64_methods = ["b64", "b16", "b32", "a85", "b85"] - @slash_command(name="encode", description="Encode some data") + @dev.subcommand(sub_cmd_name="encode", sub_cmd_description="Encode some data") @slash_option( name="method", description="Encode method", @@ -245,7 +247,7 @@ class DevCog(Extension): ) await ctx.send(embeds=embed, components=components) - @slash_command(name="decode", description="Decode some data") + @dev.subcommand(sub_cmd_name="decode", sub_cmd_description="Decode some data") @slash_option( name="method", description="Decode method", @@ -283,7 +285,7 @@ class DevCog(Extension): ) await ctx.send(embeds=embed, components=components) - @slash_command(name="cloc", description="Get JARVIS lines of code") + @dev.subcommand(sub_cmd_name="cloc", sub_cmd_description="Get JARVIS lines of code") @cooldown(bucket=Buckets.CHANNEL, rate=1, interval=30) async def _cloc(self, ctx: InteractionContext) -> None: await ctx.defer() diff --git a/jarvis/cogs/image.py b/jarvis/cogs/image.py index 93f7a12..83e385b 100644 --- a/jarvis/cogs/image.py +++ b/jarvis/cogs/image.py @@ -15,7 +15,7 @@ from naff.models.discord.file import File from naff.models.discord.message import Attachment from naff.models.naff.application_commands import ( OptionTypes, - slash_command, + SlashCommand, slash_option, ) @@ -40,7 +40,9 @@ class ImageCog(Extension): def __del__(self): self._session.close() - @slash_command(name="resize", description="Resize an image") + image = SlashCommand(name="image", description="Manipulate images") + + @image.subcommand(sub_cmd_name="shrink", sub_cmd_description="Shrink an image") @slash_option( name="target", description="Target size, i.e. 200KB", diff --git a/jarvis/cogs/remindme.py b/jarvis/cogs/remindme.py index b247bc4..9d2f0ce 100644 --- a/jarvis/cogs/remindme.py +++ b/jarvis/cogs/remindme.py @@ -18,7 +18,6 @@ from naff.models.discord.modal import InputText, Modal, TextStyles from naff.models.naff.application_commands import ( OptionTypes, SlashCommand, - slash_command, slash_option, ) from thefuzz import process @@ -40,7 +39,9 @@ class RemindmeCog(Extension): self.bot = bot self.logger = logging.getLogger(__name__) - @slash_command(name="remindme", description="Set a reminder") + reminders = SlashCommand(name="reminders", description="Manage reminders") + + @reminders.subcommand(sub_cmd_name="set", sub_cmd_description="Set a reminder") @slash_option( name="private", description="Send as DM?", @@ -210,8 +211,6 @@ class RemindmeCog(Extension): return embed - reminders = SlashCommand(name="reminders", description="Manage reminders") - @reminders.subcommand(sub_cmd_name="list", sub_cmd_description="List reminders") async def _list(self, ctx: InteractionContext) -> None: reminders = await Reminder.find(q(user=ctx.author.id, active=True)).to_list(None) diff --git a/jarvis/cogs/rolegiver.py b/jarvis/cogs/rolegiver.py index 1cc1c45..b4727c8 100644 --- a/jarvis/cogs/rolegiver.py +++ b/jarvis/cogs/rolegiver.py @@ -212,9 +212,7 @@ class RolegiverCog(Extension): ) await ctx.send(embeds=embed, components=components) - role = SlashCommand(name="role", description="Get/Remove Rolegiver roles") - - @role.subcommand(sub_cmd_name="get", sub_cmd_description="Get a role") + @rolegiver.subcommand(sub_cmd_name="get", sub_cmd_description="Get a role") @cooldown(bucket=Buckets.USER, rate=1, interval=10) async def _role_get(self, ctx: InteractionContext) -> None: setting = await Rolegiver.find_one(q(guild=ctx.guild.id)) @@ -290,7 +288,7 @@ class RolegiverCog(Extension): component.disabled = True await message.edit(components=components) - @role.subcommand(sub_cmd_name="remove", sub_cmd_description="Remove a role") + @rolegiver.subcommand(sub_cmd_name="forfeit", sub_cmd_description="Forfeit a role") @cooldown(bucket=Buckets.USER, rate=1, interval=10) async def _role_remove(self, ctx: InteractionContext) -> None: user_roles = ctx.author.roles diff --git a/jarvis/cogs/starboard.py b/jarvis/cogs/starboard.py index 7e62058..018480c 100644 --- a/jarvis/cogs/starboard.py +++ b/jarvis/cogs/starboard.py @@ -265,56 +265,6 @@ class StarboardCog(Extension): description="Manage stars", ) - @star.subcommand(sub_cmd_name="delete", sub_cmd_description="Delete a starred message") - @slash_option( - name="id", description="Star ID to delete", opt_type=OptionTypes.INTEGER, required=True - ) - @slash_option( - name="starboard", - description="Starboard to delete star from", - opt_type=OptionTypes.CHANNEL, - required=True, - ) - @check(admin_or_permissions(Permissions.MANAGE_GUILD)) - async def _star_delete( - self, - ctx: InteractionContext, - id: int, - starboard: GuildText, - ) -> None: - if not isinstance(starboard, GuildText): - await ctx.send("Channel must be a GuildText channel", ephemeral=True) - return - - exists = await Starboard.find_one(q(channel=starboard.id, guild=ctx.guild.id)) - if not exists: - # TODO: automagically create starboard - await ctx.send( - f"Starboard does not exist in {starboard.mention}. Please create it first", - ephemeral=True, - ) - return - - star = await Star.find_one( - q( - starboard=starboard.id, - index=id, - guild=ctx.guild.id, - active=True, - ) - ) - if not star: - await ctx.send(f"No star exists with id {id}", ephemeral=True) - return - - message = await starboard.fetch_message(star.star) - if message: - await message.delete() - - await star.delete() - - await ctx.send(f"Star {id} deleted from {starboard.mention}") - def setup(bot: Client) -> None: """Add StarboardCog to JARVIS""" diff --git a/jarvis/cogs/util.py b/jarvis/cogs/util.py index 1134a81..f9ac647 100644 --- a/jarvis/cogs/util.py +++ b/jarvis/cogs/util.py @@ -20,6 +20,7 @@ from naff.models.discord.user import User from naff.models.naff.application_commands import ( CommandTypes, OptionTypes, + SlashCommand, SlashCommandChoice, context_menu, slash_command, @@ -49,7 +50,9 @@ class UtilCog(Extension): self.bot = bot self.logger = logging.getLogger(__name__) - @slash_command(name="status", description="Retrieve JARVIS status") + bot = SlashCommand(name="bot", description="Bot commands") + + @bot.subcommand(sub_cmd_name="status", sub_cmd_description="Retrieve JARVIS status") @cooldown(bucket=Buckets.CHANNEL, rate=1, interval=30) async def _status(self, ctx: InteractionContext) -> None: title = "JARVIS Status" @@ -74,9 +77,9 @@ class UtilCog(Extension): ) await ctx.send(embeds=embed, components=components) - @slash_command( - name="logo", - description="Get the current logo", + @bot.subcommand( + sub_cmd_name="logo", + sub_cmd_description="Get the current logo", ) @cooldown(bucket=Buckets.CHANNEL, rate=1, interval=30) async def _logo(self, ctx: InteractionContext) -> None: @@ -89,13 +92,15 @@ class UtilCog(Extension): ) await ctx.send(file=logo, components=components) - @slash_command(name="rchk", description="Robot Camo HK416") + rc = SlashCommand(name="rc", description="Robot Camo emoji commands") + + @rc.subcommand(sub_cmd_name="hk", sub_cmd_description="Robot Camo HK416") async def _rchk(self, ctx: InteractionContext) -> None: await ctx.send(content=hk, ephemeral=True) - @slash_command( - name="rcauto", - description="Automates robot camo letters", + @rc.subcommand( + sub_cmd_name="auto", + sub_cmd_description="Automates robot camo letters", ) @slash_option( name="text", @@ -176,7 +181,7 @@ class UtilCog(Extension): embed.set_thumbnail(url="attachment://color_show.png") data = np.array(JARVIS_LOGO) - r, g, b, a = data.T + *_, a = data.T fill = a > 0 @@ -397,7 +402,7 @@ class UtilCog(Extension): ) await ctx.send(embeds=embed, ephemeral=private, components=components) - @slash_command(name="support", description="Got issues?") + @bot.subcommand(sub_cmd_name="support", sub_cmd_description="Got issues?") async def _support(self, ctx: InteractionContext) -> None: await ctx.send( f""" @@ -409,7 +414,9 @@ We'll help as best we can with whatever issues you encounter. """ ) - @slash_command(name="privacy_terms", description="View Privacy and Terms of Use") + @bot.subcommand( + sub_cmd_name="privacy_terms", sub_cmd_description="View Privacy and Terms of Use" + ) async def _privacy_terms(self, ctx: InteractionContext) -> None: await ctx.send( """