Data integrity check for realms #481

This commit is contained in:
Sven Heidemann 2025-01-07 18:24:04 +01:00
parent 6e74e9b190
commit 5d30f551fd
2 changed files with 42 additions and 1 deletions

View File

@ -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:

View File

@ -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)