diff --git a/kdb-bot/src/bot/config/appsettings.edrafts-pc.json b/kdb-bot/src/bot/config/appsettings.edrafts-pc.json index 2e9e52a3d7..9deca01737 100644 --- a/kdb-bot/src/bot/config/appsettings.edrafts-pc.json +++ b/kdb-bot/src/bot/config/appsettings.edrafts-pc.json @@ -8,7 +8,7 @@ "LoggingSettings": { "Path": "logs/", "Filename": "bot.log", - "ConsoleLogLevel": "TRACE", + "ConsoleLogLevel": "DEBUG", "FileLogLevel": "TRACE" }, "BotLoggingSettings": { diff --git a/kdb-bot/src/modules/level/level_seeder.py b/kdb-bot/src/modules/level/level_seeder.py index a8bcd5db94..8193f47426 100644 --- a/kdb-bot/src/modules/level/level_seeder.py +++ b/kdb-bot/src/modules/level/level_seeder.py @@ -1,9 +1,13 @@ +from cpl_discord.container import Guild, Role from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List from discord import Permissions, Colour from bot_core.logging.database_logger import DatabaseLogger from bot_data.abc.data_seeder_abc import DataSeederABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.level import Level +from bot_data.model.server import Server from bot_data.service.level_repository_service import LevelRepositoryService from modules.level.configuration.level_settings import LevelSettings @@ -19,8 +23,39 @@ class LevelSeeder(DataSeederABC): self._servers = servers self._bot = bot + async def _create_level(self, level: Level, guild: Guild, server: Server): + try: + if guild.roles.where(lambda r: r.name == level.name).count() != 0: + return + + await guild.create_role(name=level.name, colour=Colour(int(level.color, 16)), hoist=False, mentionable=True, permissions=Permissions(level.permissions)) + self._logger.info(__name__, f'Created level {level.name}') + level.server = server + self._levels.add_level(level) + except Exception as e: + self._logger.error(__name__, f'Creating level failed', e) + async def seed(self): - if self._levels.get_levels().count() > 0: + created_default = False + for guild in self._bot.guilds: + server = self._servers.find_server_by_discord_id(guild.id) + if server is None: + continue + + levels = self._levels.find_levels_by_server_id(server.server_id) + if levels is not None and levels.count() > 0: + for level in levels: + await self._create_level(level, guild, server) + + continue + + for level in self._default_levels: + created_default = True + await self._create_level(level, guild, server) + + self._logger.debug(__name__, f'Seeded default levels') + + if created_default: return for guild in self._bot.guilds: @@ -29,18 +64,26 @@ class LevelSeeder(DataSeederABC): continue levels = self._levels.find_levels_by_server_id(server.server_id) - if levels is not None and levels.count() > 0: - continue + roles: List[Role] = List(Role) + for role in guild.roles.order_by_descending(lambda r: r.position): + if levels.where(lambda l: l.name == role.name).count() == 0: + continue + + roles.append(role) + + levels = levels.order_by(lambda l: l.min_xp) + position_below_levels = roles.where(lambda r: r.name == levels.order_by(lambda l: l.min_xp).first().name).single().position - 1 + for role in roles: + new_position = position_below_levels + (levels.index(levels.where(lambda l: l.name == role.name).single()) - 1) + if new_position <= 0: + above_role: Role = roles.where(lambda r: r.position == 1).single() + await above_role.edit(position=above_role.position + 1) + new_position = 1 - for level in self._default_levels: try: - if guild.roles.where(lambda r: r.name == level.name).count() == 0: - await guild.create_role(name=level.name, colour=Colour(int(level.color, 16)), hoist=False, mentionable=True, permissions=Permissions(level.permissions)) - - self._logger.info(__name__, f'Created default level {level.name}') - level.server = server - self._levels.add_level(level) + self._logger.debug(__name__, f'Moved {role.name} from {role.position} to {new_position}') + await role.edit(position=new_position) except Exception as e: - self._logger.error(__name__, f'Creating default role failed', e) + self._logger.error(__name__, f'Cannot change position of {role.name}', e) - self._logger.debug(__name__, f'Seeded default levels') + self._logger.debug(__name__, f'Checked role order')