From 1b9553e63b874fe4a6bf67327f00cb577a62b23b Mon Sep 17 00:00:00 2001 From: Nick Jungmann Date: Sun, 18 Dec 2022 22:42:38 +0100 Subject: [PATCH] Improved error handling and changed attribute names from constants to variables which are located in the language file #22 --- kdb-bot/src/bot/translation/de.json | 28 +++++----- .../src/modules/base/command/user_group.py | 51 ++++++++++++------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index e18c24b5..ea3b84f3 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -161,19 +161,19 @@ "max_char_count_exceeded": "Der Text darf nicht mehr als 128 Zeichen lang sein!" }, "user": { + "atr": { + "id": "Id", + "name": "Name", + "discord_join": "Discord beigetreten am", + "last_join": "Server beigetreten am", + "xp": "XP", + "ontime": "Ontime", + "roles": "Rollen", + "joins": "Beitritte", + "lefts": "Abgänge", + "warnings": "Verwarnungen" + }, "info": { - "fields": { - "id": "Id", - "name": "Name", - "discord_join": "Discord beigetreten am", - "last_join": "Server beigetreten am", - "xp": "XP", - "ontime": "Ontime", - "roles": "Rollen", - "joins": "Beitritte", - "lefts": "Abgänge", - "warnings": "Verwarnungen" - }, "footer": "" }, "get": { @@ -184,7 +184,9 @@ "xp": "{} hat nun {} xp", "error": { "value_type_not_numeric": "Der angegebende Wert ist keine Ganzzahl! :(", - "set_xp": "Es kam zu einem Fehler bei der Zuweisung der XP :(" + "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 :(" } }, "error": { diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index 0c6692f6..64f78938 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -9,6 +9,7 @@ from cpl_translation import TranslatePipe from discord import app_commands from discord.ext import commands from discord.ext.commands import Context +from mysql.connector.errors import DatabaseError from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC @@ -25,6 +26,10 @@ from modules.permission.abc.permission_service_abc import PermissionServiceABC class UserGroup(DiscordCommandABC): + class _AtrClass: + xp: str + ontime: str + def __init__( self, config: ConfigurationABC, @@ -61,6 +66,10 @@ class UserGroup(DiscordCommandABC): self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + self._atr_class = self._AtrClass + self._atr_class.xp = self._t.transform('modules.base.user.atr.xp') + self._atr_class.ontime = self._t.transform('modules.base.user.atr.ontime') + @commands.hybrid_group() @commands.guild_only() async def user(self, ctx: Context): @@ -93,25 +102,25 @@ class UserGroup(DiscordCommandABC): ontime = self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id).sum( lambda join: round((join.leaved_on - join.joined_on).total_seconds() / 3600, 2)) - embed.add_field(name=self._t.transform('modules.base.user.info.fields.id'), value=member.id) - embed.add_field(name=self._t.transform('modules.base.user.info.fields.name'), value=member.name) - embed.add_field(name=self._t.transform('modules.base.user.info.fields.discord_join'), + embed.add_field(name=self._t.transform('modules.base.user.atr.id'), value=member.id) + embed.add_field(name=self._t.transform('modules.base.user.atr.name'), value=member.name) + embed.add_field(name=self._t.transform('modules.base.user.atr.discord_join'), value=self._date.transform(member.created_at), inline=False) - embed.add_field(name=self._t.transform('modules.base.user.info.fields.last_join'), + embed.add_field(name=self._t.transform('modules.base.user.atr.last_join'), value=self._date.transform(member.joined_at), inline=False) - embed.add_field(name=self._t.transform('modules.base.user.info.fields.xp'), value=str(user.xp)) - embed.add_field(name=self._t.transform('modules.base.user.info.fields.ontime'), value=str(ontime)) + embed.add_field(name=self._t.transform('modules.base.user.atr.xp'), value=str(user.xp)) + embed.add_field(name=self._t.transform('modules.base.user.atr.ontime'), value=str(ontime)) roles = '' for role in member.roles: roles += f'{role.name}\n' - embed.add_field(name=self._t.transform('modules.base.user.info.fields.roles'), value=roles, inline=False) + embed.add_field(name=self._t.transform('modules.base.user.atr.roles'), value=roles, inline=False) if is_mod or member == ctx.author: joins_string = '' for join in joins: joins_string += f'{self._date.transform(join.joined_on)}\n' - embed.add_field(name=self._t.transform('modules.base.user.info.fields.joins'), value=joins_string) + embed.add_field(name=self._t.transform('modules.base.user.atr.joins'), value=joins_string) if is_mod or member == ctx.author: lefts_string = '' @@ -122,10 +131,10 @@ class UserGroup(DiscordCommandABC): continue lefts_string += f'{self._date.transform(join.leaved_on)}\n' - embed.add_field(name=self._t.transform('modules.base.user.info.fields.lefts'), value=lefts_string) + embed.add_field(name=self._t.transform('modules.base.user.atr.lefts'), value=lefts_string) if is_mod or member == ctx.author: - embed.add_field(name=self._t.transform('modules.base.user.info.fields.warnings'), + embed.add_field(name=self._t.transform('modules.base.user.atr.warnings'), value=self._t.transform('common.not_implemented_yet'), inline=False) # send to interaction because of sensitive data @@ -150,10 +159,10 @@ class UserGroup(DiscordCommandABC): user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) match atr: - case 'xp': + case self._atr_class.xp: value = str(user.xp) - case 'ontime': + case self._atr_class.ontime: value = str(round( self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id) .sum(lambda join: (join.leaved_on - join.joined_on).total_seconds() / 3600), @@ -171,7 +180,7 @@ class UserGroup(DiscordCommandABC): @get.autocomplete('atr') async def get_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] @user.command() @@ -188,7 +197,7 @@ class UserGroup(DiscordCommandABC): user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) match atr: - case 'xp': + case self._atr_class.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')) return @@ -198,9 +207,17 @@ class UserGroup(DiscordCommandABC): self._users.update_user(user) self._db.save_changes() await self._level.check_level(member) + 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')) + return + except DatabaseError as de: + self._logger.error(__name__, f'An error occurred while updating the user', de) + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.set.error.database_error')) + return except Exception as e: - self._logger.error(__name__, f'Value couldn\'t be converted to int', e) - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.set.error.set_xp')) + self._logger.error(__name__, f'An error occurred while setting xp', e) + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.set.error.general_error')) return case other: @@ -215,5 +232,5 @@ class UserGroup(DiscordCommandABC): @set.autocomplete('atr') async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: - atr_list = ['xp'] + atr_list = [self._atr_class.xp] return [app_commands.Choice(name=atr, value=atr) for atr in atr_list]