Handle sync error and quit if initial sync fails

This commit is contained in:
Ville Ranki 2021-07-18 22:50:34 +03:00
parent 549fa08918
commit 1291e324ce
1 changed files with 25 additions and 24 deletions

13
bot.py
View File

@ -22,7 +22,7 @@ from io import BytesIO
from PIL import Image from PIL import Image
import requests import requests
from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LoginError, RoomMemberEvent, RoomVisibility, RoomPreset, RoomCreateError, RoomResolveAliasResponse, UploadError, UploadResponse from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LoginError, RoomMemberEvent, RoomVisibility, RoomPreset, RoomCreateError, RoomResolveAliasResponse, UploadError, UploadResponse, SyncError
# Couple of custom exceptions # Couple of custom exceptions
@ -544,18 +544,19 @@ class Bot:
self.logger.info(f'All modules stopped.') self.logger.info(f'All modules stopped.')
async def run(self): async def run(self):
await self.client.sync() sync_response = await self.client.sync()
if type(sync_response) == SyncError:
self.logger.error(f"Received Sync Error when trying to do initial sync! Error message is: %s", sync_response.message)
else:
for roomid, room in self.client.rooms.items(): for roomid, room in self.client.rooms.items():
self.logger.info(f"Bot is on '{room.display_name}'({roomid}) with {len(room.users)} users") self.logger.info(f"Bot is on '{room.display_name}'({roomid}) with {len(room.users)} users")
if len(room.users) == 1 and self.leave_empty_rooms: if len(room.users) == 1 and self.leave_empty_rooms:
self.logger.info(f'Room {roomid} has no other users - leaving it.') self.logger.info(f'Room {roomid} has no other users - leaving it.')
self.logger.info(await self.client.room_leave(roomid)) self.logger.info(await self.client.room_leave(roomid))
self.start()
self.poll_task = asyncio.get_event_loop().create_task(self.poll_timer())
if self.client.logged_in: if self.client.logged_in:
self.start()
self.poll_task = asyncio.get_event_loop().create_task(self.poll_timer())
self.load_settings(self.get_account_data()) self.load_settings(self.get_account_data())
self.client.add_event_callback(self.message_cb, RoomMessageText) self.client.add_event_callback(self.message_cb, RoomMessageText)
self.client.add_event_callback(self.invite_cb, (InviteEvent,)) self.client.add_event_callback(self.invite_cb, (InviteEvent,))