From 9c514cd9cd22acace7a83127380ede014b3855b6 Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 12 Nov 2024 21:21:36 +0100 Subject: [PATCH] fix(bot): handle room state update failures Add error handling for room state updates when setting monitored and protected rooms. Log errors and send a notification to the operator room if the update fails. This ensures that failures don't silently occur, providing better feedback and fault management. --- src/matrix_applicationbot/classes/bot.py | 26 ++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/matrix_applicationbot/classes/bot.py b/src/matrix_applicationbot/classes/bot.py index 8b0ea90..9ba5f67 100644 --- a/src/matrix_applicationbot/classes/bot.py +++ b/src/matrix_applicationbot/classes/bot.py @@ -480,12 +480,23 @@ class ApplicationBot: return [] async def set_monitored_rooms(self, rooms): - await self.client.room_put_state( + response = await self.client.room_put_state( room_id=self.operator_room_id, event_type="m.room.monitored", content={"rooms": rooms}, ) + if not isinstance(response, RoomPutStateResponse): + logging.error(f"Failed to update monitored rooms: {response}") + await self.client.room_send( + self.operator_room_id, + "m.room.message", + { + "msgtype": "m.text", + "body": "Failed to update monitored rooms.", + }, + ) + async def list_monitored_rooms(self, room): monitored_rooms = await self.get_monitored_rooms() if monitored_rooms: @@ -531,12 +542,23 @@ class ApplicationBot: return [] async def set_protected_rooms(self, rooms): - await self.client.room_put_state( + response = await self.client.room_put_state( room_id=self.operator_room_id, event_type="m.room.protected", content={"rooms": rooms}, ) + if not isinstance(response, RoomPutStateResponse): + logging.error(f"Failed to update protected rooms: {response}") + await self.client.room_send( + self.operator_room_id, + "m.room.message", + { + "msgtype": "m.text", + "body": "Failed to update protected rooms.", + }, + ) + async def list_protected_rooms(self, room): protected_rooms = await self.get_protected_rooms() if protected_rooms: