From e5eb50a3cbff82f8bfda6c94c96ce09cefdac663 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 8 Nov 2022 18:49:10 +0100 Subject: [PATCH 1/5] Fixed level seeder #26 --- kdb-bot/src/modules/level/level_seeder.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kdb-bot/src/modules/level/level_seeder.py b/kdb-bot/src/modules/level/level_seeder.py index 2b4fc643..8e6eb9e7 100644 --- a/kdb-bot/src/modules/level/level_seeder.py +++ b/kdb-bot/src/modules/level/level_seeder.py @@ -1,7 +1,6 @@ import discord -from cpl_discord.container import Guild, Role +from cpl_discord.container import Guild from cpl_discord.service import DiscordBotServiceABC -from cpl_query.extension import List from discord import Permissions, Colour from bot_core.logging.database_logger import DatabaseLogger @@ -34,7 +33,7 @@ class LevelSeeder(DataSeederABC): self._logger.debug(__name__, f'Created role {level.name}') 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: + if levels is None or levels.where(lambda l: l.name == level.name).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: -- 2.45.2 From c666ceb51ed0b1964b969d39836384cc8bde76db Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 8 Nov 2022 18:49:18 +0100 Subject: [PATCH 2/5] Added level set command #26 --- kdb-bot/src/bot/translation/de.json | 5 ++ .../src/modules/level/command/level_group.py | 51 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 146ea6c7..ea8eb111 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -202,6 +202,11 @@ "already_last": "{} hat bereits das höchste Level.", "success": "{} wurde auf Level {} hochgesetzt :)", "failed": "{} konnte nicht hochgesetzt werden :(" + }, + "set": { + "already_level": "{} hat bereits das Level {} :/", + "success": "{} ist nun Level {} :)", + "failed": "Das Level von {} konnte nicht auf {} gesetzt 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 8175c976..241971b9 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -268,3 +268,54 @@ class LevelGroup(DiscordCommandABC): 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') + + @level.command() + @commands.guild_only() + async def set(self, ctx: Context, member: discord.Member, level: str): + 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) + current_level = self._level_service.get_level(user) + new_level = self._levels.get_levels_by_server_id(server.server_id).where(lambda l: l.name == level).single_or_default() + + if new_level is None: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.set.failed').format(member.name, level)) + self._logger.trace(__name__, f'Finished command level set') + return + + if current_level.name == level: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.set.already_level').format(member.name, level)) + self._logger.trace(__name__, f'Finished command level set') + return + + try: + 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.set.success').format(member.name, new_level.name)) + await self._level_service.set_level(user) + except Exception as e: + self._logger.error(__name__, f'Cannot set level {level} for {member.name}', e) + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.set.failed').format(member.name)) + + self._logger.trace(__name__, f'Finished command level set') + + @set.autocomplete('level') + async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + 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] -- 2.45.2 From 455c1e7023c6b3efe0a715a389c115c81b271ef5 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 9 Nov 2022 19:26:41 +0100 Subject: [PATCH 3/5] Fixed translation #26 --- kdb-bot/src/bot/translation/de.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index ea8eb111..ecc7359a 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -193,6 +193,12 @@ "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 :)", -- 2.45.2 From c632ad51d4e2a614e1187a03f6ccaa822f87f741 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 9 Nov 2022 19:33:45 +0100 Subject: [PATCH 4/5] Fixed stuff caused by merges #26 --- kdb-bot/src/bot/translation/de.json | 10 +++++----- .../src/modules/level/command/level_group.py | 20 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index ecc7359a..0aa1395e 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -62,7 +62,7 @@ "gold": "Gold", "green": "Grün", "greyple": "Graugrün", - "light_gray": "Hellgrau", + "light_grey": "Hellgrau", "magenta": "Magenta", "orange": "Orange", "purple": "Violett", @@ -175,6 +175,9 @@ }, "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!", @@ -188,10 +191,7 @@ "permission_int": "Berechtigungen" }, "create": { - "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." + "created": "Level {} mit Berechtigungen {} wurde erstellt." }, "remove": { "success": "Level {} wurde entfernt :D", diff --git a/kdb-bot/src/modules/level/command/level_group.py b/kdb-bot/src/modules/level/command/level_group.py index da59dbb6..c024dbe3 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -57,6 +57,16 @@ class LevelGroup(DiscordCommandABC): 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): @@ -146,15 +156,7 @@ class LevelGroup(DiscordCommandABC): 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)) - - # 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')) + await self._seed_levels(ctx.channel) self._logger.trace(__name__, f'Finished command level create') -- 2.45.2 From a5ef2551e8c7521589d153ab5e5d540d20d68aa2 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 9 Nov 2022 19:50:57 +0100 Subject: [PATCH 5/5] Improved error translation #26 --- kdb-bot/src/bot/translation/de.json | 3 ++- kdb-bot/src/modules/level/command/level_group.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 0aa1395e..45281393 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -212,7 +212,8 @@ "set": { "already_level": "{} hat bereits das Level {} :/", "success": "{} ist nun Level {} :)", - "failed": "Das Level von {} konnte nicht auf {} gesetzt werden :(" + "failed": "Das Level von {} konnte nicht auf {} gesetzt werden :(", + "not_found": "Das Level {} konnte nicht gefunden 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 c024dbe3..32ccb708 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -343,7 +343,7 @@ class LevelGroup(DiscordCommandABC): new_level = self._levels.get_levels_by_server_id(server.server_id).where(lambda l: l.name == level).single_or_default() if new_level is None: - await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.set.failed').format(member.name, level)) + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.set.not_found').format(level)) self._logger.trace(__name__, f'Finished command level set') return -- 2.45.2