development #1

Merged
AllfatherHatt merged 157 commits from development into master 2025-06-14 15:47:26 +02:00
2 changed files with 41 additions and 46 deletions
Showing only changes of commit ddf1d883b6 - Show all commits

View File

@ -1,12 +1,19 @@
from redbot.core import commands from redbot.core import commands
import discord
async def list_allowed_roles_logic(ctx): class PermissionsMixin:
"""Handles the logic for listing allowed roles.""" """Handles role-based access control for Reginald."""
allowed_roles = await ctx.cog.config.guild(ctx.guild).allowed_roles() or []
# Ensure roles still exist in the server @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 []
# 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)] 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) if valid_roles != allowed_roles: # Update config only if there's a difference
await self.config.guild(ctx.guild).allowed_roles.set(valid_roles)
if not valid_roles: if not valid_roles:
await ctx.send("⚠️ No roles are currently allowed to interact with Reginald.") await ctx.send("⚠️ No roles are currently allowed to interact with Reginald.")
@ -14,3 +21,25 @@ async def list_allowed_roles_logic(ctx):
role_mentions = [f"<@&{role_id}>" for role_id in 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)}") 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.")

View File

@ -8,7 +8,7 @@ import traceback
from collections import Counter from collections import Counter
from redbot.core import Config, commands from redbot.core import Config, commands
from openai import OpenAIError from openai import OpenAIError
from .permissions import list_allowed_roles_logic from .permissions import PermissionsMixin
class ReginaldCog(commands.Cog): class ReginaldCog(commands.Cog):
def __init__(self, bot): 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 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) 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: async def is_blacklisted(self, user: discord.Member) -> bool:
blacklisted_users = await self.config.guild(user.guild).blacklisted_users() blacklisted_users = await self.config.guild(user.guild).blacklisted_users()
return str(user.id) in blacklisted_users return str(user.id) in blacklisted_users
@ -473,34 +467,6 @@ class ReginaldCog(commands.Cog):
else: else:
await ctx.send(f"No stored knowledge about {user.display_name} to delete.") 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.guild_only()
@commands.has_permissions(manage_guild=True) @commands.has_permissions(manage_guild=True)
@commands.command(help="Set the OpenAI API key") @commands.command(help="Set the OpenAI API key")