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

View file

@ -480,12 +480,23 @@ class ApplicationBot:
return [] return []
async def set_monitored_rooms(self, rooms): 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, room_id=self.operator_room_id,
event_type="m.room.monitored", event_type="m.room.monitored",
content={"rooms": rooms}, 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): async def list_monitored_rooms(self, room):
monitored_rooms = await self.get_monitored_rooms() monitored_rooms = await self.get_monitored_rooms()
if monitored_rooms: if monitored_rooms:
@ -531,12 +542,23 @@ class ApplicationBot:
return [] return []
async def set_protected_rooms(self, rooms): 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, room_id=self.operator_room_id,
event_type="m.room.protected", event_type="m.room.protected",
content={"rooms": rooms}, 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): async def list_protected_rooms(self, room):
protected_rooms = await self.get_protected_rooms() protected_rooms = await self.get_protected_rooms()
if protected_rooms: if protected_rooms: