Removed error handling for database errors, replaced match-case-statements with if-else-statements and removed unused variables from the language file #23

This commit is contained in:
Nick Jungmann 2022-12-22 20:54:15 +01:00
parent 9040ab6fca
commit ffd5105154
2 changed files with 20 additions and 51 deletions

View File

@ -184,20 +184,15 @@
"xp": "{} hat nun {} xp", "xp": "{} hat nun {} xp",
"error": { "error": {
"value_type_not_numeric": "Der angegebende Wert ist keine Ganzzahl! :(", "value_type_not_numeric": "Der angegebende Wert ist keine Ganzzahl! :(",
"type_error": "Der angegebene Wert konnte nicht als eine Ganzzahl interpretiert werden :(", "type_error": "Der angegebene Wert konnte nicht als eine Ganzzahl interpretiert werden :("
"database_error": "Beim Speichern des Wertes kam es zu einem Fehler :("
} }
}, },
"remove": { "remove": {
"xp": "Die {} von {} wurden entfernt", "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": { "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 :("
} }
} }
}, },

View File

@ -194,16 +194,14 @@ class UserGroup(DiscordCommandABC):
if atr == self._atr_dict["xp"]: if atr == self._atr_dict["xp"]:
if not value.isnumeric(): if not value.isnumeric():
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.set.error.value_type_not_numeric'))
'modules.base.user.set.error.value_type_not_numeric'))
return return
try: try:
user.xp = int(value) user.xp = int(value)
except TypeError as te: except TypeError as te:
self._logger.error(__name__, f'String value couldn\'t be converted to int', 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( await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.set.error.type_error'))
'modules.base.user.set.error.type_error'))
return return
else: else:
self._users.update_user(user) self._users.update_user(user)
@ -211,14 +209,10 @@ class UserGroup(DiscordCommandABC):
await self._level.check_level(member) await self._level.check_level(member)
else: else:
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.error.atr_not_found').format(atr))
return return
await self._message_service.send_interaction_msg( await self._message_service.send_interaction_msg(ctx.interaction,self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value))
ctx.interaction,
self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value)
)
@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]]:
@ -238,39 +232,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: user.xp = 0
try: self._users.update_user(user)
user.xp = 0 self._db.save_changes()
self._users.update_user(user) await self._level.check_level(member)
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 self._atr_class.ontime: elif atr == self._atr_dict["ontime"]:
try: self._user_joined_voice_channel.delete_user_joined_voice_channel_by_user_id(user.user_id)
self._user_joined_voice_channel.delete_user_joined_voice_channel_by_user_id(user.user_id) self._db.save_changes()
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: 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,
@ -279,5 +253,5 @@ class UserGroup(DiscordCommandABC):
@remove.autocomplete('atr') @remove.autocomplete('atr')
async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: 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] 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]