Add city timezone lookup

This commit is contained in:
Dragon Fire
2021-04-24 14:24:20 -04:00
parent c20e727bfd
commit 65beca4226
4 changed files with 38 additions and 28 deletions
+1 -12
View File
@@ -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 <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones>.
${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], /[_ ]/);
+1 -12
View File
@@ -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 <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones>.
${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], /[_ ]/);
+5 -4
View File
@@ -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",
+31
View File
@@ -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;
}
};