From df970717c280e686c12899e567437aa3763c56d6 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 8 Apr 2024 11:57:28 -0400 Subject: [PATCH] feat(aurora): added sorting based on role position for the addrole and immune role configuration embeds --- aurora/utilities/factory.py | 76 ++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/aurora/utilities/factory.py b/aurora/utilities/factory.py index 9a9e572..3cfaf9e 100644 --- a/aurora/utilities/factory.py +++ b/aurora/utilities/factory.py @@ -2,12 +2,16 @@ from datetime import datetime, timedelta 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.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.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( @@ -510,15 +514,29 @@ async def guild_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.""" - whitelist = await config.guild(ctx.guild).addrole_whitelist() - if whitelist: - whitelist = [ - ctx.guild.get_role(role).mention or error(f"`{role}` (Not Found)") - for role in whitelist - ] - whitelist = "\n".join(whitelist) + roles = [] + async with config.guild(ctx.guild).addrole_whitelist() as whitelist: + for role in whitelist: + evalulated_role = ctx.guild.get_role(role) or error(f"`{role}` (Not Found)") + if isinstance(evalulated_role, Role): + roles.append({ + "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: - whitelist = warning("No roles are on the addrole whitelist!") + whitelist_str = warning("No roles are on the addrole whitelist!") e = await _config(ctx) 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." ) - if len(whitelist) > 4000 and len(whitelist) < 5000: - lines = whitelist.split("\n") + if len(whitelist_str) > 4000 and len(whitelist_str) < 5000: + lines = whitelist_str.split("\n") chunks = [] chunk = "" for line in lines: @@ -541,21 +559,35 @@ async def addrole_embed(ctx: commands.Context) -> Embed: for chunk in chunks: e.add_field(name="", value=chunk) else: - e.description += "\n\n" + whitelist + e.description += "\n\n" + whitelist_str return e 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() - if immune_roles: - immune_str = [ - ctx.guild.get_role(role).mention or error(f"`{role}` (Not Found)") - for role in immune_roles - ] - immune_str = "\n".join(immune_str) + roles = [] + async with config.guild(ctx.guild).immune_roles() as immune_roles: + for role in immune_roles: + evalulated_role = ctx.guild.get_role(role) or error(f"`{role}` (Not Found)") + if isinstance(evalulated_role, Role): + roles.append({ + "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: immune_str = warning("No roles are set as immune roles!")