From a9c75207da672511d0a73c6a65aaff4bf243cdaa Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Wed, 2 Feb 2022 21:18:07 -0700 Subject: [PATCH] Migrate admin/roleping, ref #91 --- jarvis/cogs/admin/roleping.py | 248 +++++++++++++--------------------- 1 file changed, 95 insertions(+), 153 deletions(-) diff --git a/jarvis/cogs/admin/roleping.py b/jarvis/cogs/admin/roleping.py index fe81488..893416b 100644 --- a/jarvis/cogs/admin/roleping.py +++ b/jarvis/cogs/admin/roleping.py @@ -1,41 +1,37 @@ """J.A.R.V.I.S. RolepingCog.""" from datetime import datetime, timedelta -from ButtonPaginator import Paginator -from discord import Member, Role -from discord.ext.commands import Bot -from discord_slash import SlashContext, cog_ext -from discord_slash.model import ButtonStyle -from discord_slash.utils.manage_commands import create_option +from dis_snek import InteractionContext, Permissions, Snek +from dis_snek.ext.paginators import Paginator +from dis_snek.models.discord.embed import EmbedField +from dis_snek.models.discord.role import Role +from dis_snek.models.discord.user import Member +from dis_snek.models.snek.application_commands import ( + OptionTypes, + slash_command, + slash_option, +) from jarvis.db.models import Roleping from jarvis.utils import build_embed from jarvis.utils.cachecog import CacheCog -from jarvis.utils.field import Field from jarvis.utils.permissions import admin_or_permissions class RolepingCog(CacheCog): """J.A.R.V.I.S. RolepingCog.""" - def __init__(self, bot: Bot): + def __init__(self, bot: Snek): super().__init__(bot) - @cog_ext.cog_subcommand( - base="roleping", - name="add", - description="Add a role to roleping", - options=[ - create_option( - name="role", - description="Role to add to roleping", - option_type=8, - required=True, - ) - ], + @slash_command( + name="roleping", sub_cmd_name="add", sub_cmd_description="Add a role to roleping" ) - @admin_or_permissions(manage_guild=True) - async def _roleping_add(self, ctx: SlashContext, role: Role) -> None: + @slash_option( + name="role", description="Role to add", option_type=OptionTypes.ROLE, required=True + ) + @admin_or_permissions(Permissions.MANAGE_GUILD) + async def _roleping_add(self, ctx: InteractionContext, role: Role) -> None: roleping = Roleping.objects(guild=ctx.guild.id, role=role.id).first() if roleping: await ctx.send(f"Role `{role.name}` already in roleping.", hidden=True) @@ -49,21 +45,12 @@ class RolepingCog(CacheCog): ).save() await ctx.send(f"Role `{role.name}` added to roleping.") - @cog_ext.cog_subcommand( - base="roleping", - name="remove", - description="Remove a role from the roleping", - options=[ - create_option( - name="role", - description="Role to remove from roleping", - option_type=8, - required=True, - ) - ], + @slash_command(name="roleping", sub_cmd_name="remove", sub_cmd_description="Remove a role") + @slash_option( + name="role", description="Role to remove", option_type=OptionTypes.ROLE, required=True ) - @admin_or_permissions(manage_guild=True) - async def _roleping_remove(self, ctx: SlashContext, role: Role) -> None: + @admin_or_permissions(Permissions.MANAGE_GUILD) + async def _roleping_remove(self, ctx: InteractionContext, role: Role) -> None: roleping = Roleping.objects(guild=ctx.guild.id, role=role.id) if not roleping: await ctx.send("Roleping does not exist", hidden=True) @@ -72,12 +59,8 @@ class RolepingCog(CacheCog): roleping.delete() await ctx.send(f"Role `{role.name}` removed from roleping.") - @cog_ext.cog_subcommand( - base="roleping", - name="list", - description="List all blocklisted roles", - ) - async def _roleping_list(self, ctx: SlashContext) -> None: + @slash_command(name="roleping", sub_cmd_name="list", description="Lick all blocklisted roles") + async def _roleping_list(self, ctx: InteractionContext) -> None: exists = self.check_cache(ctx) if exists: await ctx.defer(hidden=True) @@ -108,17 +91,17 @@ class RolepingCog(CacheCog): description=role.mention, color=str(role.color), fields=[ - Field( + EmbedField( name="Created At", value=roleping.created_at.strftime("%a, %b %d, %Y %I:%M %p"), inline=False, ), - Field(name="Active", value=str(roleping.active)), - Field( + EmbedField(name="Active", value=str(roleping.active)), + EmbedField( name="Bypass Users", value="\n".join(bypass_users), ), - Field( + EmbedField( name="Bypass Roles", value="\n".join(bypass_roles), ), @@ -129,23 +112,12 @@ class RolepingCog(CacheCog): if not admin: admin = self.bot.user - embed.set_author(name=admin.nick or admin.name, icon_url=admin.avatar_url) + embed.set_author(name=admin.display_name, icon_url=admin.avatar_url) embed.set_footer(text=f"{admin.name}#{admin.discriminator} | {admin.id}") embeds.append(embed) - paginator = Paginator( - bot=self.bot, - ctx=ctx, - embeds=embeds, - only=ctx.author, - timeout=60 * 5, # 5 minute timeout - disable_after_timeout=True, - use_extend=len(embeds) > 2, - left_button_style=ButtonStyle.grey, - right_button_style=ButtonStyle.grey, - basic_buttons=["◀", "▶"], - ) + paginator = Paginator.create_from_embeds(self.bot, embeds, timeout=300) self.cache[hash(paginator)] = { "user": ctx.author.id, @@ -155,32 +127,26 @@ class RolepingCog(CacheCog): "paginator": paginator, } - await paginator.start() + await paginator.send(ctx) - @cog_ext.cog_subcommand( - base="roleping", - subcommand_group="bypass", - name="user", - description="Add a user as a bypass to a roleping", - base_desc="Block roles from being pinged", - sub_group_desc="Allow specific users/roles to ping rolepings", - options=[ - create_option( - name="user", - description="User to add", - option_type=6, - required=True, - ), - create_option( - name="rping", - description="Rolepinged role", - option_type=8, - required=True, - ), - ], + @slash_command( + name="roleping", + description="Block roles from being pinged", + group_name="bypass", + group_description="Allow specific users/roles to ping rolepings", + sub_cmd_name="user", + sub_cmd_description="Add a user as a bypass to a roleping", ) - @admin_or_permissions(manage_guild=True) - async def _roleping_bypass_user(self, ctx: SlashContext, user: Member, rping: Role) -> None: + @slash_option( + name="user", description="User to add", option_type=OptionTypes.USER, required=True + ) + @slash_option( + name="rping", description="Rolepinged role", option_type=OptionTypes.ROLE, required=True + ) + @admin_or_permissions(Permissions.MANAGE_GUILD) + async def _roleping_bypass_user( + self, ctx: InteractionContext, user: Member, rping: Role + ) -> None: roleping = Roleping.objects(guild=ctx.guild.id, role=rping.id).first() if not roleping: await ctx.send(f"Roleping not configured for {rping.mention}", hidden=True) @@ -208,32 +174,22 @@ class RolepingCog(CacheCog): roleping.bypass["users"].append(user.id) roleping.save() - await ctx.send(f"{user.nick or user.name} user bypass added for `{rping.name}`") + await ctx.send(f"{user.display_name} user bypass added for `{rping.name}`") - @cog_ext.cog_subcommand( - base="roleping", - subcommand_group="bypass", - name="role", - description="Add a role as a bypass to a roleping", - base_desc="Block roles from being pinged", - sub_group_desc="Allow specific users/roles to ping rolepings", - options=[ - create_option( - name="role", - description="Role to add", - option_type=8, - required=True, - ), - create_option( - name="rping", - description="Rolepinged role", - option_type=8, - required=True, - ), - ], + @slash_command( + name="roleping", + group_name="bypass", + sub_cmd_name="role", + description="Add a role as a bypass to roleping", ) - @admin_or_permissions(manage_guild=True) - async def _roleping_bypass_role(self, ctx: SlashContext, role: Role, rping: Role) -> None: + @slash_option( + name="role", description="Role to add", option_type=OptionTypes.ROLE, required=True + ) + @slash_option( + name="rping", description="Rolepinged role", option_type=OptionTypes.ROLE, required=True + ) + @admin_or_permissions(Permissions.MANAGE_GUILD) + async def _roleping_bypass_role(self, ctx: InteractionContext, role: Role, rping: Role) -> None: roleping = Roleping.objects(guild=ctx.guild.id, role=rping.id).first() if not roleping: await ctx.send(f"Roleping not configured for {rping.mention}", hidden=True) @@ -255,30 +211,24 @@ class RolepingCog(CacheCog): roleping.save() await ctx.send(f"{role.name} role bypass added for `{rping.name}`") - @cog_ext.cog_subcommand( - base="roleping", - subcommand_group="restore", - name="user", - description="Remove a role bypass", - base_desc="Block roles from being pinged", - sub_group_desc="Remove a bypass from a roleping (restoring it)", - options=[ - create_option( - name="user", - description="User to add", - option_type=6, - required=True, - ), - create_option( - name="rping", - description="Rolepinged role", - option_type=8, - required=True, - ), - ], + @slash_command( + name="roleping", + description="Block roles from being pinged", + group_name="restore", + group_description="Remove a roleping bypass", + sub_cmd_name="user", + sub_cmd_description="Remove a bypass from a roleping (restoring it)", ) - @admin_or_permissions(manage_guild=True) - async def _roleping_restore_user(self, ctx: SlashContext, user: Member, rping: Role) -> None: + @slash_option( + name="user", description="User to remove", option_type=OptionTypes.USER, required=True + ) + @slash_option( + name="rping", description="Rolepinged role", option_type=OptionTypes.ROLE, required=True + ) + @admin_or_permissions(Permissions.MANAGE_GUILD) + async def _roleping_restore_user( + self, ctx: InteractionContext, user: Member, rping: Role + ) -> None: roleping = Roleping.objects(guild=ctx.guild.id, role=rping.id).first() if not roleping: await ctx.send(f"Roleping not configured for {rping.mention}", hidden=True) @@ -290,32 +240,24 @@ class RolepingCog(CacheCog): roleping.bypass["users"].delete(user.id) roleping.save() - await ctx.send(f"{user.nick or user.name} user bypass removed for `{rping.name}`") + await ctx.send(f"{user.display_name} user bypass removed for `{rping.name}`") - @cog_ext.cog_subcommand( - base="roleping", - subcommand_group="restore", - name="role", - description="Remove a role bypass", - base_desc="Block roles from being pinged", - sub_group_desc="Remove a bypass from a roleping (restoring it)", - options=[ - create_option( - name="role", - description="Role to add", - option_type=8, - required=True, - ), - create_option( - name="rping", - description="Rolepinged role", - option_type=8, - required=True, - ), - ], + @slash_command( + name="roleping", + group_name="restore", + sub_cmd_name="role", + description="Remove a bypass from a roleping (restoring it)", + ) + @slash_option( + name="role", description="Role to remove", option_type=OptionTypes.ROLE, required=True + ) + @slash_option( + name="rping", description="Rolepinged role", option_type=OptionTypes.ROLE, required=True ) @admin_or_permissions(manage_guild=True) - async def _roleping_restore_role(self, ctx: SlashContext, role: Role, rping: Role) -> None: + async def _roleping_restore_role( + self, ctx: InteractionContext, role: Role, rping: Role + ) -> None: roleping = Roleping.objects(guild=ctx.guild.id, role=rping.id).first() if not roleping: await ctx.send(f"Roleping not configured for {rping.mention}", hidden=True)