From ddf1d883b69ce7894a2af8cd5f4be4c38202cfa6 Mon Sep 17 00:00:00 2001 From: AllfatherHatt Date: Thu, 13 Mar 2025 16:31:37 +0100 Subject: [PATCH] Trying to move permissions into its own file --- reginaldCog/permissions.py | 51 ++++++++++++++++++++++++++++++-------- reginaldCog/reginald.py | 36 +-------------------------- 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/reginaldCog/permissions.py b/reginaldCog/permissions.py index b08dfce..db8cd67 100644 --- a/reginaldCog/permissions.py +++ b/reginaldCog/permissions.py @@ -1,16 +1,45 @@ from redbot.core import commands +import discord -async def list_allowed_roles_logic(ctx): - """Handles the logic for listing allowed roles.""" - allowed_roles = await ctx.cog.config.guild(ctx.guild).allowed_roles() or [] +class PermissionsMixin: + """Handles role-based access control for Reginald.""" - # Ensure roles still exist in the server - valid_roles = [role_id for role_id in allowed_roles if ctx.guild.get_role(role_id)] - await ctx.cog.config.guild(ctx.guild).allowed_roles.set(valid_roles) + @commands.command(name="reginald_list_roles", help="List roles that can interact with Reginald.") + @commands.has_permissions(administrator=True) + async def list_allowed_roles(self, ctx: commands.Context): + """Lists all roles that are allowed to interact with Reginald.""" + allowed_roles = await self.config.guild(ctx.guild).allowed_roles() or [] - if not valid_roles: - await ctx.send("⚠️ No roles are currently allowed to interact with Reginald.") - return + # Ensure all roles still exist in the server + valid_roles = [role_id for role_id in allowed_roles if ctx.guild.get_role(role_id)] + if valid_roles != allowed_roles: # Update config only if there's a difference + await self.config.guild(ctx.guild).allowed_roles.set(valid_roles) - role_mentions = [f"<@&{role_id}>" for role_id in valid_roles] - await ctx.send(f"✅ **Roles with access to Reginald:**\n{', '.join(role_mentions)}") + if not valid_roles: + await ctx.send("⚠️ No roles are currently allowed to interact with Reginald.") + return + + role_mentions = [f"<@&{role_id}>" for role_id in valid_roles] + await ctx.send(f"✅ **Roles with access to Reginald:**\n{', '.join(role_mentions)}") + + @commands.command(name="reginald_allowrole", help="Grant a role permission to interact with Reginald.") + @commands.has_permissions(administrator=True) + async def allow_role(self, ctx: commands.Context, role: discord.Role): + """Grants a role permission to interact with Reginald.""" + async with self.config.guild(ctx.guild).allowed_roles() as allowed_roles: + if role.id not in allowed_roles: + allowed_roles.append(role.id) + await ctx.send(f"✅ Role **{role.name}** has been granted access to Reginald.") + else: + await ctx.send(f"⚠️ Role **{role.name}** already has access.") + + @commands.command(name="reginald_disallowrole", help="Revoke a role's access to interact with Reginald.") + @commands.has_permissions(administrator=True) + async def disallow_role(self, ctx: commands.Context, role: discord.Role): + """Revokes a role's permission to interact with Reginald.""" + async with self.config.guild(ctx.guild).allowed_roles() as allowed_roles: + if role.id in allowed_roles: + allowed_roles.remove(role.id) + await ctx.send(f"❌ Role **{role.name}** has been removed from Reginald's access list.") + else: + await ctx.send(f"⚠️ Role **{role.name}** was not in the access list.") diff --git a/reginaldCog/reginald.py b/reginaldCog/reginald.py index a503e60..b9349d6 100644 --- a/reginaldCog/reginald.py +++ b/reginaldCog/reginald.py @@ -8,7 +8,7 @@ import traceback from collections import Counter from redbot.core import Config, commands from openai import OpenAIError -from .permissions import list_allowed_roles_logic +from .permissions import PermissionsMixin class ReginaldCog(commands.Cog): def __init__(self, bot): @@ -45,12 +45,6 @@ class ReginaldCog(commands.Cog): allowed_roles = await self.config.guild(user.guild).allowed_roles() or [] # Ensure it's always a list return any(role.id in allowed_roles for role in user.roles) - - @commands.command(name="reginald_list_roles", help="List roles that can interact with Reginald.") - @commands.has_permissions(administrator=True) - async def list_allowed_roles(self, ctx): - await list_allowed_roles_logic(ctx) - async def is_blacklisted(self, user: discord.Member) -> bool: blacklisted_users = await self.config.guild(user.guild).blacklisted_users() return str(user.id) in blacklisted_users @@ -472,34 +466,6 @@ class ReginaldCog(commands.Cog): await ctx.send(f"Reginald has forgotten all stored information about {user.display_name}.") else: await ctx.send(f"No stored knowledge about {user.display_name} to delete.") - - @commands.command(name="reginald_allowrole", help="Grant a role permission to interact with Reginald.") - @commands.has_permissions(administrator=True) - async def allow_role(self, ctx, role: discord.Role): - async with self.config.guild(ctx.guild).allowed_roles() as allowed_roles: - # ✅ Clean list of invalid roles before adding new one - valid_roles = [role_id for role_id in allowed_roles if ctx.guild.get_role(role_id)] - - if role.id not in valid_roles: - valid_roles.append(role.id) - await self.config.guild(ctx.guild).allowed_roles.set(valid_roles) # Save change - await ctx.send(f"✅ Role {role.name} has been granted access to Reginald.") - else: - await ctx.send(f"⚠️ Role {role.name} already has access.") - - @commands.command(name="reginald_disallowrole", help="Revoke a role's access to interact with Reginald.") - @commands.has_permissions(administrator=True) - async def disallow_role(self, ctx, role: discord.Role): - async with self.config.guild(ctx.guild).allowed_roles() as allowed_roles: - valid_roles = [role_id for role_id in allowed_roles if ctx.guild.get_role(role_id)] - await self.config.guild(ctx.guild).allowed_roles.set(valid_roles) - - if role.id in valid_roles: - valid_roles.remove(role.id) - await self.config.guild(ctx.guild).allowed_roles.set(valid_roles) - await ctx.send(f"❌ Role {role.name} has been removed from Reginald's access list.") - else: - await ctx.send(f"⚠️ Role {role.name} was not in the access list.") @commands.guild_only() @commands.has_permissions(manage_guild=True)