only logout on shutdown if MATRIX_PASSWORD is set

This commit is contained in:
Frank Becker 2020-02-23 21:13:02 +01:00
parent ab7f803e9b
commit df224a1b03
1 changed files with 14 additions and 14 deletions

24
bot.py
View File

@ -341,23 +341,23 @@ class Bot:
self.logger.error('Client was not able to log in, check env variables!') self.logger.error('Client was not able to log in, check env variables!')
async def shutdown(self): async def shutdown(self):
await self.logout()
await self.close()
if self.client.logged_in: async def logout(self):
logout = await self.client.logout() if self.matrix_pass is not None and self.client.logged_in:
response = await self.client.logout()
if isinstance(logout, LogoutResponse): if isinstance(response, LogoutResponse):
self.logger.info("Logout successful") self.logger.info("Logout successful")
else:
self.logger.error(f"Logout unsuccessful. msg: {response.message}")
async def close(self):
try: try:
await self.client.close() await self.client.close()
self.logger.info("Connection closed") self.logger.info("Connection closed")
except Exception as e: except Exception as ex:
self.logger.error("error while closing client: %s", e) self.logger.error("error while closing client: %s", ex)
else:
logout: LogoutError
self.logger.error(f"Logout unsuccessful. msg: {logout.message}")
else:
await self.client.client_session.close()
def handle_exit(self, signame, loop): def handle_exit(self, signame, loop):
self.logger.info(f"Received signal {signame}") self.logger.info(f"Received signal {signame}")