Files
xiao/types/timezone.js
T
2021-04-24 14:24:20 -04:00

32 lines
1.0 KiB
JavaScript

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;
}
};