From f0f23163e45129070e47d059812ef3b3928f336b Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 8 Nov 2022 18:01:04 +0100 Subject: [PATCH] Added level down command #27 --- kdb-bot/src/bot/translation/de.json | 5 ++ .../src/modules/level/command/level_group.py | 48 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index e66eeb02..146ea6c7 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -197,6 +197,11 @@ "already_first": "{} hat bereits das erste Level.", "success": "{} wurde auf Level {} runtergesetzt :)", "failed": "{} konnte nicht runtergesetzt werden :(" + }, + "up": { + "already_last": "{} hat bereits das höchste Level.", + "success": "{} wurde auf Level {} hochgesetzt :)", + "failed": "{} konnte nicht hochgesetzt werden :(" } }, "database": {}, diff --git a/kdb-bot/src/modules/level/command/level_group.py b/kdb-bot/src/modules/level/command/level_group.py index 26d03045..8175c976 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -196,12 +196,15 @@ class LevelGroup(DiscordCommandABC): 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') + self._logger.trace(__name__, f'Finished command level down') return if ctx.guild is None: return + if member.bot: + 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) @@ -209,7 +212,7 @@ class LevelGroup(DiscordCommandABC): 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') + self._logger.trace(__name__, f'Finished command level down') return try: @@ -224,3 +227,44 @@ class LevelGroup(DiscordCommandABC): 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') + + @level.command() + @commands.guild_only() + async def up(self, ctx: Context, member: discord.Member): + self._logger.debug(__name__, f'Received command level up {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 up') + return + + if ctx.guild is None: + return + + if member.bot: + 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.name == levels.last().name: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.up.already_last').format(member.name)) + self._logger.trace(__name__, f'Finished command level up') + return + + try: + new_level = levels.where(lambda l: l.min_xp > level.min_xp).first() + 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.up.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 up {member.name} with level {level.name}', e) + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.up.failed').format(member.name)) + + self._logger.trace(__name__, f'Finished command level up') -- 2.45.2