Browse Source

Additional error handling and code cleanup

develop
Drew Short 5 years ago
parent
commit
c13c8fdc64
  1. 5
      README.md
  2. 28
      bot/bot.js

5
README.md

@ -21,7 +21,7 @@ npm install
### Requirements: ### Requirements:
Nodejs 12 LTS
NodeJS 12 LTS
OR OR
@ -32,11 +32,12 @@ Docker
```bash ```bash
npm install npm install
#<make changes> #<make changes>
./entrypoint.sh run
./scripts/run_development_local.sh
``` ```
#### Docker: #### Docker:
```bash ```bash
#<make changes>
./scripts/run_development_docker.sh ./scripts/run_development_docker.sh
``` ```

28
bot/bot.js

@ -71,6 +71,16 @@ class Bot {
}); });
} }
let attemptPasswordLogin = (botClient, botConfig) => {
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);
});
}
logger.info("Authenticating With Server"); logger.info("Authenticating With Server");
if (typeof botConfig.accessToken !== 'undefined') { if (typeof botConfig.accessToken !== 'undefined') {
await botClient.loginWithToken(this.config.accessToken) await botClient.loginWithToken(this.config.accessToken)
@ -80,25 +90,13 @@ class Bot {
}, (err) => { }, (err) => {
logger.error("Failed to authenticate with access token %o", err); logger.error("Failed to authenticate with access token %o", err);
if (typeof botConfig.userPassword !== 'undefined') { if (typeof botConfig.userPassword !== 'undefined') {
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);
});
return attemptPasswordLogin(botClient, botConfig);
} else { } else {
logger.error("No fallback password provided!"); logger.error("No fallback password provided!");
} }
}); });
} else if (typeof botConfig.userPassword !== 'undefined') { } 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);
});
await attemptPasswordLogin(botClient, botConfig);
} else { } else {
logger.error("No authentication credentials available!"); logger.error("No authentication credentials available!");
process.exit(1); process.exit(1);
@ -169,6 +167,8 @@ class Bot {
roomId, message.createBasic("Shutting down", message.types.NOTICE) roomId, message.createBasic("Shutting down", message.types.NOTICE)
).then(() => { ).then(() => {
logger.debug("Notified %s of shutdown", roomId); logger.debug("Notified %s of shutdown", roomId);
}, (err) => {
logger.error("Unable to send message to room %s because %s", roomId, err.errcode);
})); }));
}); });
} else { } else {

Loading…
Cancel
Save