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,
ReactionEvent,
RoomSendResponse,
RoomPutStateError,
)
import logging
@ -183,6 +184,7 @@ class ApplicationBot:
)
async def handle_command(self, room, sender, command):
try:
if command == "help":
return await self.help_command(room, sender)
elif command == "openticket":
@ -272,6 +274,16 @@ class ApplicationBot:
"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