Fixed level stuff #90

This commit is contained in:
Sven Heidemann 2022-11-07 20:35:24 +01:00
parent b2397fcc2f
commit 6a18b15447
2 changed files with 11 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import discord
from cpl_discord.container import Guild, Role from cpl_discord.container import Guild, Role
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List from cpl_query.extension import List
@ -26,14 +27,20 @@ class LevelSeeder(DataSeederABC):
self._default_levels = levels.levels.order_by_descending(lambda l: l.min_xp) self._default_levels = levels.levels.order_by_descending(lambda l: l.min_xp)
async def _create_level(self, level: Level, guild: Guild, server: Server): async def _create_level(self, level: Level, guild: Guild, server: Server):
level.server = server
try: try:
if guild.roles.where(lambda r: r.name == level.name).first_or_default() is not None: if guild.roles.where(lambda r: r.name == level.name).first_or_default() is not None:
return return
await guild.create_role(name=level.name, colour=Colour(int(level.color, 16)), hoist=False, mentionable=True, permissions=Permissions(level.permissions)) 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}') self._logger.info(__name__, f'Created level {level.name}')
level.server = server
if self._levels.find_levels_by_server_id(server.server_id).where(lambda l: l == level).first_or_default() is not None:
self._levels.add_level(level) self._levels.add_level(level)
except discord.errors.Forbidden as e:
self._logger.error(__name__, f'Creating level failed', e)
level.permissions = 0
self._levels.update_level(level)
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Creating level failed', e) self._logger.error(__name__, f'Creating level failed', e)
@ -66,6 +73,7 @@ class LevelSeeder(DataSeederABC):
if created_default: if created_default:
continue continue
levels = levels.order_by_descending(lambda l: l.min_xp)
position_above_levels = guild.roles.where(lambda r: r.name == self._level_header).single().position position_above_levels = guild.roles.where(lambda r: r.name == self._level_header).single().position
for role in guild.roles.order_by_descending(lambda r: r.position): for role in guild.roles.order_by_descending(lambda r: r.position):
if levels.where(lambda l: l.name == role.name).count() == 0: if levels.where(lambda l: l.name == role.name).count() == 0:

View File

@ -76,7 +76,7 @@ class LevelService:
level_settings: LevelServerSettings = self._config.get_configuration(f'LevelServerSettings_{guild.id}') level_settings: LevelServerSettings = self._config.get_configuration(f'LevelServerSettings_{guild.id}')
await self._message_service.send_channel_message( await self._message_service.send_channel_message(
self._bot.get_channel(level_settings.changed_level_notification_channel), self._bot.get_channel(level_settings.changed_level_notification_channel),
self._t.transform('modules.level.new_level_message'.format(member.id, level.name)), self._t.transform('modules.level.new_level_message').format(member.id, level.name),
is_persistent=True is_persistent=True
) )