Improved logic to create roles for levels by default #25

This commit is contained in:
Sven Heidemann 2022-11-06 19:18:11 +01:00
parent 666c527730
commit 861a847088
2 changed files with 56 additions and 13 deletions

View File

@ -8,7 +8,7 @@
"LoggingSettings": {
"Path": "logs/",
"Filename": "bot.log",
"ConsoleLogLevel": "TRACE",
"ConsoleLogLevel": "DEBUG",
"FileLogLevel": "TRACE"
},
"BotLoggingSettings": {

View File

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