From 40e53de3f2bf393c1efd72737b6853698aa75f44 Mon Sep 17 00:00:00 2001 From: Nick Jungmann Date: Thu, 22 Dec 2022 20:38:40 +0100 Subject: [PATCH] Removed error handling for database errors and replaced match-case-statements with if-else-statements #22 --- .../src/modules/base/command/user_group.py | 86 ++++++++----------- 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index 64f78938..a919f3f7 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -26,10 +26,6 @@ from modules.permission.abc.permission_service_abc import PermissionServiceABC class UserGroup(DiscordCommandABC): - class _AtrClass: - xp: str - ontime: str - def __init__( self, config: ConfigurationABC, @@ -66,9 +62,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') + self._atr_dict = { + "xp": self._t.transform('modules.base.user.atr.xp'), + 'ontime': self._t.transform('modules.base.user.atr.ontime') + } @commands.hybrid_group() @commands.guild_only() @@ -158,20 +155,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: - value = str(user.xp) - - 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), - 2 - )) + if atr == self._atr_dict["xp"]: + value = str(user.xp) - case other: - await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.atr_not_found').format(atr)) - return + elif atr == self._atr_dict["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), + 2 + )) + + 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, @@ -180,7 +176,7 @@ class UserGroup(DiscordCommandABC): @get.autocomplete('atr') async def get_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: - atr_list = [self._atr_class.xp, self._atr_class.ontime] + atr_list = [self._atr_dict["xp"], self._atr_dict["ontime"]] return [app_commands.Choice(name=atr, value=atr) for atr in atr_list] @user.command() @@ -196,35 +192,29 @@ 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: - 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 - - try: - user.xp = int(value) - 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'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: + 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.error.atr_not_found').format(atr)) + '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')) + return + else: + self._users.update_user(user) + self._db.save_changes() + 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)) + return + await self._message_service.send_interaction_msg( ctx.interaction, self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value) @@ -232,5 +222,5 @@ class UserGroup(DiscordCommandABC): @set.autocomplete('atr') async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: - atr_list = [self._atr_class.xp] + atr_list = [self._atr_dict["xp"]] return [app_commands.Choice(name=atr, value=atr) for atr in atr_list]