diff --git a/package-lock.json b/package-lock.json index 868bdc2..cc936d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -951,9 +951,9 @@ "integrity": "sha512-pNgbURSuab90KbTqvRPsseaTxOJCZBD0a7t+haSN33piP9cCM4l0CqdzAif2hUqm716UovKB2ROmiabGAKVXyg==" }, "humanize-duration": { - "version": "3.10.1", - "resolved": "https://registry.npmjs.org/humanize-duration/-/humanize-duration-3.10.1.tgz", - "integrity": "sha512-FHD+u5OKj8TSsSdMHJxSCC78N5Rt4ecil6sWvI+xPbUKhxvHmkKo/V8imbR1m2dXueZYLIl7PcSYX9i/oEiOIA==" + "version": "3.12.1", + "resolved": "https://registry.npmjs.org/humanize-duration/-/humanize-duration-3.12.1.tgz", + "integrity": "sha512-Eu68Xnq5C38391em1zfVy8tiapQrOvTNTlWpax9smHMlEEUcudXrdMfXMoMRyZx4uODowYgi1AYiMzUXEbG+sA==" }, "ignore": { "version": "3.3.3", diff --git a/package.json b/package.json index 111abda..a312360 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "author": "", "dependencies": { "eris": "^0.8.4", - "humanize-duration": "^3.10.0", + "humanize-duration": "^3.12.1", "knex": "^0.14.2", "mime": "^1.3.4", "moment": "^2.21.0", diff --git a/src/index.js b/src/index.js index 96db8f9..85e0a95 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,20 @@ const path = require('path'); try { fs.accessSync(path.join(__dirname, '..', 'node_modules')); } catch (e) { - console.error('Please run "npm install" before trying to start the bot.'); + console.error('Please run "npm install" before starting the bot'); + process.exit(1); +} + +let testedPackage = ''; +try { + const packageJson = require('../package.json'); + const modules = Object.keys(packageJson.dependencies); + modules.forEach(mod => { + testedPackage = mod; + fs.accessSync(path.join(__dirname, '..', 'node_modules', mod)) + }); +} catch (e) { + console.error(`Please run "npm install" again! Package "${testedPackage}" is missing.`); process.exit(1); } diff --git a/src/main.js b/src/main.js index 157d09e..000c025 100644 --- a/src/main.js +++ b/src/main.js @@ -1,6 +1,6 @@ const Eris = require('eris'); const moment = require('moment'); -const transliterate = require('transliteration'); +const humanizeDuration = require('humanize-duration'); const config = require('./config'); const bot = require('./bot'); @@ -23,6 +23,7 @@ const {ACCIDENTAL_THREAD_MESSAGES} = require('./data/constants'); const messageQueue = new Queue(); const addInboxServerCommand = (...args) => threadUtils.addInboxServerCommand(bot, ...args); +const humanizeDelay = (delay, opts = {}) => humanizeDuration(delay, Object.assign({conjunction: ' and '}, opts)); // Once the bot has connected, set the status/"playing" message bot.on('ready', () => { @@ -256,7 +257,7 @@ addInboxServerCommand('close', async (msg, args, thread) => { if (args.length) { if (args[0] === 'cancel') { // Cancel timed close - // The string type check is due to a knex bug, see https://github.com/tgriesser/knex/issues/1276 + // The string type check is due to a knex bug, see https://github.com/tgriesser/knex/issues/1276git if (thread.scheduled_close_at) { await thread.cancelScheduledClose(); thread.postSystemMessage(`Cancelled scheduled closing`); @@ -267,14 +268,14 @@ addInboxServerCommand('close', async (msg, args, thread) => { // Set a timed close const delay = utils.convertDelayStringToMS(args.join(' ')); - if (delay === 0) { + if (delay === 0 || delay === null) { thread.postSystemMessage(`Invalid delay specified. Format: "1h30m"`); return; } const closeAt = moment.utc().add(delay, 'ms'); await thread.scheduleClose(closeAt.format('YYYY-MM-DD HH:mm:ss'), msg.author); - thread.postSystemMessage(`Thread is scheduled to be closed ${moment.duration(delay).humanize(true)} by ${msg.author.username}. Use \`${config.prefix}close cancel\` to cancel.`); + thread.postSystemMessage(`Thread is now scheduled to be closed in ${humanizeDelay(delay)}. Use \`${config.prefix}close cancel\` to cancel.`); return; } diff --git a/src/utils.js b/src/utils.js index 474247d..8177a5b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -203,14 +203,24 @@ function trimAll(str) { * @returns {Number} */ function convertDelayStringToMS(str) { - const regex = /([0-9]+)\s*([hms])/g; + const regex = /^([0-9]+)\s*([dhms])?[a-z]*\s*/; let match; let ms = 0; - while (match = regex.exec(str)) { - if (match[2] === 'h') ms += match[1] * 1000 * 60 * 60; + str = str.trim(); + + while (str !== '' && (match = str.match(regex)) !== null) { + if (match[2] === 'd') ms += match[1] * 1000 * 60 * 60 * 24; + else if (match[2] === 'h') ms += match[1] * 1000 * 60 * 60; else if (match[2] === 'm') ms += match[1] * 1000 * 60; - else if (match[2] === 's') ms += match[1] * 1000; + else if (match[2] === 's' || ! match[2]) ms += match[1] * 1000; + + str = str.slice(match[0].length); + } + + // Invalid delay string + if (str !== '') { + return null; } return ms;