From 83764b3cee3f53c060afc64e1e9fc1534dad3796 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 13 Jan 2024 13:04:41 +0100 Subject: [PATCH] Added logic to remove short role name for all members by deleting --- bot/src/bot/config | 2 +- .../mutations/short_role_name_mutation.py | 4 ++ .../service/short_role_name_service.py | 39 +++++++++++++++++-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/bot/src/bot/config b/bot/src/bot/config index be5b15f2..eeebd13f 160000 --- a/bot/src/bot/config +++ b/bot/src/bot/config @@ -1 +1 @@ -Subproject commit be5b15f227e7b2b12cc73c0edd7f6fa95dbdd52f +Subproject commit eeebd13f80c6ceecc922ede5771e55212a884019 diff --git a/bot/src/bot_graphql/mutations/short_role_name_mutation.py b/bot/src/bot_graphql/mutations/short_role_name_mutation.py index 14ae37a0..ec5ee9b2 100644 --- a/bot/src/bot_graphql/mutations/short_role_name_mutation.py +++ b/bot/src/bot_graphql/mutations/short_role_name_mutation.py @@ -7,6 +7,7 @@ from bot_data.model.short_role_name import ShortRoleName from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC from bot_core.service.permission_service import PermissionService +from modules.short_role_name.service.short_role_name_service import ShortRoleNameService class ShortRoleNameMutation(QueryABC): @@ -17,6 +18,7 @@ class ShortRoleNameMutation(QueryABC): bot: DiscordBotServiceABC, db: DatabaseContextABC, permissions: PermissionService, + short_role_name_service: ShortRoleNameService, ): QueryABC.__init__(self, "ShortRoleNameMutation") @@ -25,6 +27,7 @@ class ShortRoleNameMutation(QueryABC): self._bot = bot self._db = db self._permissions = permissions + self._short_role_name_service = short_role_name_service self.set_field("createShortRoleName", self.resolve_create_short_role_name) self.set_field("updateShortRoleName", self.resolve_update_short_role_name) @@ -79,6 +82,7 @@ class ShortRoleNameMutation(QueryABC): short_role_name = self._short_role_names.get_short_role_name_by_id(id) self._can_user_mutate_data(short_role_name.server, UserRoleEnum.admin) + self._bot.loop.create_task(self._short_role_name_service.remove_short_role_from_members(short_role_name)) self._short_role_names.delete_short_role_name(short_role_name) self._db.save_changes() diff --git a/bot/src/modules/short_role_name/service/short_role_name_service.py b/bot/src/modules/short_role_name/service/short_role_name_service.py index 5a2e8af8..2063fdd8 100644 --- a/bot/src/modules/short_role_name/service/short_role_name_service.py +++ b/bot/src/modules/short_role_name/service/short_role_name_service.py @@ -30,6 +30,41 @@ class ShortRoleNameService: self._server = server self._short_role_names = short_role_names + def _remove_short_role_name_from_name(self, short_role_name: ShortRoleName, name: str) -> str: + return ( + name.replace(f" [{short_role_name.short_name}]", "") + .replace(f"[{short_role_name.short_name}] ", "") + .replace(f"[{short_role_name.short_name}]", "") + ) + + async def remove_short_role_from_members(self, short_role_name: ShortRoleName): + guild = self._bot.get_guild(short_role_name.server.discord_id) + settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild.id}") + for member in guild.members: + try: + member_roles = List(discord.Role, member.roles) + highest_role: Optional[discord.Role] = ( + member_roles.last_or_default() if settings.short_role_name_only_set_highest_role else None + ) + member_role_ids = member_roles.select(lambda x: x.id) + + if ( + short_role_name.role_id not in member_role_ids + or highest_role + and highest_role.id != short_role_name.role_id + ): + continue + + self._logger.debug(__name__, f"Update member {member.id}") + await member.edit( + nick=self._remove_short_role_name_from_name( + short_role_name, member.nick if member.nick is not None else member.name + ) + ) + self._logger.debug(__name__, f"Updated member {member.id} {member.name}") + except Exception as e: + self._logger.error(__name__, f"Renaming member {member.name} failed", e) + async def check_short_role_names(self, member: discord.Member): self._logger.debug(__name__, f"Started short role name check for {member.id}") settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}") @@ -53,9 +88,7 @@ class ShortRoleNameService: for short_role_name in self._short_role_names.get_short_role_names_by_server_id(server.id): short_role_name: ShortRoleName = short_role_name - new_nick = new_nick.replace(f" [{short_role_name.short_name}]", "") - new_nick = new_nick.replace(f"[{short_role_name.short_name}] ", "") - new_nick = new_nick.replace(f"[{short_role_name.short_name}]", "") + new_nick = self._remove_short_role_name_from_name(short_role_name, new_nick) if ( short_role_name.role_id not in member_role_ids