Removed error handling for database errors and replaced match-case-statements with if-else-statements #22

This commit is contained in:
Nick Jungmann 2022-12-22 20:38:40 +01:00
parent 1b9553e63b
commit 40e53de3f2

View File

@ -26,10 +26,6 @@ from modules.permission.abc.permission_service_abc import PermissionServiceABC
class UserGroup(DiscordCommandABC): class UserGroup(DiscordCommandABC):
class _AtrClass:
xp: str
ontime: str
def __init__( def __init__(
self, self,
config: ConfigurationABC, config: ConfigurationABC,
@ -66,9 +62,10 @@ class UserGroup(DiscordCommandABC):
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
self._atr_class = self._AtrClass self._atr_dict = {
self._atr_class.xp = self._t.transform('modules.base.user.atr.xp') "xp": self._t.transform('modules.base.user.atr.xp'),
self._atr_class.ontime = self._t.transform('modules.base.user.atr.ontime') 'ontime': self._t.transform('modules.base.user.atr.ontime')
}
@commands.hybrid_group() @commands.hybrid_group()
@commands.guild_only() @commands.guild_only()
@ -158,20 +155,19 @@ class UserGroup(DiscordCommandABC):
server = self._servers.find_server_by_discord_id(ctx.guild.id) 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) user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id)
match atr: if atr == self._atr_dict["xp"]:
case self._atr_class.xp: value = str(user.xp)
value = str(user.xp)
case self._atr_class.ontime: elif atr == self._atr_dict["ontime"]:
value = str(round( value = str(round(
self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id) 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), .sum(lambda join: (join.leaved_on - join.joined_on).total_seconds() / 3600),
2 2
)) ))
case other: 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 return
await self._message_service.send_interaction_msg( await self._message_service.send_interaction_msg(
ctx.interaction, ctx.interaction,
@ -180,7 +176,7 @@ class UserGroup(DiscordCommandABC):
@get.autocomplete('atr') @get.autocomplete('atr')
async def get_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: 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] return [app_commands.Choice(name=atr, value=atr) for atr in atr_list]
@user.command() @user.command()
@ -196,35 +192,29 @@ class UserGroup(DiscordCommandABC):
server = self._servers.find_server_by_discord_id(ctx.guild.id) 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) user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id)
match atr: if atr == self._atr_dict["xp"]:
case self._atr_class.xp: if not value.isnumeric():
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:
await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform( 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 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( await self._message_service.send_interaction_msg(
ctx.interaction, ctx.interaction,
self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value) self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value)
@ -232,5 +222,5 @@ class UserGroup(DiscordCommandABC):
@set.autocomplete('atr') @set.autocomplete('atr')
async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: 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] return [app_commands.Choice(name=atr, value=atr) for atr in atr_list]