From 5d30f551fd50e30893efbda6c24021f1e0aa8fe6 Mon Sep 17 00:00:00 2001 From: edraft Date: Tue, 7 Jan 2025 18:24:04 +0100 Subject: [PATCH] Data integrity check for realms #481 --- .../service/data_integrity_service.py | 6 +++ bot/src/modules/realms/realm_utils.py | 37 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/bot/src/bot_core/service/data_integrity_service.py b/bot/src/bot_core/service/data_integrity_service.py index e40bf70d..e3d2bbf1 100644 --- a/bot/src/bot_core/service/data_integrity_service.py +++ b/bot/src/bot_core/service/data_integrity_service.py @@ -27,6 +27,7 @@ from bot_data.model.user_joined_server import UserJoinedServer from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel from bot_data.service.user_repository_service import ServerRepositoryABC from modules.achievements.achievement_service import AchievementService +from modules.realms.realm_utils import RealmUtils class DataIntegrityService: @@ -46,6 +47,7 @@ class DataIntegrityService: achievement_service: AchievementService, client_utils: ClientUtilsABC, dtp: DateTimeOffsetPipe, + ream_utils: RealmUtils, ): self._config = config @@ -62,6 +64,7 @@ class DataIntegrityService: self._achievements = achievement_service self._client_utils = client_utils self._dtp = dtp + self._realm_utils = ream_utils self._is_for_shutdown = False @@ -96,6 +99,9 @@ class DataIntegrityService: self._logger.debug(__name__, f"Start check for user got achievements: {g.id}@{m.id}") await self._check_for_user_achievements(u) + self._logger.debug(__name__, f"Start check for user got achievements: {g.id}@{m.id}") + await self._realm_utils.check_integrity(s, g) + for m in [m for m in g.members if m.bot]: u = self._users.find_user_by_discord_id_and_server_id(m.id, s.id) if u is None: diff --git a/bot/src/modules/realms/realm_utils.py b/bot/src/modules/realms/realm_utils.py index c20fb7c1..8b055f5e 100644 --- a/bot/src/modules/realms/realm_utils.py +++ b/bot/src/modules/realms/realm_utils.py @@ -2,14 +2,17 @@ from typing import Optional, Union import discord from cpl_discord.container import Role, CategoryChannel +from cpl_discord.service import DiscordBotServiceABC from discord import Guild, Member from discord.abc import GuildChannel from bot_data.abc.realm_repository_abc import RealmRepositoryABC +from bot_data.model.server import Server class RealmUtils: - def __init__(self, realms: RealmRepositoryABC): + def __init__(self, bot: DiscordBotServiceABC, realms: RealmRepositoryABC): + self._bot = bot self._realms = realms @staticmethod @@ -92,3 +95,35 @@ class RealmUtils: .select(lambda x: x.user.discord_id) .contains(member_id) ) + + async def check_integrity(self, server: Server, guild: Guild): + for realm in self._realms.get_realms_by_server_id(server.id): + category = self.find_category_by_name(guild, realm.name) + if category is None: + await self.create_category(guild, realm.name, realm.role, None) + + role = self.find_role_by_name(guild, realm.name) + if role is None: + await guild.create_role(name=realm.name) + + role = self.get_role_by_name(guild, realm.name) + category = self.get_category_by_name(guild, realm.name) + for mod in self._realms.get_realm_moderators_by_realm_id(realm.id): + member = guild.get_member(mod.user.discord_id) + if member is None: + continue + + if member.get_role(role.id) is None: + await member.add_roles(role) + + if member not in category.overwrites: + await self.assign_moderator(category, member) + + mods = self._realms.get_realm_moderators_by_realm_id(realm.id) + mod_user_ids = mods.select(lambda x: x.user.discord_id) + for mod in category.overwrites: + if isinstance(mod, discord.Role): + continue + + if mod.id not in mod_user_ids: + await self.remove_moderator(category, mod)