diff --git a/kdb-bot/src/bot/config/appsettings.edrafts-lapi.json b/kdb-bot/src/bot/config/appsettings.edrafts-lapi.json index 2e9e52a3..e912b148 100644 --- a/kdb-bot/src/bot/config/appsettings.edrafts-lapi.json +++ b/kdb-bot/src/bot/config/appsettings.edrafts-lapi.json @@ -8,7 +8,7 @@ "LoggingSettings": { "Path": "logs/", "Filename": "bot.log", - "ConsoleLogLevel": "TRACE", + "ConsoleLogLevel": "DEBUG", "FileLogLevel": "TRACE" }, "BotLoggingSettings": { @@ -76,7 +76,12 @@ }, "BootLog": { "910199451145076828": { - "LoginMessageChannelId": "910199452915093588" + "LoginMessageChannelId": 910199452915093588 + } + }, + "Level": { + "910199451145076828": { + "ChangedLevelNotificationChannelId": 910199452667637892 } }, "Permission": { diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 4812a5c1..64fb23b1 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -175,9 +175,6 @@ }, "level": { "new_level_message": "<@{}> ist nun Level {}", - "seeding_started": "Levelsystem wird neu geladen.", - "seeding_failed": "Levelsystem konnte nicht neu geladen werden.", - "seeding_finished": "Levelsystem wurde erfolgreich neu geladen.", "error": { "nothing_found": "Keine Level Einträge gefunden.", "level_with_name_already_exists": "Ein Level mit dem Namen {} existiert bereits!", @@ -191,13 +188,22 @@ "permission_int": "Berechtigungen" }, "create": { - "created": "Level {} mit Berechtigungen {} wurde erstellt." + "created": "Level {} mit Berechtigungen {} wurde erstellt.", + "seeding_started": "Levelsystem wird neu geladen.", + "seeding_failed": "Levelsystem konnte nicht neu geladen werden.", + "seeding_finished": "Levelsystem wurde Erfolgreich neu geladen." }, "remove": { "success": "Level {} wurde entfernt :D", "error": { "not_found": "Level {} nicht gefunden!" } + }, + "down": { + "already_first": "{} hat bereits das erste Level.", + "success": "{} wurde auf Level {} runtergesetzt :)", + "failed": "{} konnte nicht runtergesetzt werden :(", + "created": "Level {} mit Berechtigungen {} wurde erstellt." } }, "database": {}, diff --git a/kdb-bot/src/modules/level/command/level_group.py b/kdb-bot/src/modules/level/command/level_group.py index 7658da5a..f8e519c2 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -15,6 +15,7 @@ from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.logging.command_logger import CommandLogger from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.level import Level from modules.level.level_seeder import LevelSeeder from modules.level.service.level_service import LevelService @@ -31,9 +32,10 @@ class LevelGroup(DiscordCommandABC): client_utils: ClientUtilsServiceABC, permission_service: PermissionServiceABC, translate: TranslatePipe, - db_context: DatabaseContextABC, + db: DatabaseContextABC, levels: LevelRepositoryABC, servers: ServerRepositoryABC, + users: UserRepositoryABC, level_service: LevelService, level_seeder: LevelSeeder, ): @@ -45,25 +47,16 @@ class LevelGroup(DiscordCommandABC): self._client_utils = client_utils self._permissions = permission_service self._t = translate - self._db_context = db_context + self._db = db self._levels = levels self._servers = servers + self._users = users self._level_service = level_service self._level_seeder = level_seeder self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') - async def _seed_levels(self, channel: discord.TextChannel): - # send message to ctx.channel because send_ctx_msg resolves ctx - try: - await self._message_service.send_channel_message(channel, self._t.transform('modules.level.seeding_started')) - await self._level_seeder.seed() - await self._message_service.send_channel_message(channel, self._t.transform('modules.level.seeding_finished')) - except Exception as e: - self._logger.error(__name__, f'Level seeding failed', e) - await self._message_service.send_channel_message(channel, self._t.transform('modules.level.seeding_failed')) - @commands.hybrid_group() @commands.guild_only() async def level(self, ctx: Context): @@ -115,14 +108,6 @@ class LevelGroup(DiscordCommandABC): async def create(self, ctx: Context, name: str, color: str, min_xp: int, permissions: int): self._logger.debug(__name__, f'Received command level create {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - - if not self._permissions.is_member_admin(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command level remove') - return - try: color = hex(discord.Colour.from_str(color).value) except Exception as e: @@ -135,6 +120,9 @@ class LevelGroup(DiscordCommandABC): self._logger.error(__name__, f'Error parsing permissions {permissions}', e) return + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + if ctx.guild is None: return @@ -152,13 +140,21 @@ class LevelGroup(DiscordCommandABC): else: try: self._levels.add_level(level) - self._db_context.save_changes() + self._db.save_changes() self._logger.info(__name__, f'Saved level {name} with color {color}, min_xp {min_xp} and permissions {permissions}') except Exception as e: self._logger.error(__name__, f'Could not save level {name} with color {color}, min_xp {min_xp} and permissions {permissions}', e) else: await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.create.created').format(name, permissions)) - await self._seed_levels(ctx.channel) + + # send message to ctx.channel because send_ctx_msg resolves ctx + try: + await self._message_service.send_channel_message(ctx.channel, self._t.transform('modules.level.create.seeding_started')) + await self._level_seeder.seed() + await self._message_service.send_channel_message(ctx.channel, self._t.transform('modules.level.create.seeding_finished')) + except Exception as e: + self._logger.error(__name__, f'Level seeding failed', e) + await self._message_service.send_channel_message(ctx.channel, self._t.transform('modules.level.create.seeding_failed')) self._logger.trace(__name__, f'Finished command level create') @@ -219,7 +215,7 @@ class LevelGroup(DiscordCommandABC): try: self._levels.delete_level(level_from_db) - self._db_context.save_changes() + self._db.save_changes() guild: Guild = self._bot.guilds.where(lambda g: g == ctx.guild).single() role: Role = guild.roles.where(lambda r: r.name == level).single_or_default() if role is not None: @@ -238,3 +234,41 @@ class LevelGroup(DiscordCommandABC): server = self._servers.get_server_by_discord_id(interaction.guild.id) levels = self._levels.get_levels_by_server_id(server.server_id).select(lambda l: l.name) return [app_commands.Choice(name=level, value=level) for level in levels] + + @level.command() + @commands.guild_only() + async def down(self, ctx: Context, member: discord.Member): + self._logger.debug(__name__, f'Received command level down {ctx} {member}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + + if not self._permissions.is_member_moderator(ctx.author): + await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) + self._logger.trace(__name__, f'Finished command level list') + return + + if ctx.guild is None: + return + + server = self._servers.get_server_by_discord_id(ctx.guild.id) + user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id) + level = self._level_service.get_level(user) + levels = self._levels.get_levels_by_server_id(server.server_id).order_by(lambda l: l.min_xp) + + if level == levels.first(): + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.down.already_first').format(member.name)) + self._logger.trace(__name__, f'Finished command level list') + return + + try: + new_level = levels.where(lambda l: l.min_xp < level.min_xp).last() + user.xp = new_level.min_xp + self._users.update_user(user) + self._db.save_changes() + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.down.success').format(member.name, new_level.name)) + await self._level_service.set_level(user) + except Exception as e: + self._logger.error(__name__, f'Cannot level down {member.name} with level {level.name}', e) + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.down.failed').format(member.name)) + + self._logger.trace(__name__, f'Finished command level down') diff --git a/kdb-bot/src/modules/level/level_seeder.py b/kdb-bot/src/modules/level/level_seeder.py index 870a7b30..2b4fc643 100644 --- a/kdb-bot/src/modules/level/level_seeder.py +++ b/kdb-bot/src/modules/level/level_seeder.py @@ -29,14 +29,14 @@ class LevelSeeder(DataSeederABC): async def _create_level(self, level: Level, guild: Guild, server: Server): level.server = server try: - if guild.roles.where(lambda r: r.name == level.name).first_or_default() is not None: - return + if guild.roles.where(lambda r: r.name == level.name).first_or_default() is None: + await guild.create_role(name=level.name, colour=Colour(int(level.color, 16)), hoist=False, mentionable=True, permissions=Permissions(level.permissions)) + self._logger.debug(__name__, f'Created role {level.name}') - 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}') - - if self._levels.find_levels_by_server_id(server.server_id).where(lambda l: l == level).first_or_default() is not None: + levels = self._levels.find_levels_by_server_id(server.server_id) + if levels is None or levels.where(lambda l: l == level).first_or_default() is None: self._levels.add_level(level) + self._logger.debug(__name__, f'Saved level {level.name}') except discord.errors.Forbidden as e: self._logger.error(__name__, f'Creating level failed', e) level.permissions = 0