From fe32604a146ec71fe1ccb720db6e1b27d62dedf5 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 8 Nov 2022 17:49:32 +0100 Subject: [PATCH] Added level down command #28 --- .../bot/config/appsettings.edrafts-lapi.json | 9 +++- kdb-bot/src/bot/translation/de.json | 5 ++ .../src/modules/level/command/level_group.py | 47 +++++++++++++++++-- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/kdb-bot/src/bot/config/appsettings.edrafts-lapi.json b/kdb-bot/src/bot/config/appsettings.edrafts-lapi.json index 2e9e52a3d7..e912b14863 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 9287476882..e66eeb025d 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -192,6 +192,11 @@ "seeding_started": "Levelsystem wird neu geladen.", "seeding_failed": "Levelsystem konnte nicht neu geladen werden.", "seeding_finished": "Levelsystem wurde Erfolgreich neu geladen." + }, + "down": { + "already_first": "{} hat bereits das erste Level.", + "success": "{} wurde auf Level {} runtergesetzt :)", + "failed": "{} konnte nicht runtergesetzt 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 0eaf7d350e..26d03045d4 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -14,6 +14,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 @@ -30,9 +31,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, ): @@ -44,9 +46,10 @@ 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 @@ -136,7 +139,7 @@ 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) @@ -183,3 +186,41 @@ class LevelGroup(DiscordCommandABC): # value in rg format see: # https://discordpy.readthedocs.io/en/latest/api.html#discord.Colour.to_rgb return [app_commands.Choice(name=self._t.transform(f'common.colors.{color}'), value=f'rgb({code[0]}, {code[1]}, {code[2]})') for color, code in colors] + + @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')