From 7922a97939c1d78da4a57bf8ed793527572c36b1 Mon Sep 17 00:00:00 2001 From: scragly <29337040+scragly@users.noreply.github.com> Date: Tue, 7 Sep 2021 13:09:44 +1000 Subject: [PATCH 1/2] Allow top role's hoisted check to be configurable. --- cogs/modmail.py | 4 ++-- core/config.py | 2 ++ core/thread.py | 6 +++--- core/utils.py | 6 ++++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/cogs/modmail.py b/cogs/modmail.py index 4a974b26cc..fdbc27429b 100644 --- a/cogs/modmail.py +++ b/cogs/modmail.py @@ -918,7 +918,7 @@ async def anonadduser(self, ctx, *users_arg: Union[discord.Member, discord.Role, tag = self.bot.config["mod_tag"] if tag is None: - tag = str(get_top_hoisted_role(ctx.author)) + tag = str(get_top_role(ctx.author, self.bot.config["use_hoisted_top_role"])) name = self.bot.config["anon_username"] if name is None: name = tag @@ -1003,7 +1003,7 @@ async def anonremoveuser(self, ctx, *users_arg: Union[discord.Member, discord.Ro tag = self.bot.config["mod_tag"] if tag is None: - tag = str(get_top_hoisted_role(ctx.author)) + tag = str(get_top_role(ctx.author, self.bot.config["use_hoisted_top_role"])) name = self.bot.config["anon_username"] if name is None: name = tag diff --git a/core/config.py b/core/config.py index d8593a02ef..e9de7d516d 100644 --- a/core/config.py +++ b/core/config.py @@ -123,6 +123,7 @@ class ConfigManager: "confirm_thread_creation_deny": "\N{NO ENTRY SIGN}", # regex "use_regex_autotrigger": False, + "use_hoisted_top_role": True, } private_keys = { @@ -209,6 +210,7 @@ class ConfigManager: "thread_show_roles", "thread_show_account_age", "thread_show_join_age", + "use_hoisted_top_role", } enums = { diff --git a/core/thread.py b/core/thread.py index 6bc7adc6cb..e20a7d1110 100644 --- a/core/thread.py +++ b/core/thread.py @@ -21,7 +21,7 @@ match_user_id, match_other_recipients, truncate, - get_top_hoisted_role, + get_top_role, create_thread_channel, get_joint_id, ) @@ -938,7 +938,7 @@ async def send( # Anonymously sending to the user. tag = self.bot.config["mod_tag"] if tag is None: - tag = str(get_top_hoisted_role(author)) + tag = str(get_top_role(author, self.bot.config["use_hoisted_top_role"])) name = self.bot.config["anon_username"] if name is None: name = tag @@ -1055,7 +1055,7 @@ async def send( elif not anonymous: mod_tag = self.bot.config["mod_tag"] if mod_tag is None: - mod_tag = str(get_top_hoisted_role(message.author)) + mod_tag = str(get_top_role(message.author, self.bot.config["use_hoisted_top_role"])) embed.set_footer(text=mod_tag) # Normal messages else: embed.set_footer(text=self.bot.config["anon_tag"]) diff --git a/core/utils.py b/core/utils.py index 0fa74e457a..44df043938 100644 --- a/core/utils.py +++ b/core/utils.py @@ -30,7 +30,7 @@ "trigger_typing", "escape_code_block", "tryint", - "get_top_hoisted_role", + "get_top_role", "get_joint_id", ] @@ -369,9 +369,11 @@ def tryint(x): return x -def get_top_hoisted_role(member: discord.Member): +def get_top_role(member: discord.Member, hoisted=True): roles = sorted(member.roles, key=lambda r: r.position, reverse=True) for role in roles: + if not hoisted: + return role if role.hoist: return role From 655e063e7ba200fcce770b4d8d3d103f236d7e1d Mon Sep 17 00:00:00 2001 From: scragly <29337040+scragly@users.noreply.github.com> Date: Tue, 7 Sep 2021 18:55:32 +1000 Subject: [PATCH 2/2] Add use_hoisted_top_role config help details. --- core/config_help.json | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/config_help.json b/core/config_help.json index 1bfd65a92b..d7bbcf5da8 100644 --- a/core/config_help.json +++ b/core/config_help.json @@ -1121,5 +1121,16 @@ "notes": [ "This configuration can only to be set through `.env` file or environment (config) variables." ] + }, + "use_hoisted_top_role": { + "default": "Yes", + "description": "Controls if only hoisted roles are evaluated when finding top role.", + "examples": [ + ], + "notes": [ + "Top role is displayed in embeds when replying or adding/removing users to a thread in the case mod_tag and anon_username are not set.", + "If this configuration is enabled, only roles that are hoisted (displayed seperately in member list) will be used. If a user has no hoisted roles, it will return 'None'.", + "If you would like to display the top role of a user regardless of if it's hoisted or not, disable `use_hoisted_top_role`." + ] } }