From 9146f9879381d3e33c0d437145bd59d413f7701f Mon Sep 17 00:00:00 2001 From: Drew Short Date: Thu, 9 Jan 2020 21:20:21 -0600 Subject: [PATCH] Converted login to promise based resolution * Added error handling to room message sending on startup --- bot/bot.js | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/bot/bot.js b/bot/bot.js index e5f335f..a16e93b 100644 --- a/bot/bot.js +++ b/bot/bot.js @@ -64,38 +64,45 @@ class Bot { let botClient = this.client; let botConfig = this.config; - let startServerConnection = async () => { + let startServerConnection = () => { logger.info("Starting Matrix SDK Client"); - await this.client.startClient({ + return this.client.startClient({ initialSyncLimit: 0 }); } - let connectWithPassword = (err, data) => { - if (err === null) { - logger.info("Successfully authenticated with password %o", data); - startServerConnection(); - } else { - logger.error("Failed to authenticate with password %o", err); - } - } - - let connectWithToken = (err, data) => { - if (err === null) { + logger.info("Authenticating With Server"); + if (typeof botConfig.accessToken !== 'undefined') { + await botClient.loginWithToken(this.config.accessToken) + .then((data) => { logger.info("Successfully authenticated with access token %o", data); - startServerConnection(); - } else { + return startServerConnection(); + }, (err) => { logger.error("Failed to authenticate with access token %o", err); if (typeof botConfig.userPassword !== 'undefined') { - botClient.loginWithPassword(botConfig.userId, botConfig.userPassword, connectWithPassword); + return botClient.loginWithPassword(botConfig.userId, botConfig.userPassword) + .then((data) => { + logger.info("Successfully authenticated with password %o", data); + return startServerConnection(); + }, (err) => { + logger.error("Failed to authenticate with password %o", err); + }); } else { logger.error("No fallback password provided!"); } - } + }); + } else if (typeof botConfig.userPassword !== 'undefined') { + await botClient.loginWithPassword(botConfig.userId, botConfig.userPassword) + .then((data) => { + logger.info("Successfully authenticated with password %o", data); + return startServerConnection(); + }, (err) => { + logger.error("Failed to authenticate with password %o", err); + }); + } else { + logger.error("No authentication credentials available!"); + process.exit(1); } - - logger.info("Authenticating With Server"); - botClient.loginWithToken(this.config.accessToken, connectWithToken); } updateAvatar(avatarFile, overwrite = false) { @@ -143,6 +150,8 @@ class Bot { roomId, message.createBasic("Started with version: " + this.buildInfo, message.types.NOTICE) ).then(() => { logger.debug("Notified %s of startup", roomId); + }, (err) => { + logger.error("Unable to send message to room %s because %s", roomId, err.errcode); })); }); } else {