From e1917e870e111ebbec06635885d4b80f55e8e7e9 Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Thu, 3 Feb 2022 19:48:56 -0700 Subject: [PATCH] Migrate starboard, closes #103 --- jarvis/cogs/starboard.py | 193 +++++++++++++++++---------------------- 1 file changed, 82 insertions(+), 111 deletions(-) diff --git a/jarvis/cogs/starboard.py b/jarvis/cogs/starboard.py index 4807be7..81747bb 100644 --- a/jarvis/cogs/starboard.py +++ b/jarvis/cogs/starboard.py @@ -1,16 +1,15 @@ """J.A.R.V.I.S. Starboard Cog.""" -from discord import TextChannel -from discord.ext import commands -from discord.utils import find -from discord_slash import SlashContext, cog_ext -from discord_slash.context import MenuContext -from discord_slash.model import ContextMenuType, SlashMessage -from discord_slash.utils.manage_commands import create_option -from discord_slash.utils.manage_components import ( - create_actionrow, - create_select, - create_select_option, - wait_for_component, +from dis_snek import InteractionContext, Permissions, Scale, Snake +from dis_snek.client.utils import find +from dis_snek.models.discord.channel import GuildText +from dis_snek.models.discord.components import ActionRow, Select, SelectOption +from dis_snek.models.discord.message import Message +from dis_snek.models.snek.application_commands import ( + CommandTypes, + OptionTypes, + context_menu, + slash_command, + slash_option, ) from jarvis.db.models import Star, Starboard @@ -26,19 +25,15 @@ supported_images = [ ] -class StarboardCog(commands.Cog): +class StarboardCog(Scale): """J.A.R.V.I.S. Starboard Cog.""" - def __init__(self, bot: commands.Bot): + def __init__(self, bot: Snake): self.bot = bot - @cog_ext.cog_subcommand( - base="starboard", - name="list", - description="Lists all Starboards", - ) - @admin_or_permissions(manage_guild=True) - async def _list(self, ctx: SlashContext) -> None: + @slash_command(name="starboard", sub_cmd_name="list", sub_cmd_description="List all starboards") + @admin_or_permissions(Permissions.MANAGE_GUILD) + async def _list(self, ctx: InteractionContext) -> None: starboards = Starboard.objects(guild=ctx.guild.id) if starboards != []: message = "Available Starboards:\n" @@ -48,29 +43,25 @@ class StarboardCog(commands.Cog): else: await ctx.send("No Starboards available.") - @cog_ext.cog_subcommand( - base="starboard", - name="create", - description="Create a starboard", - options=[ - create_option( - name="channel", - description="Starboard channel", - option_type=7, - required=True, - ), - ], + @slash_command( + name="starboard", sub_cmd_name="create", sub_cmd_description="Create a starboard" ) - @admin_or_permissions(manage_guild=True) - async def _create(self, ctx: SlashContext, channel: TextChannel) -> None: + @slash_option( + name="channel", + description="Starboard channel", + option_type=OptionTypes.CHANNEL, + required=True, + ) + @admin_or_permissions(Permissions.MANAGE_GUILD) + async def _create(self, ctx: InteractionContext, channel: GuildText) -> None: if channel not in ctx.guild.channels: await ctx.send( "Channel not in guild. Choose an existing channel.", hidden=True, ) return - if not isinstance(channel, TextChannel): - await ctx.send("Channel must be a TextChannel", hidden=True) + if not isinstance(channel, GuildText): + await ctx.send("Channel must be a GuildText", hidden=True) return exists = Starboard.objects(channel=channel.id, guild=ctx.guild.id).first() @@ -90,21 +81,17 @@ class StarboardCog(commands.Cog): ).save() await ctx.send(f"Starboard created. Check it out at {channel.mention}.") - @cog_ext.cog_subcommand( - base="starboard", - name="delete", - description="Delete a starboard", - options=[ - create_option( - name="channel", - description="Starboard channel", - option_type=7, - required=True, - ), - ], + @slash_command( + name="starboard", sub_cmd_name="delete", sub_cmd_description="Delete a starboard" ) - @admin_or_permissions(manage_guild=True) - async def _delete(self, ctx: SlashContext, channel: TextChannel) -> None: + @slash_option( + name="channel", + description="Starboard channel", + option_type=OptionTypes.CHANNEL, + required=True, + ) + @admin_or_permissions(Permissions.MANAGE_GUILD) + async def _delete(self, ctx: InteractionContext, channel: GuildText) -> None: deleted = Starboard.objects(channel=channel.id, guild=ctx.guild.id).delete() if deleted: _ = Star.objects(starboard=channel.id).delete() @@ -112,37 +99,26 @@ class StarboardCog(commands.Cog): else: await ctx.send(f"Starboard not found in {channel.mention}.", hidden=True) - @cog_ext.cog_context_menu(name="Star Message", target=ContextMenuType.MESSAGE) - async def _star_message(self, ctx: MenuContext) -> None: + @context_menu(name="Star Message", target=CommandTypes.MESSAGE) + async def _star_message(self, ctx: InteractionContext) -> None: await self._star_add.invoke(ctx, ctx.target_message) - @cog_ext.cog_subcommand( - base="star", - name="add", - description="Star a message", - options=[ - create_option( - name="message", - description="Message to star", - option_type=3, - required=True, - ), - create_option( - name="channel", - description=( - "Channel that has the message, " "required if different than command message" - ), - option_type=7, - required=False, - ), - ], + @slash_command(name="star", sub_cmd_name="add", description="Star a message") + @slash_option( + name="message", description="Message to star", option_type=OptionTypes.STRING, required=True ) - @admin_or_permissions(manage_guild=True) + @slash_option( + name="channel", + description="Channel that has the message, not required if used in same channel", + option_type=OptionTypes.CHANNEL, + required=False, + ) + @admin_or_permissions(Permissions.MANAGE_GUILD) async def _star_add( self, - ctx: SlashContext, + ctx: InteractionContext, message: str, - channel: TextChannel = None, + channel: GuildText = None, ) -> None: if not channel: channel = ctx.channel @@ -152,26 +128,35 @@ class StarboardCog(commands.Cog): return await ctx.defer() + + if not isinstance(message, Message): + if message.startswith("https://"): + message = message.split("/")[-1] + message = await channel.get_message(int(message)) + + if not message: + await ctx.send("Message not found", hidden=True) + return + channel_list = [] for starboard in starboards: channel_list.append(find(lambda x: x.id == starboard.channel, ctx.guild.channels)) select_channels = [ - create_select_option(label=x.name, value=str(idx)) for idx, x in enumerate(channel_list) + SelectOption(label=x.name, value=str(idx)) for idx, x in enumerate(channel_list) ] - select = create_select( + select = Select( options=select_channels, min_values=1, max_values=1, ) - components = [create_actionrow(select)] + components = [ActionRow(select)] msg = await ctx.send(content="Choose a starboard", components=components) - com_ctx = await wait_for_component( - self.bot, + com_ctx = await self.bot.wait_for_component( messages=msg, components=components, check=lambda x: x.author.id == ctx.author.id, @@ -179,11 +164,6 @@ class StarboardCog(commands.Cog): starboard = channel_list[int(com_ctx.selected_options[0])] - if not isinstance(message, SlashMessage): - if message.startswith("https://"): - message = message.split("/")[-1] - message = await channel.fetch_message(message) - exists = Star.objects( message=message.id, channel=message.channel.id, @@ -219,7 +199,7 @@ class StarboardCog(commands.Cog): timestamp=message.created_at, ) embed.set_author( - name=message.author.name, + name=message.author.display_name, url=message.jump_url, icon_url=message.author.display_avatar, ) @@ -247,34 +227,25 @@ class StarboardCog(commands.Cog): components=components, ) - @cog_ext.cog_subcommand( - base="star", - name="delete", - description="Delete a starred message", - options=[ - create_option( - name="id", - description="Star to delete", - option_type=4, - required=True, - ), - create_option( - name="starboard", - description="Starboard to delete star from", - option_type=7, - required=True, - ), - ], + @slash_command(name="star", sub_cmd_name="delete", description="Delete a starred message") + @slash_option( + name="message", description="Star to delete", option_type=OptionTypes.INTEGER, required=True ) - @admin_or_permissions(manage_guild=True) + @slash_option( + name="starboard", + description="Starboard to delete star from", + option_type=OptionTypes.CHANNEL, + required=True, + ) + @admin_or_permissions(Permissions.MANAGE_GUILD) async def _star_delete( self, - ctx: SlashContext, + ctx: InteractionContext, id: int, - starboard: TextChannel, + starboard: GuildText, ) -> None: - if not isinstance(starboard, TextChannel): - await ctx.send("Channel must be a TextChannel", hidden=True) + if not isinstance(starboard, GuildText): + await ctx.send("Channel must be a GuildText channel", hidden=True) return exists = Starboard.objects(channel=starboard.id, guild=ctx.guild.id).first() if not exists: @@ -304,7 +275,7 @@ class StarboardCog(commands.Cog): await ctx.send(f"Star {id} deleted") -def setup(bot: commands.Bot) -> None: +def setup(bot: Snake) -> None: """Add StarboardCog to J.A.R.V.I.S.""" bot.add_cog(StarboardCog(bot)) bot.add_cog(StarboardCog(bot))