Improved permission role handling

This commit is contained in:
Sven Heidemann 2021-12-29 21:01:58 +01:00
parent a8663b8d54
commit b352987dcc
2 changed files with 41 additions and 22 deletions

View File

@ -34,4 +34,4 @@ class Permission(ModuleABC, OnReadyABC, OnMemberUpdateABC):
self._logger.debug(__name__, f'Module {type(self)} started') self._logger.debug(__name__, f'Module {type(self)} started')
if before.roles != after.roles: if before.roles != after.roles:
self._permission_service.on_member_update(before, after) self._permission_service.on_member_update(before, after)

View File

@ -25,6 +25,7 @@ class PermissionService(PermissionServiceABC):
def on_ready(self): def on_ready(self):
for guild in self._bot.guilds: for guild in self._bot.guilds:
guild: discord.Guild = guild guild: discord.Guild = guild
self._logger.debug(__name__, f'Validate permission settings')
settings: PermissionSettings = self._config.get_configuration(f'Permission_{guild.id}') settings: PermissionSettings = self._config.get_configuration(f'Permission_{guild.id}')
if settings is None: if settings is None:
@ -32,42 +33,60 @@ class PermissionService(PermissionServiceABC):
return return
self._admin_role_ids[guild.id] = settings.admin_roles 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_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: for role in guild.roles:
role: discord.Role = role role: discord.Role = role
if role.id in self._admin_role_ids: if role.id in self._admin_role_ids[guild.id]:
self._admin_roles[guild.id].append(role) admin_roles.append(role)
self._logger.trace(__name__, f'Added admin role {role}')
for member in role.members: 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: if role.id in self._moderator_role_ids[guild.id]:
self._moderator_roles[guild.id].append(role) mod_roles.append(role)
self._logger.trace(__name__, f'Added moderator role {role}')
for member in role.members: 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): def on_member_update(self, before: discord.Member, after: discord.Member):
g_id = after.guild.id 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]: for admin_role in self._admin_roles[g_id]:
self._moderators[g_id].remove(after) 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]: for moderator_role in self._moderator_roles[g_id]:
self._moderators[g_id].append(after) 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]: def get_admin_role_ids(self, g_id: int) -> list[int]:
return self._admin_role_ids[g_id] return self._admin_role_ids[g_id]