From 4159cbdfbd096ded23431bfd3e39d63b7d258715 Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Tue, 1 Feb 2022 11:32:38 -0700 Subject: [PATCH] Fix error in bans --- jarvis/cogs/admin/ban.py | 31 ++++---- jarvis/cogs/rolegiver.py | 152 ++++++++++++++++++++++++--------------- 2 files changed, 115 insertions(+), 68 deletions(-) diff --git a/jarvis/cogs/admin/ban.py b/jarvis/cogs/admin/ban.py index c4cca83..828d9b3 100644 --- a/jarvis/cogs/admin/ban.py +++ b/jarvis/cogs/admin/ban.py @@ -31,6 +31,7 @@ class BanCog(CacheCog): duration: int, active: bool, fields: list, + mtype: str, ) -> None: """Apply a Discord ban.""" await ctx.guild.ban(user, reason=reason) @@ -41,7 +42,7 @@ class BanCog(CacheCog): reason=reason, admin=ctx.author.id, guild=ctx.guild.id, - type=type, + type=mtype, duration=duration, active=active, ).save() @@ -103,14 +104,14 @@ class BanCog(CacheCog): option_type=3, ), create_option( - name="type", + name="btype", description="Ban type", option_type=3, required=False, choices=[ - create_choice(value="perm", name="Permanent"), - create_choice(value="temp", name="Temporary"), - create_choice(value="soft", name="Soft"), + create_choice(value=0, name="Permanent"), + create_choice(value=1, name="Temporary"), + create_choice(value=2, name="Soft"), ], ), create_option( @@ -127,19 +128,25 @@ class BanCog(CacheCog): ctx: SlashContext, user: User = None, reason: str = None, - type: str = "perm", + btype: int = 0, duration: int = 4, ) -> None: + if btype == 0: + btype = "perm" + elif btype == 1: + btype = "temp" + else: + btype = "soft" if not user or user == ctx.author: await ctx.send("You cannot ban yourself.", hidden=True) return if user == self.bot.user: await ctx.send("I'm afraid I can't let you do that", hidden=True) return - if type == "temp" and duration < 0: + if btype == "temp" and duration < 0: await ctx.send("You cannot set a temp ban to < 0 hours.", hidden=True) return - elif type == "temp" and duration > 744: + elif btype == "temp" and duration > 744: await ctx.send("You cannot set a temp ban to > 1 month", hidden=True) return if len(reason) > 100: @@ -150,7 +157,7 @@ class BanCog(CacheCog): await ctx.defer() - mtype = type + mtype = btype if mtype == "perm": mtype = "perma" @@ -190,13 +197,13 @@ class BanCog(CacheCog): await ctx.guild.unban(user, reason="Ban was softban") fields.append(Field(name="DM Sent?", value=str(not send_failed))) - if type != "temp": + if btype != "temp": duration = None active = True - if type == "soft": + if btype == "soft": active = False - await self.discord_apply_ban(ctx, reason, user, duration, active, fields) + await self.discord_apply_ban(ctx, reason, user, duration, active, fields, mtype) @cog_ext.cog_slash( name="unban", diff --git a/jarvis/cogs/rolegiver.py b/jarvis/cogs/rolegiver.py index e0737db..4047ac0 100644 --- a/jarvis/cogs/rolegiver.py +++ b/jarvis/cogs/rolegiver.py @@ -1,8 +1,16 @@ """J.A.R.V.I.S. Role Giver Cog.""" +import asyncio + from discord import Role from discord.ext import commands from discord_slash import SlashContext, cog_ext 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 jarvis.db.models import Rolegiver from jarvis.utils import build_embed @@ -39,6 +47,10 @@ class RolegiverCog(commands.Cog): if not setting: setting = Rolegiver(guild=ctx.guild.id, roles=[]) + if len(setting.roles) >= 20: + await ctx.send("You can only have 20 roles in the rolegiver", hidden=True) + return + setting.roles.append(role.id) setting.save() @@ -79,59 +91,84 @@ class RolegiverCog(commands.Cog): base="rolegiver", name="remove", description="Remove a role from rolegiver", - options=[ - create_option( - name="role", - description="Role to remove", - option_type=8, - required=True, - ) - ], ) @admin_or_permissions(manage_guild=True) - async def _rolegiver_remove(self, ctx: SlashContext, role: Role) -> None: + async def _rolegiver_remove(self, ctx: SlashContext) -> None: setting = Rolegiver.objects(guild=ctx.guild.id).first() if not setting or (setting and not setting.roles): await ctx.send("Rolegiver has no roles", hidden=True) return - elif role.id not in setting.roles: - await ctx.send("Role not in rolegiver", hidden=True) - return - setting.roles.remove(role.id) - setting.save() + options = [] + for role in setting.roles: + role: Role = ctx.guild.get_role(role) + option = create_select_option(label=role.name, value=str(role.id)) + options.append(option) - roles = [] - for role_id in setting.roles: - e_role = ctx.guild.get_role(role_id) - if not e_role: - continue - roles.append(e_role) - - if roles: - roles.sort(key=lambda x: -x.position) - - value = "\n".join([r.mention for r in roles]) if roles else "None" - fields = [ - Field(name="Removed Role", value=f"{role.mention}"), - Field(name="Remaining Role(s)", value=value), - ] - - embed = build_embed( - title="Rolegiver Updated", - description="Role removed from rolegiver", - fields=fields, + select = create_select( + options=options, + custom_id="to_delete", + placeholder="Select roles to remove", + min_values=1, + max_values=len(options), ) + components = [create_actionrow(select)] - embed.set_thumbnail(url=ctx.guild.icon_url) - embed.set_author( - name=ctx.author.nick if ctx.author.nick else ctx.author.name, - icon_url=ctx.author.avatar_url, - ) + message = await ctx.send(content="\u200b", components=components) + try: + context = await wait_for_component( + self.bot, + check=lambda x: ctx.author.id == x.author.id, + message=message, + timeout=60 * 1, + ) + for to_delete in context.selected_options: + setting.roles.remove(int(to_delete)) + setting.save() + for row in components: + for component in row["components"]: + component["disabled"] = True - embed.set_footer(text=f"{ctx.author.name}#{ctx.author.discriminator} | {ctx.author.id}") + roles = [] + for role_id in setting.roles: + e_role = ctx.guild.get_role(role_id) + if not e_role: + continue + roles.append(e_role) - await ctx.send(embed=embed) + if roles: + roles.sort(key=lambda x: -x.position) + + value = "\n".join([r.mention for r in roles]) if roles else "None" + fields = [ + Field(name="Removed Role", value=f"{role.mention}"), + Field(name="Remaining Role(s)", value=value), + ] + + embed = build_embed( + title="Rolegiver Updated", + description="Role removed from rolegiver", + fields=fields, + ) + + embed.set_thumbnail(url=ctx.guild.icon_url) + embed.set_author( + name=ctx.author.nick if ctx.author.nick else ctx.author.name, + icon_url=ctx.author.avatar_url, + ) + + embed.set_footer(text=f"{ctx.author.name}#{ctx.author.discriminator} | {ctx.author.id}") + + await context.edit_origin( + content=f"Removed {len(context.selected_options)} role(s)", + embed=embed, + components=components, + ) + except asyncio.TimeoutError: + for row in components: + for component in row["components"]: + component["disabled"] = True + await message.edit(components=components) @cog_ext.cog_subcommand( base="rolegiver", @@ -176,27 +213,30 @@ class RolegiverCog(commands.Cog): base="role", name="get", description="Get a role from rolegiver", - options=[ - create_option( - name="role", - description="Role to add", - option_type=8, - required=True, - ) - ], ) @commands.cooldown(1, 10, commands.BucketType.user) - async def _role_get(self, ctx: SlashContext, role: Role) -> None: + async def _role_get(self, ctx: SlashContext) -> None: setting = Rolegiver.objects(guild=ctx.guild.id).first() if not setting or (setting and not setting.roles): await ctx.send("Rolegiver has no roles", hidden=True) return - elif role.id not in setting.roles: - await ctx.send("Role not in rolegiver", hidden=True) - return - elif role in ctx.author.roles: - await ctx.send("You already have that role", hidden=True) - return + + options = [] + for role in setting.roles: + role: Role = ctx.guild.get_role(role) + option = create_select_option(label=role.name, value=str(role.id)) + options.append(option) + + select = create_select( + options=options, + custom_id="to_delete", + placeholder="Select roles to remove", + min_values=1, + max_values=len(options), + ) + components = [create_actionrow(select)] + + _ = await ctx.send(content="\u200b", components=components) await ctx.author.add_roles(role, reason="Rolegiver")