From b8fcaa48a52835e8767cff753ba9ca842cfb9479 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 7 Nov 2022 20:54:27 +0100 Subject: [PATCH] Added remove command #91 --- kdb-bot/src/bot/translation/de.json | 14 +++- .../src/modules/level/command/level_group.py | 79 ++++++++++++++++--- 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 994ec206c3..cabd346b39 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -176,6 +176,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!", @@ -189,10 +192,13 @@ "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", + "error": { + "not_found": "Level {} nicht gefunden!" + } } }, "database": {}, diff --git a/kdb-bot/src/modules/level/command/level_group.py b/kdb-bot/src/modules/level/command/level_group.py index 5dd951c01a..05cdcad78d 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 @@ -53,6 +54,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): @@ -104,6 +115,14 @@ class LevelGroup(DiscordCommandABC): async def create(self, ctx: Context, name: str, color: str, min_xp: int, permissions: int): self._logger.debug(__name__, f'Received command level create {ctx}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + + 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 + try: color = hex(discord.Colour.from_str(color).value) except Exception as e: @@ -116,9 +135,6 @@ class LevelGroup(DiscordCommandABC): self._logger.error(__name__, f'Error parsing permissions {permissions}', e) return - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - if ctx.guild is None: return @@ -142,15 +158,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') @@ -184,3 +192,50 @@ 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 remove(self, ctx: Context, level: str): + self._logger.debug(__name__, f'Received command level remove {ctx}') + + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + + 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: + self._logger.trace(__name__, f'Finished command level remove') + 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).first_or_default() + if level_from_db is None: + self._logger.debug(__name__, f'level {level} not found') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.remove.error.not_found').format(level)) + self._logger.trace(__name__, f'Finished command level remove') + return + + try: + self._levels.delete_level(level_from_db) + self._db_context.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: + await role.delete() + self._logger.info(__name__, f'Removed level {level}') + except Exception as e: + self._logger.error(__name__, f'Could not remove level {level}', e) + else: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.remove.success').format(level)) + await self._seed_levels(ctx.channel) + + self._logger.trace(__name__, f'Finished command level remove') + + @remove.autocomplete('level') + async def remove_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]