From 7a52188ff653099d9cb48ca18e5c54af4953069f Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 12 Nov 2024 21:07:05 +0100 Subject: [PATCH] fix(bot): handle errors in command processing Wrapped command handling in a try-except block to log errors and notify users of failures in processing commands. Updated the error message for the protect command, and included responses for invalid or unauthorized commands. This enhances robustness and improves user feedback. Adjustments ensure all exceptions are caught, improving usability and maintainability. --- src/matrix_applicationbot/classes/bot.py | 160 ++++++++++++----------- 1 file changed, 86 insertions(+), 74 deletions(-) diff --git a/src/matrix_applicationbot/classes/bot.py b/src/matrix_applicationbot/classes/bot.py index 8694d69..8b0ea90 100644 --- a/src/matrix_applicationbot/classes/bot.py +++ b/src/matrix_applicationbot/classes/bot.py @@ -14,6 +14,7 @@ from nio import ( RoomInviteResponse, ReactionEvent, RoomSendResponse, + RoomPutStateError, ) import logging @@ -183,95 +184,106 @@ class ApplicationBot: ) async def handle_command(self, room, sender, command): - if command == "help": - return await self.help_command(room, sender) - elif command == "openticket": - return await self.open_ticket(room, sender) - elif await self.is_operator(sender): - if command.startswith("invite"): - return await self.invite_operator(room, sender, command) - elif command.startswith("close"): - return await self.close_ticket(room, sender, command) - elif command.startswith("approve"): - return await self.approve_or_reject_application( - room, sender, command, approved=True - ) - elif command.startswith("reject"): - return await self.approve_or_reject_application( - room, sender, command, approved=False - ) - elif command == "list": - return await self.list_tickets(room) - elif command.startswith("monitor"): - room_id = self.resolve_room_id(command.split()[1]) - await self.add_monitored_room(room_id) - return await self.client.room_send( - room.room_id, - "m.room.message", - { - "msgtype": "m.text", - "body": f"Room {room_id} is now being monitored for applications.", - }, - ) - elif command.startswith("unmonitor"): - room_id = self.resolve_room_id(command.split()[1]) - await self.remove_monitored_room(room_id) - return await self.client.room_send( - room.room_id, - "m.room.message", - { - "msgtype": "m.text", - "body": f"Room {room_id} is no longer being monitored for applications.", - }, - ) - elif command.startswith("monitored"): - return await self.list_monitored_rooms(room) - elif command.startswith("protect "): - try: - room_id = await self.resolve_room_id(command.split()[1]) - await self.add_protected_room(room_id) + try: + if command == "help": + return await self.help_command(room, sender) + elif command == "openticket": + return await self.open_ticket(room, sender) + elif await self.is_operator(sender): + if command.startswith("invite"): + return await self.invite_operator(room, sender, command) + elif command.startswith("close"): + return await self.close_ticket(room, sender, command) + elif command.startswith("approve"): + return await self.approve_or_reject_application( + room, sender, command, approved=True + ) + elif command.startswith("reject"): + return await self.approve_or_reject_application( + room, sender, command, approved=False + ) + elif command == "list": + return await self.list_tickets(room) + elif command.startswith("monitor"): + room_id = self.resolve_room_id(command.split()[1]) + await self.add_monitored_room(room_id) return await self.client.room_send( room.room_id, "m.room.message", { "msgtype": "m.text", - "body": f"Room {room_id} has been protected.", + "body": f"Room {room_id} is now being monitored for applications.", }, ) - except IndexError: - logging.error( - f"No room ID specified for protect command: {command}" - ) + elif command.startswith("unmonitor"): + room_id = self.resolve_room_id(command.split()[1]) + await self.remove_monitored_room(room_id) return await self.client.room_send( room.room_id, "m.room.message", { "msgtype": "m.text", - "body": "Please specify a room ID to protect.", + "body": f"Room {room_id} is no longer being monitored for applications.", }, ) - elif command.startswith("unprotect"): - room_id = self.resolve_room_id(command.split()[1]) - await self.remove_protected_room(room_id) - return await self.client.room_send( - room.room_id, - "m.room.message", - { - "msgtype": "m.text", - "body": f"Room {room_id} has been unprotected.", - }, - ) - elif command.startswith("protected"): - return await self.list_protected_rooms(room) + elif command.startswith("monitored"): + return await self.list_monitored_rooms(room) + elif command.startswith("protect "): + try: + room_id = await self.resolve_room_id(command.split()[1]) + await self.add_protected_room(room_id) + return await self.client.room_send( + room.room_id, + "m.room.message", + { + "msgtype": "m.text", + "body": f"Room {room_id} has been protected.", + }, + ) + except IndexError: + logging.error( + f"No room ID specified for protect command: {command}" + ) + return await self.client.room_send( + room.room_id, + "m.room.message", + { + "msgtype": "m.text", + "body": "Please specify a room ID to protect.", + }, + ) + elif command.startswith("unprotect"): + room_id = self.resolve_room_id(command.split()[1]) + await self.remove_protected_room(room_id) + return await self.client.room_send( + room.room_id, + "m.room.message", + { + "msgtype": "m.text", + "body": f"Room {room_id} has been unprotected.", + }, + ) + elif command.startswith("protected"): + return await self.list_protected_rooms(room) - await self.client.room_send( - room.room_id, - "m.room.message", - { - "msgtype": "m.text", - "body": "Sorry, I do not know this command, or you are not authorized to use it.", - }, - ) + await self.client.room_send( + room.room_id, + "m.room.message", + { + "msgtype": "m.text", + "body": "Sorry, I do not know this command, or you are not authorized to use it.", + }, + ) + except Exception as e: + logging.error(f"Failed to handle command: {e}") + await self.client.room_send( + room.room_id, + "m.room.message", + { + "msgtype": "m.text", + "body": "An error occurred while processing the command.", + }, + ) async def process_application(self, room: MatrixRoom, sender, event): message_body = event.body