From d197a6e1582dc6610b7bf97e297f95a5b00a0cff Mon Sep 17 00:00:00 2001 From: Nick Jungmann Date: Sun, 11 Dec 2022 03:31:49 +0100 Subject: [PATCH 1/5] Added user remove command #23 --- .../src/modules/base/command/user_group.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index a919f3f7..6ce8f72f 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -224,3 +224,36 @@ class UserGroup(DiscordCommandABC): async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: atr_list = [self._atr_dict["xp"]] return [app_commands.Choice(name=atr, value=atr) for atr in atr_list] + + async def remove(self, ctx: context, atr: str, member: discord.Member = none): + self._logger.debug(__name__, f'Received command user-remove {ctx}:{member}') + + if member is None or not isinstance(member, discord.Member): + member = ctx.author + + server = self._servers.find_server_by_discord_id(ctx.guild.id) + user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) + + match atr: + case 'xp': + user.xp = 0 + + case 'ontime': + # ToDo: SQL-Statement to delete all records for dcUser in user_joined_voice_channel + return + + case other: + # ToDo: Move atr_not_found + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.atr_not_found').format(atr)) + return + + # ToDo: Add text for it + await self._message_service.send_interaction_msg( + ctx.interaction, + self._t.transform(f'modules.base.user.remove.{atr}').format(member.mention, value) + ) + + @remove.autocomplete('atr') + async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: + atr_list = ['xp', 'ontime'] + return [app_commands.Choice(name=atr, value=atr) for atr in atr_list] \ No newline at end of file -- 2.45.2 From 71899346b220faaa64c40b5f33ccd24a546aafbb Mon Sep 17 00:00:00 2001 From: Nick Jungmann Date: Sun, 11 Dec 2022 14:25:47 +0100 Subject: [PATCH 2/5] Added translation for user remove command #23 --- kdb-bot/src/bot/translation/de.json | 4 ++++ kdb-bot/src/modules/base/command/user_group.py | 4 +--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index ea3b84f3..e8d784d8 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -189,6 +189,10 @@ "general_error": "Bei der Ausführung dieses Befehls kam es zu einen Fehler :(" } }, + "remove": { + "xp": "Die {} von {} wurden entfernt", + "ontime": "Die {} von {} wurden entfernt" + }, "error": { "atr_not_found": "Das Attribut {} konnte nicht gefunden werden :(" } diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index 6ce8f72f..3d0d12c5 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -243,14 +243,12 @@ class UserGroup(DiscordCommandABC): return case other: - # ToDo: Move atr_not_found await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.atr_not_found').format(atr)) return - # ToDo: Add text for it await self._message_service.send_interaction_msg( ctx.interaction, - self._t.transform(f'modules.base.user.remove.{atr}').format(member.mention, value) + self._t.transform(f'modules.base.user.remove.{atr}').format(value, member.mention) ) @remove.autocomplete('atr') -- 2.45.2 From 9d89135b4c30385154d56b0e9b5abddf07d96491 Mon Sep 17 00:00:00 2001 From: Nick Jungmann Date: Sat, 17 Dec 2022 21:49:46 +0100 Subject: [PATCH 3/5] Added SQL command to delete all records by user id in "userjoinedvoicechannel"-table #23 --- .../bot_data/abc/user_joined_voice_channel_abc.py | 3 +++ .../bot_data/model/user_joined_voice_channel.py | 7 +++++++ .../service/user_joined_voice_channel_service.py | 4 ++++ kdb-bot/src/modules/base/command/user_group.py | 14 +++++++++++--- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/kdb-bot/src/bot_data/abc/user_joined_voice_channel_abc.py b/kdb-bot/src/bot_data/abc/user_joined_voice_channel_abc.py index d46a6f2b..b1084da8 100644 --- a/kdb-bot/src/bot_data/abc/user_joined_voice_channel_abc.py +++ b/kdb-bot/src/bot_data/abc/user_joined_voice_channel_abc.py @@ -35,3 +35,6 @@ class UserJoinedVoiceChannelRepositoryABC(ABC): @abstractmethod def delete_user_joined_voice_channel(self, user_joined_voice_channel: UserJoinedVoiceChannel): pass + + @abstractmethod + def delete_user_joined_voice_channel_by_user_id(self, user_id: int): pass diff --git a/kdb-bot/src/bot_data/model/user_joined_voice_channel.py b/kdb-bot/src/bot_data/model/user_joined_voice_channel.py index 9f216350..4977c2e6 100644 --- a/kdb-bot/src/bot_data/model/user_joined_voice_channel.py +++ b/kdb-bot/src/bot_data/model/user_joined_voice_channel.py @@ -119,3 +119,10 @@ class UserJoinedVoiceChannel(TableABC): DELETE FROM `UserJoinedVoiceChannel` WHERE `JoinId` = {self._join_id}; """) + + @staticmethod + def delete_by_user_id_string(id: int) -> str: + return str(f""" + DELETE FROM `UserJoinedVoiceChannel` + WHERE `UserId` = {id} + """) \ No newline at end of file diff --git a/kdb-bot/src/bot_data/service/user_joined_voice_channel_service.py b/kdb-bot/src/bot_data/service/user_joined_voice_channel_service.py index d6b1b596..919b2ee5 100644 --- a/kdb-bot/src/bot_data/service/user_joined_voice_channel_service.py +++ b/kdb-bot/src/bot_data/service/user_joined_voice_channel_service.py @@ -121,3 +121,7 @@ class UserJoinedVoiceChannelRepositoryService(UserJoinedVoiceChannelRepositoryAB def delete_user_joined_voice_channel(self, user_joined_voice_channel: UserJoinedVoiceChannel): self._logger.trace(__name__, f'Send SQL command: {user_joined_voice_channel.delete_string}') self._context.cursor.execute(user_joined_voice_channel.delete_string) + + def delete_user_joined_voice_channel_by_user_id(self, user_id: int): + self._logger.trace(__name__, f'Send SQL command: {UserJoinedVoiceChannel.delete_by_user_id_string}') + self._context.cursor.execute(UserJoinedVoiceChannel.delete_by_user_id_string(user_id)) diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index 3d0d12c5..102bef95 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -225,7 +225,11 @@ class UserGroup(DiscordCommandABC): atr_list = [self._atr_dict["xp"]] return [app_commands.Choice(name=atr, value=atr) for atr in atr_list] - async def remove(self, ctx: context, atr: str, member: discord.Member = none): + @user.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() + async def remove(self, ctx: Context, atr: str, member: discord.Member = None): self._logger.debug(__name__, f'Received command user-remove {ctx}:{member}') if member is None or not isinstance(member, discord.Member): @@ -237,9 +241,13 @@ class UserGroup(DiscordCommandABC): match atr: case 'xp': user.xp = 0 + self._users.update_user(user) + self._db.save_changes() + await self._level.check_level(member) case 'ontime': - # ToDo: SQL-Statement to delete all records for dcUser in user_joined_voice_channel + self._user_joined_voice_channel.delete_user_joined_voice_channel_by_user_id(user.user_id) + self._db.save_changes() return case other: @@ -248,7 +256,7 @@ class UserGroup(DiscordCommandABC): await self._message_service.send_interaction_msg( ctx.interaction, - self._t.transform(f'modules.base.user.remove.{atr}').format(value, member.mention) + self._t.transform(f'modules.base.user.remove.{atr}').format(atr, member.mention) ) @remove.autocomplete('atr') -- 2.45.2 From 9040ab6fca4aad550baece3db2a322bfe53274a3 Mon Sep 17 00:00:00 2001 From: Nick Jungmann Date: Mon, 19 Dec 2022 00:11:43 +0100 Subject: [PATCH 4/5] Improved error handling and changed attribute names from constants to variables which are located in the language file #23 --- kdb-bot/src/bot/translation/de.json | 11 ++++-- .../src/modules/base/command/user_group.py | 38 ++++++++++++++----- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index e8d784d8..c16720a1 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -185,16 +185,19 @@ "error": { "value_type_not_numeric": "Der angegebende Wert ist keine Ganzzahl! :(", "type_error": "Der angegebene Wert konnte nicht als eine Ganzzahl interpretiert werden :(", - "database_error": "Beim Speichern des Wertes kam es zu einem Fehler :(", - "general_error": "Bei der Ausführung dieses Befehls kam es zu einen Fehler :(" + "database_error": "Beim Speichern des Wertes kam es zu einem Fehler :(" } }, "remove": { "xp": "Die {} von {} wurden entfernt", - "ontime": "Die {} von {} wurden entfernt" + "ontime": "Die {} von {} wurden entfernt", + "error": { + "database_error": "Beim Speichern der Änderung kam es zu einen Fehler :(" + } }, "error": { - "atr_not_found": "Das Attribut {} konnte nicht gefunden werden :(" + "atr_not_found": "Das Attribut {} konnte nicht gefunden werden :(", + "general_error": "Bei der Ausführung dieses Befehls kam es zu einen Fehler :(" } } }, diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index 102bef95..317b3bcf 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -239,16 +239,34 @@ class UserGroup(DiscordCommandABC): user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) match atr: - case 'xp': - user.xp = 0 - self._users.update_user(user) - self._db.save_changes() - await self._level.check_level(member) + case self._atr_class.xp: + try: + user.xp = 0 + self._users.update_user(user) + self._db.save_changes() + await self._level.check_level(member) + except DatabaseError as de: + self._logger.error(__name__, f'An error occurred while removing xp in the database', de) + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.remove.error.database_error')) + return + except Exception as e: + self._logger.error(__name__, f'An error occurred while removing xp', e) + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.general_error')) + return - case 'ontime': - self._user_joined_voice_channel.delete_user_joined_voice_channel_by_user_id(user.user_id) - self._db.save_changes() - return + case self._atr_class.ontime: + try: + self._user_joined_voice_channel.delete_user_joined_voice_channel_by_user_id(user.user_id) + self._db.save_changes() + return + except DatabaseError as de: + self._logger.error(__name__, f'An error occurred while removing ontime in the database', de) + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.remove.error.database_error')) + return + except Exception as e: + self._logger.error(__name__, f'An error occurred while removing ontime', e) + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.general_error')) + return case other: await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.atr_not_found').format(atr)) @@ -261,5 +279,5 @@ class UserGroup(DiscordCommandABC): @remove.autocomplete('atr') async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: - atr_list = ['xp', 'ontime'] + atr_list = [self._atr_class.xp, self._atr_class.ontime] return [app_commands.Choice(name=atr, value=atr) for atr in atr_list] \ No newline at end of file -- 2.45.2 From ffd51051540731585549798cb088aced865e64ba Mon Sep 17 00:00:00 2001 From: Nick Jungmann Date: Thu, 22 Dec 2022 20:54:15 +0100 Subject: [PATCH 5/5] Removed error handling for database errors, replaced match-case-statements with if-else-statements and removed unused variables from the language file #23 --- kdb-bot/src/bot/translation/de.json | 11 +--- .../src/modules/base/command/user_group.py | 60 ++++++------------- 2 files changed, 20 insertions(+), 51 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index c16720a1..6d8ef799 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -184,20 +184,15 @@ "xp": "{} hat nun {} xp", "error": { "value_type_not_numeric": "Der angegebende Wert ist keine Ganzzahl! :(", - "type_error": "Der angegebene Wert konnte nicht als eine Ganzzahl interpretiert werden :(", - "database_error": "Beim Speichern des Wertes kam es zu einem Fehler :(" + "type_error": "Der angegebene Wert konnte nicht als eine Ganzzahl interpretiert werden :(" } }, "remove": { "xp": "Die {} von {} wurden entfernt", - "ontime": "Die {} von {} wurden entfernt", - "error": { - "database_error": "Beim Speichern der Änderung kam es zu einen Fehler :(" - } + "ontime": "Die {} von {} wurden entfernt" }, "error": { - "atr_not_found": "Das Attribut {} konnte nicht gefunden werden :(", - "general_error": "Bei der Ausführung dieses Befehls kam es zu einen Fehler :(" + "atr_not_found": "Das Attribut {} konnte nicht gefunden werden :(" } } }, diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index 317b3bcf..626ce84a 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -194,16 +194,14 @@ class UserGroup(DiscordCommandABC): if atr == self._atr_dict["xp"]: if not value.isnumeric(): - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform( - 'modules.base.user.set.error.value_type_not_numeric')) + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.set.error.value_type_not_numeric')) return try: user.xp = int(value) except TypeError as te: self._logger.error(__name__, f'String value couldn\'t be converted to int', te) - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform( - 'modules.base.user.set.error.type_error')) + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.set.error.type_error')) return else: self._users.update_user(user) @@ -211,14 +209,10 @@ class UserGroup(DiscordCommandABC): await self._level.check_level(member) else: - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform( - 'modules.base.user.error.atr_not_found').format(atr)) + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.atr_not_found').format(atr)) return - await self._message_service.send_interaction_msg( - ctx.interaction, - self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value) - ) + await self._message_service.send_interaction_msg(ctx.interaction,self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value)) @set.autocomplete('atr') async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: @@ -238,39 +232,19 @@ class UserGroup(DiscordCommandABC): server = self._servers.find_server_by_discord_id(ctx.guild.id) user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) - match atr: - case self._atr_class.xp: - try: - user.xp = 0 - self._users.update_user(user) - self._db.save_changes() - await self._level.check_level(member) - except DatabaseError as de: - self._logger.error(__name__, f'An error occurred while removing xp in the database', de) - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.remove.error.database_error')) - return - except Exception as e: - self._logger.error(__name__, f'An error occurred while removing xp', e) - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.general_error')) - return + if atr == self._atr_dict["xp"]: + user.xp = 0 + self._users.update_user(user) + self._db.save_changes() + await self._level.check_level(member) - case self._atr_class.ontime: - try: - self._user_joined_voice_channel.delete_user_joined_voice_channel_by_user_id(user.user_id) - self._db.save_changes() - return - except DatabaseError as de: - self._logger.error(__name__, f'An error occurred while removing ontime in the database', de) - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.remove.error.database_error')) - return - except Exception as e: - self._logger.error(__name__, f'An error occurred while removing ontime', e) - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.general_error')) - return + elif atr == self._atr_dict["ontime"]: + self._user_joined_voice_channel.delete_user_joined_voice_channel_by_user_id(user.user_id) + self._db.save_changes() - case other: - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.atr_not_found').format(atr)) - return + else: + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.atr_not_found').format(atr)) + return await self._message_service.send_interaction_msg( ctx.interaction, @@ -279,5 +253,5 @@ class UserGroup(DiscordCommandABC): @remove.autocomplete('atr') async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: - atr_list = [self._atr_class.xp, self._atr_class.ontime] - return [app_commands.Choice(name=atr, value=atr) for atr in atr_list] \ No newline at end of file + atr_list = [self._atr_dict["xp"], self._atr_dict["ontime"]] + return [app_commands.Choice(name=atr, value=atr) for atr in atr_list] -- 2.45.2