Use Sherlock to parse times

This commit is contained in:
Dragon Fire
2020-11-13 16:52:12 -05:00
parent f9aea25916
commit d56bc13649
3 changed files with 26 additions and 6 deletions
+7 -6
View File
@@ -1,4 +1,5 @@
const Command = require('../../structures/Command');
const moment = require('moment');
module.exports = class TimerCommand extends Command {
constructor(client) {
@@ -10,10 +11,8 @@ module.exports = class TimerCommand extends Command {
args: [
{
key: 'time',
prompt: 'How long should the timer last (in seconds)?',
type: 'integer',
max: 600,
min: 1
prompt: 'How long should the timer last?',
type: 'sherlock'
}
]
});
@@ -23,11 +22,13 @@ module.exports = class TimerCommand extends Command {
run(msg, { time }) {
if (this.timers.has(msg.channel.id)) return msg.reply('Only one timer can be set per channel.');
const display = time > 59 ? `${Math.trunc(time / 60)} minutes, ${time % 60} seconds` : `${time} seconds`;
const timeMs = time.startDate.getTime() - Date.now();
if (timeMs > 600000) return msg.reply('Times above 10 minutes are not currently supported. Sorry!');
const display = moment().add(timeMs, 'ms').fromNow();
const timeout = setTimeout(async () => {
await msg.channel.send(`🕰️ Your **${display}** timer is finished ${msg.author}!`);
this.timers.delete(msg.channel.id);
}, time * 1000);
}, timeMs);
this.timers.set(msg.channel.id, timeout);
return msg.say(`🕰️ Set a timer for **${display}**.`);
}
+1
View File
@@ -57,6 +57,7 @@
"pokersolver": "^2.1.4",
"random-js": "^2.1.0",
"rss-parser": "^3.9.0",
"sherlockjs": "^1.4.0",
"stackblur-canvas": "^2.4.0",
"tesseract.js": "^2.1.4",
"winston": "^3.3.3"
+18
View File
@@ -0,0 +1,18 @@
const { ArgumentType } = require('discord.js-commando');
const sherlock = require('sherlockjs');
module.exports = class SherlockType extends ArgumentType {
constructor(client) {
super(client, 'sherlock');
}
validate(value) {
const time = sherlock.parse(value);
if (!time.startDate) return `Please provide a valid starting time.`;
return true;
}
parse(value) {
return sherlock.parse(value);
}
};