diff --git a/commands/edit-image/analog-clock.js b/commands/edit-image/analog-clock.js index 647810b9..ccd7f243 100644 --- a/commands/edit-image/analog-clock.js +++ b/commands/edit-image/analog-clock.js @@ -1,8 +1,6 @@ const Command = require('../../structures/Command'); const { createCanvas } = require('canvas'); const moment = require('moment-timezone'); -const { default: didYouMean, ReturnTypeEnums } = require('didyoumean2'); -const { stripIndents } = require('common-tags'); const { firstUpperCase } = require('../../util/Util'); module.exports = class AnalogClockCommand extends Command { @@ -38,22 +36,13 @@ module.exports = class AnalogClockCommand extends Command { key: 'timeZone', label: 'time zone', prompt: 'Which time zone do you want to get the time of?', - type: 'string', - parse: timeZone => timeZone.replaceAll(' ', '_').toLowerCase() + type: 'timezone' } ] }); } run(msg, { timeZone }) { - if (!moment.tz.zone(timeZone)) { - const results = didYouMean(timeZone, moment.tz.names(), { returnType: ReturnTypeEnums.ALL_SORTED_MATCHES }); - return msg.reply(stripIndents` - Invalid time zone. Refer to . - - ${results.length ? `Did You Mean: ${results.slice(0, 5).map(c => `\`${c}\``).join(', ')}` : ''} - `); - } const time = moment().tz(timeZone); const location = timeZone.split('/'); const main = firstUpperCase(location[0], /[_ ]/); diff --git a/commands/events/time.js b/commands/events/time.js index d57860d8..b8498baf 100644 --- a/commands/events/time.js +++ b/commands/events/time.js @@ -1,7 +1,5 @@ const Command = require('../../structures/Command'); const moment = require('moment-timezone'); -const { default: didYouMean, ReturnTypeEnums } = require('didyoumean2'); -const { stripIndents } = require('common-tags'); const { firstUpperCase } = require('../../util/Util'); module.exports = class TimeCommand extends Command { @@ -31,22 +29,13 @@ module.exports = class TimeCommand extends Command { key: 'timeZone', label: 'time zone', prompt: 'Which time zone do you want to get the time of?', - type: 'string', - parse: timeZone => timeZone.replaceAll(' ', '_').toLowerCase() + type: 'timezone' } ] }); } run(msg, { timeZone }) { - if (!moment.tz.zone(timeZone)) { - const results = didYouMean(timeZone, moment.tz.names(), { returnType: ReturnTypeEnums.ALL_SORTED_MATCHES }); - return msg.reply(stripIndents` - Invalid time zone. Refer to . - - ${results.length ? `Did You Mean: ${results.slice(0, 5).map(c => `\`${c}\``).join(', ')}` : ''} - `); - } const time = moment().tz(timeZone).format('h:mm A'); const location = timeZone.split('/'); const main = firstUpperCase(location[0], /[_ ]/); diff --git a/package.json b/package.json index e978aa5c..dc2afb13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "136.6.0", + "version": "136.6.1", "description": "Your personal server companion.", "main": "Xiao.js", "private": true, @@ -39,7 +39,8 @@ "aki-api": "^5.2.1", "bombsweeper.js": "^1.0.1", "canvas": "^2.7.0", - "cheerio": "1.0.0-rc.6", + "cheerio": "^1.0.0-rc.6", + "city-timezones": "^1.2.0", "cloc": "^2.7.0", "common-tags": "^1.8.0", "connect4-ai": "^0.1.3", @@ -59,7 +60,7 @@ "gm": "^1.23.1", "html-entities": "^2.3.2", "image-to-ascii": "^3.0.13", - "ioredis": "^4.26.0", + "ioredis": "^4.27.1", "js-beautify": "^1.13.13", "js-chess-engine": "^0.11.3", "jszip": "^3.6.0", @@ -83,7 +84,7 @@ "text-diff": "^1.0.1", "tictactoe-minimax-ai": "^1.2.1", "twemoji-parser": "^13.0.0", - "user-agents": "^1.0.631", + "user-agents": "^1.0.632", "valid-url": "^1.0.9", "wavefile": "^11.0.0", "winston": "^3.3.3", diff --git a/types/timezone.js b/types/timezone.js new file mode 100644 index 00000000..55792b3a --- /dev/null +++ b/types/timezone.js @@ -0,0 +1,31 @@ +const { ArgumentType } = require('discord.js-commando'); +const cityTimezones = require('city-timezones'); +const moment = require('moment-timezone'); + +module.exports = class TimezoneType extends ArgumentType { + constructor(client) { + super(client, 'timezone'); + } + + validate(value) { + value = value.replaceAll(' ', '_').toLowerCase(); + const directZone = moment.tz.zone(value); + if (directZone) return true; + const cityZone = cityTimezones.lookupViaCity(value); + if (cityZone.length) return true; + const provZone = cityTimezones.findFromCityStateProvince(value); + if (provZone.length) return true; + return false; + } + + parse(value) { + value = value.replaceAll(' ', '_').toLowerCase(); + const directZone = moment.tz.zone(value); + if (directZone) return directZone.name; + const cityZone = cityTimezones.lookupViaCity(value); + if (cityZone.length) return cityZone[0].timezone; + const provZone = cityTimezones.findFromCityStateProvince(value); + if (provZone.length) return provZone[0].timezone; + return null; + } +};