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.
This commit is contained in:
Kumi 2024-11-12 21:07:05 +01:00
parent 400a694362
commit 7a52188ff6
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -14,6 +14,7 @@ from nio import (
RoomInviteResponse, RoomInviteResponse,
ReactionEvent, ReactionEvent,
RoomSendResponse, RoomSendResponse,
RoomPutStateError,
) )
import logging import logging
@ -183,95 +184,106 @@ class ApplicationBot:
) )
async def handle_command(self, room, sender, command): async def handle_command(self, room, sender, command):
if command == "help": try:
return await self.help_command(room, sender) if command == "help":
elif command == "openticket": return await self.help_command(room, sender)
return await self.open_ticket(room, sender) elif command == "openticket":
elif await self.is_operator(sender): return await self.open_ticket(room, sender)
if command.startswith("invite"): elif await self.is_operator(sender):
return await self.invite_operator(room, sender, command) if command.startswith("invite"):
elif command.startswith("close"): return await self.invite_operator(room, sender, command)
return await self.close_ticket(room, sender, command) elif command.startswith("close"):
elif command.startswith("approve"): return await self.close_ticket(room, sender, command)
return await self.approve_or_reject_application( elif command.startswith("approve"):
room, sender, command, approved=True return await self.approve_or_reject_application(
) room, sender, command, approved=True
elif command.startswith("reject"): )
return await self.approve_or_reject_application( elif command.startswith("reject"):
room, sender, command, approved=False return await self.approve_or_reject_application(
) room, sender, command, approved=False
elif command == "list": )
return await self.list_tickets(room) elif command == "list":
elif command.startswith("monitor"): return await self.list_tickets(room)
room_id = self.resolve_room_id(command.split()[1]) elif command.startswith("monitor"):
await self.add_monitored_room(room_id) room_id = self.resolve_room_id(command.split()[1])
return await self.client.room_send( await self.add_monitored_room(room_id)
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)
return await self.client.room_send( return await self.client.room_send(
room.room_id, room.room_id,
"m.room.message", "m.room.message",
{ {
"msgtype": "m.text", "msgtype": "m.text",
"body": f"Room {room_id} has been protected.", "body": f"Room {room_id} is now being monitored for applications.",
}, },
) )
except IndexError: elif command.startswith("unmonitor"):
logging.error( room_id = self.resolve_room_id(command.split()[1])
f"No room ID specified for protect command: {command}" await self.remove_monitored_room(room_id)
)
return await self.client.room_send( return await self.client.room_send(
room.room_id, room.room_id,
"m.room.message", "m.room.message",
{ {
"msgtype": "m.text", "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"): elif command.startswith("monitored"):
room_id = self.resolve_room_id(command.split()[1]) return await self.list_monitored_rooms(room)
await self.remove_protected_room(room_id) elif command.startswith("protect "):
return await self.client.room_send( try:
room.room_id, room_id = await self.resolve_room_id(command.split()[1])
"m.room.message", await self.add_protected_room(room_id)
{ return await self.client.room_send(
"msgtype": "m.text", room.room_id,
"body": f"Room {room_id} has been unprotected.", "m.room.message",
}, {
) "msgtype": "m.text",
elif command.startswith("protected"): "body": f"Room {room_id} has been protected.",
return await self.list_protected_rooms(room) },
)
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( await self.client.room_send(
room.room_id, room.room_id,
"m.room.message", "m.room.message",
{ {
"msgtype": "m.text", "msgtype": "m.text",
"body": "Sorry, I do not know this command, or you are not authorized to use it.", "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): async def process_application(self, room: MatrixRoom, sender, event):
message_body = event.body message_body = event.body