Make !close time parser stricter and allow 'd' for days. Format close time better in the confirmation message. Add additional start-up info for missing dependencies.

master
Dragory 2018-03-13 07:24:01 +02:00
parent 5b2bfc6073
commit 5717bf83fd
5 changed files with 37 additions and 13 deletions

6
package-lock.json generated
View File

@ -951,9 +951,9 @@
"integrity": "sha512-pNgbURSuab90KbTqvRPsseaTxOJCZBD0a7t+haSN33piP9cCM4l0CqdzAif2hUqm716UovKB2ROmiabGAKVXyg==" "integrity": "sha512-pNgbURSuab90KbTqvRPsseaTxOJCZBD0a7t+haSN33piP9cCM4l0CqdzAif2hUqm716UovKB2ROmiabGAKVXyg=="
}, },
"humanize-duration": { "humanize-duration": {
"version": "3.10.1", "version": "3.12.1",
"resolved": "https://registry.npmjs.org/humanize-duration/-/humanize-duration-3.10.1.tgz", "resolved": "https://registry.npmjs.org/humanize-duration/-/humanize-duration-3.12.1.tgz",
"integrity": "sha512-FHD+u5OKj8TSsSdMHJxSCC78N5Rt4ecil6sWvI+xPbUKhxvHmkKo/V8imbR1m2dXueZYLIl7PcSYX9i/oEiOIA==" "integrity": "sha512-Eu68Xnq5C38391em1zfVy8tiapQrOvTNTlWpax9smHMlEEUcudXrdMfXMoMRyZx4uODowYgi1AYiMzUXEbG+sA=="
}, },
"ignore": { "ignore": {
"version": "3.3.3", "version": "3.3.3",

View File

@ -12,7 +12,7 @@
"author": "", "author": "",
"dependencies": { "dependencies": {
"eris": "^0.8.4", "eris": "^0.8.4",
"humanize-duration": "^3.10.0", "humanize-duration": "^3.12.1",
"knex": "^0.14.2", "knex": "^0.14.2",
"mime": "^1.3.4", "mime": "^1.3.4",
"moment": "^2.21.0", "moment": "^2.21.0",

View File

@ -12,7 +12,20 @@ const path = require('path');
try { try {
fs.accessSync(path.join(__dirname, '..', 'node_modules')); fs.accessSync(path.join(__dirname, '..', 'node_modules'));
} catch (e) { } 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); process.exit(1);
} }

View File

@ -1,6 +1,6 @@
const Eris = require('eris'); const Eris = require('eris');
const moment = require('moment'); const moment = require('moment');
const transliterate = require('transliteration'); const humanizeDuration = require('humanize-duration');
const config = require('./config'); const config = require('./config');
const bot = require('./bot'); const bot = require('./bot');
@ -23,6 +23,7 @@ const {ACCIDENTAL_THREAD_MESSAGES} = require('./data/constants');
const messageQueue = new Queue(); const messageQueue = new Queue();
const addInboxServerCommand = (...args) => threadUtils.addInboxServerCommand(bot, ...args); 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 // Once the bot has connected, set the status/"playing" message
bot.on('ready', () => { bot.on('ready', () => {
@ -256,7 +257,7 @@ addInboxServerCommand('close', async (msg, args, thread) => {
if (args.length) { if (args.length) {
if (args[0] === 'cancel') { if (args[0] === 'cancel') {
// Cancel timed close // 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) { if (thread.scheduled_close_at) {
await thread.cancelScheduledClose(); await thread.cancelScheduledClose();
thread.postSystemMessage(`Cancelled scheduled closing`); thread.postSystemMessage(`Cancelled scheduled closing`);
@ -267,14 +268,14 @@ addInboxServerCommand('close', async (msg, args, thread) => {
// Set a timed close // Set a timed close
const delay = utils.convertDelayStringToMS(args.join(' ')); const delay = utils.convertDelayStringToMS(args.join(' '));
if (delay === 0) { if (delay === 0 || delay === null) {
thread.postSystemMessage(`Invalid delay specified. Format: "1h30m"`); thread.postSystemMessage(`Invalid delay specified. Format: "1h30m"`);
return; return;
} }
const closeAt = moment.utc().add(delay, 'ms'); const closeAt = moment.utc().add(delay, 'ms');
await thread.scheduleClose(closeAt.format('YYYY-MM-DD HH:mm:ss'), msg.author); 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; return;
} }

View File

@ -203,14 +203,24 @@ function trimAll(str) {
* @returns {Number} * @returns {Number}
*/ */
function convertDelayStringToMS(str) { function convertDelayStringToMS(str) {
const regex = /([0-9]+)\s*([hms])/g; const regex = /^([0-9]+)\s*([dhms])?[a-z]*\s*/;
let match; let match;
let ms = 0; let ms = 0;
while (match = regex.exec(str)) { str = str.trim();
if (match[2] === 'h') ms += match[1] * 1000 * 60 * 60;
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] === '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; return ms;