From 44f22e6aee9c5ff2d6cf887fe578ed6dfbe97203 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 10 Nov 2022 16:33:04 +0100 Subject: [PATCH 1/3] Added level edit command #103 --- kdb-bot/src/bot/translation/de.json | 6 +- .../src/modules/level/command/level_group.py | 116 ++++++++++++++---- 2 files changed, 96 insertions(+), 26 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 606e89c6..7b87852d 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -191,7 +191,11 @@ "permission_int": "Berechtigungen" }, "create": { - "created": "Level {} mit Berechtigungen {} wurde erstellt." + "created": "Level {} mit Berechtigungen {} wurde erstellt :D" + }, + "edit": { + "edited": "Level {} wurde bearbeitet :D", + "not_found": "Level {} nicht gefunden!" }, "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 7a49593c..00e4bd6f 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -55,6 +55,31 @@ class LevelGroup(DiscordCommandABC): self._level_service = level_service self._level_seeder = level_seeder + self._colors = [ + ('blue', discord.Colour.blue().to_rgb()), + ('dark_blue', discord.Colour.dark_blue().to_rgb()), + ('dark_gold', discord.Colour.dark_gold().to_rgb()), + ('dark_gray', discord.Colour.dark_gray().to_rgb()), + ('dark_green', discord.Colour.dark_green().to_rgb()), + ('dark_grey', discord.Colour.dark_grey().to_rgb()), + ('dark_magenta', discord.Colour.dark_magenta().to_rgb()), + ('dark_orange', discord.Colour.dark_orange().to_rgb()), + ('dark_purple', discord.Colour.dark_purple().to_rgb()), + ('dark_red', discord.Colour.dark_red().to_rgb()), + ('dark_teal', discord.Colour.dark_teal().to_rgb()), + ('default', discord.Colour.default().to_rgb()), + ('gold', discord.Colour.gold().to_rgb()), + ('green', discord.Colour.green().to_rgb()), + ('greyple', discord.Colour.greyple().to_rgb()), + ('light_grey', discord.Colour.light_grey().to_rgb()), + ('magenta', discord.Colour.magenta().to_rgb()), + ('orange', discord.Colour.orange().to_rgb()), + ('purple', discord.Colour.purple().to_rgb()), + ('red', discord.Colour.red().to_rgb()), + ('teal', discord.Colour.teal().to_rgb()), + ('yellow', discord.Colour.yellow().to_rgb()) + ] + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') async def _seed_levels(self, channel: discord.TextChannel): @@ -163,33 +188,74 @@ class LevelGroup(DiscordCommandABC): @create.autocomplete('color') async def create_color_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: - colors = [ - ('blue', discord.Colour.blue().to_rgb()), - ('dark_blue', discord.Colour.dark_blue().to_rgb()), - ('dark_gold', discord.Colour.dark_gold().to_rgb()), - ('dark_gray', discord.Colour.dark_gray().to_rgb()), - ('dark_green', discord.Colour.dark_green().to_rgb()), - ('dark_grey', discord.Colour.dark_grey().to_rgb()), - ('dark_magenta', discord.Colour.dark_magenta().to_rgb()), - ('dark_orange', discord.Colour.dark_orange().to_rgb()), - ('dark_purple', discord.Colour.dark_purple().to_rgb()), - ('dark_red', discord.Colour.dark_red().to_rgb()), - ('dark_teal', discord.Colour.dark_teal().to_rgb()), - ('default', discord.Colour.default().to_rgb()), - ('gold', discord.Colour.gold().to_rgb()), - ('green', discord.Colour.green().to_rgb()), - ('greyple', discord.Colour.greyple().to_rgb()), - ('light_grey', discord.Colour.light_grey().to_rgb()), - ('magenta', discord.Colour.magenta().to_rgb()), - ('orange', discord.Colour.orange().to_rgb()), - ('purple', discord.Colour.purple().to_rgb()), - ('red', discord.Colour.red().to_rgb()), - ('teal', discord.Colour.teal().to_rgb()), - ('yellow', discord.Colour.yellow().to_rgb()) - ] # 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] + 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 self._colors] + + @level.command() + @commands.guild_only() + async def edit(self, ctx: Context, level: str, name: str = None, color: str = None, min_xp: int = None, permissions: int = None): + self._logger.debug(__name__, f'Received command level edit {ctx}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + self._client_utils.received_command(ctx.guild.id) + + if ctx.guild is None: + return + + server = self._servers.get_server_by_discord_id(ctx.guild.id) + level_from_db = self._levels.get_levels_by_server_id(server.server_id).where(lambda l: l.name == level).single_or_default() + if level_from_db is None: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.edit.not_found').format(level)) + return + + guild: Guild = self._bot.guilds.where(lambda g: g == ctx.guild).single() + role: Role = guild.roles.where(lambda r: r.name == level_from_db.name).single() + + if name is not None: + level_from_db.name = name + + if color is not None: + try: + level_from_db.color = hex(discord.Colour.from_str(color).value) + except Exception as e: + self._logger.error(__name__, f'Error parsing color {color}', e) + return + + if min_xp is not None: + level_from_db.min_xp = min_xp + + if permissions is not None: + try: + level_from_db.permissions = discord.Permissions(permissions).value + except Exception as e: + self._logger.error(__name__, f'Error parsing permissions {permissions}', e) + return + + try: + self._levels.update_level(level_from_db) + self._db.save_changes() + await role.edit(name=level_from_db.name, permissions=discord.Permissions(level_from_db.permissions), colour=discord.Colour(int(level_from_db.color, 16))) + self._logger.info(__name__, f'Saved level {level_from_db.name} with color {level_from_db.color}, min_xp {level_from_db.min_xp} and permissions {level_from_db.permissions}') + except Exception as e: + self._logger.error(__name__, f'Could not save level {level} 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.edit.edited').format(level)) + await self._seed_levels(ctx.channel) + + self._logger.trace(__name__, f'Finished command level edit') + + @edit.autocomplete('level') + async def edit_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] + + @edit.autocomplete('color') + async def edit_color_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + # 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 self._colors] @level.command() @commands.guild_only() -- 2.45.2 From c70149e9c8efe2a464275f241ffc5c06969410e2 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 10 Nov 2022 21:10:48 +0100 Subject: [PATCH 2/3] Added is admin check to level edit #103 --- kdb-bot/src/modules/level/command/level_group.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kdb-bot/src/modules/level/command/level_group.py b/kdb-bot/src/modules/level/command/level_group.py index 3a2c211b..27fff87a 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -205,6 +205,11 @@ class LevelGroup(DiscordCommandABC): return self._client_utils.received_command(ctx.guild.id) + 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 + if ctx.guild is None: return -- 2.45.2 From b2087042bc95f65253c5dd43794453164628255f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 11 Nov 2022 07:33:24 +0100 Subject: [PATCH 3/3] Added error notification to level edit #103 --- kdb-bot/src/bot/translation/de.json | 2 ++ kdb-bot/src/modules/level/command/level_group.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index ba857845..b3bbf9eb 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -196,6 +196,8 @@ }, "edit": { "edited": "Level {} wurde bearbeitet :D", + "color_invalid": "Die Farbe {} ist ungültig!", + "permission_invalid": "Der Berechtigungswert {} ist ungültig!", "not_found": "Level {} nicht gefunden!" }, "remove": { diff --git a/kdb-bot/src/modules/level/command/level_group.py b/kdb-bot/src/modules/level/command/level_group.py index 27fff87a..c8253a2c 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -229,6 +229,7 @@ class LevelGroup(DiscordCommandABC): try: level_from_db.color = hex(discord.Colour.from_str(color).value) except Exception as e: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.edit.color_invalid').format(color)) self._logger.error(__name__, f'Error parsing color {color}', e) return @@ -239,6 +240,7 @@ class LevelGroup(DiscordCommandABC): try: level_from_db.permissions = discord.Permissions(permissions).value except Exception as e: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.edit.permission_invalid').format(permissions)) self._logger.error(__name__, f'Error parsing permissions {permissions}', e) return -- 2.45.2