From 5485ad138f6f7a5239989fdcac72b6846b09cd20 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Thu, 22 Feb 2018 00:25:05 +0000 Subject: [PATCH] Month type --- assets/json/{months.json => month.json} | 0 commands/events/days-until.js | 21 +++++++++++---------- commands/events/google-doodle.js | 6 ++---- commands/search/zodiac-sign.js | 14 +------------- package.json | 2 +- types/month.js | 23 +++++++++++++++++++++++ 6 files changed, 38 insertions(+), 28 deletions(-) rename assets/json/{months.json => month.json} (100%) create mode 100644 types/month.js diff --git a/assets/json/months.json b/assets/json/month.json similarity index 100% rename from assets/json/months.json rename to assets/json/month.json diff --git a/commands/events/days-until.js b/commands/events/days-until.js index d94db079..c4df10a3 100644 --- a/commands/events/days-until.js +++ b/commands/events/days-until.js @@ -4,26 +4,27 @@ module.exports = class DaysUntilCommand extends Command { constructor(client) { super(client, { name: 'days-until', - aliases: ['days-until-christmas'], group: 'events', memberName: 'days-until', description: 'Responds with how many days there are until a certain date.', args: [ { - key: 'date', - prompt: 'What date do you want to get the days until? Month/Day format.', - type: 'string', - default: ['12', '25'], - parse: date => date.split('/') + key: 'month', + prompt: 'What month would you like to get the days until?', + type: 'month' + }, + { + key: 'day', + prompt: 'What day would you like to get the days until?', + type: 'integer', + min: 1, + max: 12 } ] }); } - run(msg, { date }) { - const month = Number.parseInt(date[0], 10); - const day = Number.parseInt(date[1], 10); - if (!month || !day) return msg.reply('Invalid date.'); + run(msg, { month, day }) { const now = new Date(); let year = now.getMonth() + 1 <= month ? now.getFullYear() : now.getFullYear() + 1; if (month === now.getMonth() + 1 && now.getDate() >= day) ++year; diff --git a/commands/events/google-doodle.js b/commands/events/google-doodle.js index eecf5f00..c40ffc4d 100644 --- a/commands/events/google-doodle.js +++ b/commands/events/google-doodle.js @@ -13,10 +13,8 @@ module.exports = class GoogleDoodleCommand extends Command { { key: 'month', prompt: 'What month would you like to get doodles for?', - type: 'integer', - default: 'latest', - min: 1, - max: 12 + type: 'month', + default: 'latest' }, { key: 'year', diff --git a/commands/search/zodiac-sign.js b/commands/search/zodiac-sign.js index 9e023568..f0bf83dc 100644 --- a/commands/search/zodiac-sign.js +++ b/commands/search/zodiac-sign.js @@ -1,5 +1,4 @@ const { Command } = require('discord.js-commando'); -const months = require('../../assets/json/months'); module.exports = class ZodiacSignCommand extends Command { constructor(client) { @@ -12,18 +11,7 @@ module.exports = class ZodiacSignCommand extends Command { { key: 'month', prompt: 'What month would you like to get the Zodiac Sign for?', - type: 'string', - validate: month => { - const num = Number.parseInt(month, 10); - if (num > 0 && num < 13) return true; - if (months.includes(month.toLowerCase())) return true; - return 'Please enter a valid month name or number.'; - }, - parse: month => { - const num = Number.parseInt(month, 10); - if (!Number.isNaN(num)) return num; - return months.indexOf(month.toLowerCase()) + 1; - } + type: 'month' }, { key: 'day', diff --git a/package.json b/package.json index 3e4909b7..4a65a462 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "65.4.1", + "version": "66.0.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/types/month.js b/types/month.js new file mode 100644 index 00000000..f72ae1c0 --- /dev/null +++ b/types/month.js @@ -0,0 +1,23 @@ +const { ArgumentType } = require('discord.js-commando'); +const months = require('../assets/json/month'); + +class MonthArgumentType extends ArgumentType { + constructor(client) { + super(client, 'month'); + } + + validate(value) { + const num = Number.parseInt(value, 10); + if (num > 0 && num < 13) return true; + if (months.includes(value.toLowerCase())) return true; + return false; + } + + parse(value) { + const num = Number.parseInt(value, 10); + if (!Number.isNaN(num)) return num; + return months.indexOf(value.toLowerCase()) + 1; + } +} + +module.exports = MonthArgumentType;