From b352987dccf3ec10219fdb261b259e40e7449f53 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 29 Dec 2021 21:01:58 +0100 Subject: [PATCH] Improved permission role handling --- src/modules/permission/permission.py | 2 +- .../permission/service/permission_service.py | 61 ++++++++++++------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/modules/permission/permission.py b/src/modules/permission/permission.py index 1f799d0..9a4dcee 100644 --- a/src/modules/permission/permission.py +++ b/src/modules/permission/permission.py @@ -34,4 +34,4 @@ class Permission(ModuleABC, OnReadyABC, OnMemberUpdateABC): self._logger.debug(__name__, f'Module {type(self)} started') if before.roles != after.roles: - self._permission_service.on_member_update(before, after) \ No newline at end of file + self._permission_service.on_member_update(before, after) diff --git a/src/modules/permission/service/permission_service.py b/src/modules/permission/service/permission_service.py index c0b9158..6172625 100644 --- a/src/modules/permission/service/permission_service.py +++ b/src/modules/permission/service/permission_service.py @@ -25,6 +25,7 @@ class PermissionService(PermissionServiceABC): def on_ready(self): for guild in self._bot.guilds: guild: discord.Guild = guild + self._logger.debug(__name__, f'Validate permission settings') settings: PermissionSettings = self._config.get_configuration(f'Permission_{guild.id}') if settings is None: @@ -32,42 +33,60 @@ class PermissionService(PermissionServiceABC): return self._admin_role_ids[guild.id] = settings.admin_roles - self._admin_roles[guild.id] = [] - self._admins[guild.id] = [] - self._moderator_role_ids[guild.id] = settings.moderator_roles - self._moderator_roles[guild.id] = [] - self._moderators[guild.id] = [] + + admin_roles = [] + admins = [] + + mod_roles = [] + mods = [] for role in guild.roles: role: discord.Role = role - if role.id in self._admin_role_ids: - self._admin_roles[guild.id].append(role) + if role.id in self._admin_role_ids[guild.id]: + admin_roles.append(role) + self._logger.trace(__name__, f'Added admin role {role}') for member in role.members: - self._admins[guild.id].append(member) + admins.append(member) + self._logger.trace(__name__, f'Added admin {member}') - if role.id in self._moderator_role_ids: - self._moderator_roles[guild.id].append(role) + if role.id in self._moderator_role_ids[guild.id]: + mod_roles.append(role) + self._logger.trace(__name__, f'Added moderator role {role}') for member in role.members: - self._moderators[guild.id].append(member) + mods.append(member) + self._logger.trace(__name__, f'Added moderator {member}') + + + self._admin_roles[guild.id] = admin_roles + self._admins[guild.id] = admins + self._moderator_roles[guild.id] = mod_roles + self._moderators[guild.id] = mods def on_member_update(self, before: discord.Member, after: discord.Member): g_id = after.guild.id - - if before in self._admin_roles[g_id] and after not in self._admin_roles[g_id]: - self._admins[g_id].remove(after) - - elif before not in self._admin_roles[g_id] and after in self._admin_roles[g_id]: - self._admins[g_id].append(after) - if before in self._moderator_roles[g_id] and after not in self._moderator_roles[g_id]: - self._moderators[g_id].remove(after) + for admin_role in self._admin_roles[g_id]: + if admin_role in before.roles and admin_role not in after.roles: + self._admins[g_id].remove(after) + self._logger.trace(__name__, f'Removed {after.id} from admins') + + elif admin_role in after.roles and admin_role not in before.roles: + self._admins[g_id].append(after) + self._logger.trace(__name__, f'Added {after.id} to admins') - elif before not in self._moderator_roles[g_id] and after in self._moderator_roles[g_id]: - self._moderators[g_id].append(after) + for moderator_role in self._moderator_roles[g_id]: + if moderator_role in before.roles and moderator_role not in after.roles: + self._moderators[g_id].remove(after) + self._logger.trace(__name__, f'Removed {after.id} from moderators') + + elif moderator_role in after.roles and moderator_role not in before.roles: + self._moderators[g_id].append(after) + self._logger.trace(__name__, f'Added {after.id} to moderators') + def get_admin_role_ids(self, g_id: int) -> list[int]: return self._admin_role_ids[g_id]