From e44ee780515b894db25a8b1e9a15525bbf52cbf7 Mon Sep 17 00:00:00 2001 From: Executive Dronpes Date: Sun, 12 Mar 2017 17:36:03 +0000 Subject: [PATCH 1/2] Remove moment --- package.json | 1 - plugins/seen.js | 3 +-- tools.js | 20 +++++++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index a13c439..3b1c81d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,6 @@ "description": "Pokémon Showdown Bot", "main": "app.js", "dependencies": { - "moment": "^2.17.1", "mysql": "^2.13.0", "node-static": "^0.7.9", "sqlite3": "^3.1.8", diff --git a/plugins/seen.js b/plugins/seen.js index f401802..7db0fc9 100644 --- a/plugins/seen.js +++ b/plugins/seen.js @@ -32,7 +32,6 @@ if (!Config.mysql) { }); } -const moment = require('moment'); const uuid = require('uuid/v4'); let lastUpdated = {}; @@ -125,7 +124,7 @@ exports.commands = { } else { lastSeen(targetid, data => { if (!data) return this.sendReply(Tools.sanitize(target) + " has never been seen before."); - return this.sendReply(Tools.sanitize(data.name) + " was last seen " + data.action + (data.action !== "joining" && data.action !== "leaving" ? " in " : " ") + " **" + data.room + "** on **" + data.server + "** " + moment(data.date).fromNow()); + return this.sendReply(Tools.sanitize(data.name) + " was last seen " + data.action + (data.action !== "joining" && data.action !== "leaving" ? " in " : " ") + " **" + data.room + "** on **" + data.server + "** " + Tools.toDurationString(Date.now() - data.date) + " ago"); }); } }, diff --git a/tools.js b/tools.js index 90e9d7b..02d88f6 100644 --- a/tools.js +++ b/tools.js @@ -8,7 +8,6 @@ let regdateCache = {}; try { regdateCache = JSON.parse(fs.readFileSync('config/regdate.json', 'utf8')); } catch (e) {} - module.exports = { sanitize: function (message) { if (message.charAt(0) === '/') message = '/' + message; @@ -106,4 +105,23 @@ module.exports = { req.write(toUpload); req.end(); }, + toDurationString: function (number, options) { + const date = new Date(+number); + const parts = [date.getUTCFullYear() - 1970, date.getUTCMonth(), date.getUTCDate() - 1, date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()]; + const roundingBoundaries = [6, 15, 12, 30, 30]; + const unitNames = ["second", "minute", "hour", "day", "month", "year"]; + const positiveIndex = parts.findIndex(elem => elem > 0); + const precision = (options && options.precision ? options.precision : parts.length); + if (options && options.hhmmss) { + let string = parts.slice(positiveIndex).map(value => value < 10 ? "0" + value : "" + value).join(":"); + return string.length === 2 ? "00:" + string : string; + } + // round least significant displayed unit + if (positiveIndex + precision < parts.length && precision > 0 && positiveIndex >= 0) { + if (parts[positiveIndex + precision] >= roundingBoundaries[positiveIndex + precision - 1]) { + parts[positiveIndex + precision - 1]++; + } + } + return parts.slice(positiveIndex).reverse().map((value, index) => value ? value + " " + unitNames[index] + (value > 1 ? "s" : "") : "").reverse().slice(0, precision).join(" ").trim(); + } }; From fb3dfb43f712e145ab310c4f19aed1f8dc5e65e9 Mon Sep 17 00:00:00 2001 From: Executive Dronpes Date: Sun, 12 Mar 2017 17:44:25 +0000 Subject: [PATCH 2/2] Fix Silly Errors --- plugins/seen.js | 2 +- tools.js | 29 +++++++++-------------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/plugins/seen.js b/plugins/seen.js index 7db0fc9..1fede7f 100644 --- a/plugins/seen.js +++ b/plugins/seen.js @@ -124,7 +124,7 @@ exports.commands = { } else { lastSeen(targetid, data => { if (!data) return this.sendReply(Tools.sanitize(target) + " has never been seen before."); - return this.sendReply(Tools.sanitize(data.name) + " was last seen " + data.action + (data.action !== "joining" && data.action !== "leaving" ? " in " : " ") + " **" + data.room + "** on **" + data.server + "** " + Tools.toDurationString(Date.now() - data.date) + " ago"); + return this.sendReply(Tools.sanitize(data.name) + " was last seen " + data.action + (data.action !== "joining" && data.action !== "leaving" ? " in " : " ") + " **" + data.room + "** on **" + data.server + "** " + Tools.toDurationString((Date.now() - data.date), {precision: true}) + " ago"); }); } }, diff --git a/tools.js b/tools.js index 02d88f6..d76c2de 100644 --- a/tools.js +++ b/tools.js @@ -72,13 +72,21 @@ module.exports = { // https://github.com/tc39/ecma402/issues/47 const date = new Date(+number); const parts = [date.getUTCFullYear() - 1970, date.getUTCMonth(), date.getUTCDate() - 1, date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()]; + const roundingBoundaries = [6, 15, 12, 30, 30]; const unitNames = ["second", "minute", "hour", "day", "month", "year"]; const positiveIndex = parts.findIndex(elem => elem > 0); + const precision = (options && options.precision ? options.precision : parts.length); if (options && options.hhmmss) { let string = parts.slice(positiveIndex).map(value => value < 10 ? "0" + value : "" + value).join(":"); return string.length === 2 ? "00:" + string : string; } - return parts.slice(positiveIndex).reverse().map((value, index) => value ? value + " " + unitNames[index] + (value > 1 ? "s" : "") : "").reverse().join(" ").trim(); + // round least significant displayed unit + if (positiveIndex + precision < parts.length && precision > 0 && positiveIndex >= 0) { + if (parts[positiveIndex + precision] >= roundingBoundaries[positiveIndex + precision - 1]) { + parts[positiveIndex + precision - 1]++; + } + } + return parts.slice(positiveIndex).reverse().map((value, index) => value ? value + " " + unitNames[index] + (value > 1 ? "s" : "") : "").reverse().slice(0, precision).join(" ").trim(); }, // uploadToHastebin function by TalkTakesTime (https://github.com/TalkTakesTime/Pokemon-Showdown-Bot) uploadToHastebin: function (toUpload, callback) { @@ -105,23 +113,4 @@ module.exports = { req.write(toUpload); req.end(); }, - toDurationString: function (number, options) { - const date = new Date(+number); - const parts = [date.getUTCFullYear() - 1970, date.getUTCMonth(), date.getUTCDate() - 1, date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()]; - const roundingBoundaries = [6, 15, 12, 30, 30]; - const unitNames = ["second", "minute", "hour", "day", "month", "year"]; - const positiveIndex = parts.findIndex(elem => elem > 0); - const precision = (options && options.precision ? options.precision : parts.length); - if (options && options.hhmmss) { - let string = parts.slice(positiveIndex).map(value => value < 10 ? "0" + value : "" + value).join(":"); - return string.length === 2 ? "00:" + string : string; - } - // round least significant displayed unit - if (positiveIndex + precision < parts.length && precision > 0 && positiveIndex >= 0) { - if (parts[positiveIndex + precision] >= roundingBoundaries[positiveIndex + precision - 1]) { - parts[positiveIndex + precision - 1]++; - } - } - return parts.slice(positiveIndex).reverse().map((value, index) => value ? value + " " + unitNames[index] + (value > 1 ? "s" : "") : "").reverse().slice(0, precision).join(" ").trim(); - } };