handle login error
This commit is contained in:
parent
665007a4bb
commit
0738e78e0d
14
bot.py
14
bot.py
|
@ -12,7 +12,8 @@ import urllib.parse
|
|||
from importlib import reload
|
||||
|
||||
import requests
|
||||
from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LogoutResponse, LogoutError
|
||||
from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LogoutResponse, LogoutError, \
|
||||
LoginError
|
||||
|
||||
|
||||
# Couple of custom exceptions
|
||||
|
@ -266,7 +267,12 @@ class Bot:
|
|||
|
||||
async def run(self):
|
||||
if not self.client.access_token:
|
||||
await self.client.login(self.matrix_pass)
|
||||
login_response = await self.client.login(self.matrix_pass)
|
||||
|
||||
if isinstance(login_response, LoginError):
|
||||
print(f"Failed to login: {login_response.message}")
|
||||
return
|
||||
|
||||
last_16 = self.client.access_token[-16:]
|
||||
print(f"Logged in with password, access token: ...{last_16}")
|
||||
|
||||
|
@ -296,6 +302,7 @@ class Bot:
|
|||
|
||||
async def shutdown(self):
|
||||
|
||||
if self.client.logged_in:
|
||||
logout = await self.client.logout()
|
||||
|
||||
if isinstance(logout, LogoutResponse):
|
||||
|
@ -309,7 +316,8 @@ class Bot:
|
|||
else:
|
||||
logout: LogoutError
|
||||
print(f"Logout unsuccessful. msg: {logout.message}")
|
||||
|
||||
else:
|
||||
await self.client.client_session.close()
|
||||
|
||||
bot = Bot()
|
||||
bot.init()
|
||||
|
|
Loading…
Reference in New Issue