From 9040ab6fca4aad550baece3db2a322bfe53274a3 Mon Sep 17 00:00:00 2001 From: Nick Jungmann Date: Mon, 19 Dec 2022 00:11:43 +0100 Subject: [PATCH] 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