feat(aurora): added sorting based on role position for the addrole and immune role configuration embeds

This commit is contained in:
Seaswimmer 2024-04-08 11:57:28 -04:00
parent 97b54b507b
commit df970717c2
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063

View file

@ -2,12 +2,16 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Union from typing import Union
from discord import Color, Embed, Guild, Interaction, InteractionMessage, Member, Role, User from discord import (Color, Embed, Guild, Interaction, InteractionMessage,
Member, Role, User)
from redbot.core import commands from redbot.core import commands
from redbot.core.utils.chat_formatting import bold, box, error, humanize_timedelta, warning from redbot.core.utils.chat_formatting import (bold, box, error,
humanize_timedelta, warning)
from aurora.utilities.config import config from aurora.utilities.config import config
from aurora.utilities.utils import fetch_channel_dict, fetch_user_dict, get_bool_emoji, get_next_case_number, get_pagesize_str from aurora.utilities.utils import (fetch_channel_dict, fetch_user_dict,
get_bool_emoji, get_next_case_number,
get_pagesize_str)
async def message_factory( async def message_factory(
@ -510,15 +514,29 @@ async def guild_embed(ctx: commands.Context) -> Embed:
async def addrole_embed(ctx: commands.Context) -> Embed: async def addrole_embed(ctx: commands.Context) -> Embed:
"""Generates a configuration menu field value for a guild's addrole whitelist.""" """Generates a configuration menu field value for a guild's addrole whitelist."""
whitelist = await config.guild(ctx.guild).addrole_whitelist() roles = []
if whitelist: async with config.guild(ctx.guild).addrole_whitelist() as whitelist:
whitelist = [ for role in whitelist:
ctx.guild.get_role(role).mention or error(f"`{role}` (Not Found)") evalulated_role = ctx.guild.get_role(role) or error(f"`{role}` (Not Found)")
for role in whitelist if isinstance(evalulated_role, Role):
] roles.append({
whitelist = "\n".join(whitelist) "id": evalulated_role.id,
"mention": evalulated_role.mention,
"position": evalulated_role.position
})
else:
roles.append({
"id": role,
"mention": error(f"`{role}` (Not Found)"),
"position": 0
})
if roles:
roles = sorted(roles, key=lambda x: x["position"], reverse=True)
roles = [role["mention"] for role in roles]
whitelist_str = "\n".join(roles)
else: else:
whitelist = warning("No roles are on the addrole whitelist!") whitelist_str = warning("No roles are on the addrole whitelist!")
e = await _config(ctx) e = await _config(ctx)
e.title += ": Addrole Whitelist" e.title += ": Addrole Whitelist"
@ -526,8 +544,8 @@ async def addrole_embed(ctx: commands.Context) -> Embed:
"Use the select menu below to manage this guild's addrole whitelist." "Use the select menu below to manage this guild's addrole whitelist."
) )
if len(whitelist) > 4000 and len(whitelist) < 5000: if len(whitelist_str) > 4000 and len(whitelist_str) < 5000:
lines = whitelist.split("\n") lines = whitelist_str.split("\n")
chunks = [] chunks = []
chunk = "" chunk = ""
for line in lines: for line in lines:
@ -541,21 +559,35 @@ async def addrole_embed(ctx: commands.Context) -> Embed:
for chunk in chunks: for chunk in chunks:
e.add_field(name="", value=chunk) e.add_field(name="", value=chunk)
else: else:
e.description += "\n\n" + whitelist e.description += "\n\n" + whitelist_str
return e return e
async def immune_embed(ctx: commands.Context) -> Embed: async def immune_embed(ctx: commands.Context) -> Embed:
"""Generates a configuration menu field value for a guild's immune roles.""" """Generates a configuration menu embed for a guild's immune roles."""
immune_roles = await config.guild(ctx.guild).immune_roles() roles = []
if immune_roles: async with config.guild(ctx.guild).immune_roles() as immune_roles:
immune_str = [ for role in immune_roles:
ctx.guild.get_role(role).mention or error(f"`{role}` (Not Found)") evalulated_role = ctx.guild.get_role(role) or error(f"`{role}` (Not Found)")
for role in immune_roles if isinstance(evalulated_role, Role):
] roles.append({
immune_str = "\n".join(immune_str) "id": evalulated_role.id,
"mention": evalulated_role.mention,
"position": evalulated_role.position
})
else:
roles.append({
"id": role,
"mention": error(f"`{role}` (Not Found)"),
"position": 0
})
if roles:
roles = sorted(roles, key=lambda x: x["position"], reverse=True)
roles = [role["mention"] for role in roles]
immune_str = "\n".join(roles)
else: else:
immune_str = warning("No roles are set as immune roles!") immune_str = warning("No roles are set as immune roles!")