From fe32604a146ec71fe1ccb720db6e1b27d62dedf5 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 8 Nov 2022 17:49:32 +0100 Subject: [PATCH 1/3] 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') From faaadb000914575544ec75c8395e1241015fcf34 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 8 Nov 2022 17:49:42 +0100 Subject: [PATCH 2/3] Fixed level seeder #28 --- kdb-bot/src/modules/level/level_seeder.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kdb-bot/src/modules/level/level_seeder.py b/kdb-bot/src/modules/level/level_seeder.py index 870a7b3024..2b4fc64338 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 From eaae0587543a8869887a8ef2928357765dbf04f4 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 9 Nov 2022 08:51:08 +0100 Subject: [PATCH 3/3] Fixed problems since merge #28 --- kdb-bot/src/bot/translation/de.json | 2 +- kdb-bot/src/modules/level/command/level_group.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 30d45d7b91..64fb23b1de 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -202,7 +202,7 @@ "down": { "already_first": "{} hat bereits das erste Level.", "success": "{} wurde auf Level {} runtergesetzt :)", - "failed": "{} konnte nicht runtergesetzt werden :(" + "failed": "{} konnte nicht runtergesetzt werden :(", "created": "Level {} mit Berechtigungen {} wurde erstellt." } }, diff --git a/kdb-bot/src/modules/level/command/level_group.py b/kdb-bot/src/modules/level/command/level_group.py index 5adbec6068..f8e519c2f6 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -3,6 +3,7 @@ from typing import List as TList import discord from cpl_core.database.context import DatabaseContextABC from cpl_discord.command import DiscordCommandABC +from cpl_discord.container import Guild, Role from cpl_discord.service import DiscordBotServiceABC from cpl_translation import TranslatePipe from discord import app_commands @@ -14,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 @@ -30,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, ): @@ -44,9 +47,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 +140,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) @@ -211,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: