From d56bc13649b7da67838847e541466f7e971bd7a4 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Fri, 13 Nov 2020 16:52:12 -0500 Subject: [PATCH] Use Sherlock to parse times --- commands/other/timer.js | 13 +++++++------ package.json | 1 + types/sherlock.js | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 types/sherlock.js diff --git a/commands/other/timer.js b/commands/other/timer.js index 2ac03640..fd456119 100644 --- a/commands/other/timer.js +++ b/commands/other/timer.js @@ -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}**.`); } diff --git a/package.json b/package.json index 70300ea0..80876dc0 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/types/sherlock.js b/types/sherlock.js new file mode 100644 index 00000000..67b6c8fb --- /dev/null +++ b/types/sherlock.js @@ -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); + } +};