Browse Source

Converted login to promise based resolution

* Added error handling to room message sending on startup
develop
Drew Short 5 years ago
parent
commit
9146f98793
  1. 49
      bot/bot.js

49
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 {

Loading…
Cancel
Save