From 736645d29a0bf447f1e7688993f99bf91cc928ef Mon Sep 17 00:00:00 2001 From: Drew Short Date: Sat, 11 Jan 2020 09:10:32 -0600 Subject: [PATCH] Adding to startup and shutdown messages --- src/bot.ts | 40 +++++++++++++++++++++------------------- src/utility.ts | 2 +- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index a4143ba..f2c8cfe 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -10,11 +10,13 @@ class Bot { config: any; buildInfo: string; connected: boolean; + startTime: Date; constructor(config, buildInfo) { this.config = config; this.buildInfo = buildInfo; this.connected = false; + this.startTime = new Date(); } /** @@ -79,28 +81,28 @@ 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); - }); + .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"); if (typeof botConfig.accessToken !== 'undefined') { await botClient.loginWithToken(this.config.accessToken) - .then((data) => { - logger.info("Successfully authenticated with access token %o", data); - return startServerConnection(); - }, (err) => { - logger.error("Failed to authenticate with access token %o", err); - if (typeof botConfig.userPassword !== 'undefined') { - return attemptPasswordLogin(botClient, botConfig); - } else { - logger.error("No fallback password provided!"); - } - }); + .then((data) => { + logger.info("Successfully authenticated with access token %o", data); + return startServerConnection(); + }, (err) => { + logger.error("Failed to authenticate with access token %o", err); + if (typeof botConfig.userPassword !== 'undefined') { + return attemptPasswordLogin(botClient, botConfig); + } else { + logger.error("No fallback password provided!"); + } + }); } else if (typeof botConfig.userPassword !== 'undefined') { await attemptPasswordLogin(botClient, botConfig); } else { @@ -151,7 +153,7 @@ class Bot { this.config.statusRooms.forEach(roomId => { logger.debug("Notifying %s of startup", roomId); promises.push(this.client.sendMessage( - roomId, message.createBasic("Started with version: " + this.buildInfo, message.types.NOTICE) + roomId, message.createBasic(`Started at ${utility.toISODateString(this.startTime)} with version: ${this.buildInfo}`, message.types.NOTICE) ).then(() => { logger.debug("Notified %s of startup", roomId); }, (err) => { @@ -170,7 +172,7 @@ class Bot { this.config.statusRooms.forEach(roomId => { logger.debug("Notifying %s of shutdown", roomId); promises.push(this.client.sendMessage( - roomId, message.createBasic("Shutting down", message.types.NOTICE) + roomId, message.createBasic(`Shutting down at ${utility.toISODateString(new Date())} with version: ${this.buildInfo}`, message.types.NOTICE) ).then(() => { logger.debug("Notified %s of shutdown", roomId); }, (err) => { diff --git a/src/utility.ts b/src/utility.ts index 9984587..82f03f8 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -29,7 +29,7 @@ function toISODateString(d: Date) { function getBuildInfo() { let buildInfoPath = process.env.NODE_PATH + '/build.info'; try { - return fs.readFileSync(buildInfoPath, "utf8"); + return fs.readFileSync(buildInfoPath, "utf8").trim(); } catch (err) { if (err.code === 'ENOENT') { return "UNKNOWN_" + toISODateString(new Date());