import { Command , CommandContext } from "../../base/Command" ; import { sendMessage , sendReply , waitResponse } from "../../utils/functions" ; import { stripIndents } from "common-tags" ; import moment from "moment-timezone" ; import { DBUser , userInterface } from "../../models/User" ; import Deps from "../../utils/deps" ; import Logger from "../../utils/logger" ; export default class SetTimeZone implements Command { name = "settimezone" ; aliases = [ "setz" ] ; category = "utility" ; description = "Changes a user's timezone. Used for time specific commands such as `event`." ; usage = "settimezone [timezone]" ; examples = [ "settimezone America/New_York" ] ; enabled = true ; guildOnly = false ; botPermissions = [ ] ; memberPermissions = [ ] ; ownerOnly = false ; premiumOnly = false ; cooldown = 5000 ; DBUserManager : DBUser ; constructor ( ) { this . DBUserManager = Deps . get < DBUser > ( DBUser ) ; } async execute ( ctx , args ) { let userSettings = await this . DBUserManager . get ( ctx . author ) ; if ( args [ 0 ] ) { if ( ! moment . tz . zone ( args [ 0 ] ) ) { sendReply ( ctx . client , "I couldn't understand that timezone, cancelling command. Please check to make sure you copied your timezone correctly." , ctx . msg ) ; return ; } userSettings = await this . DBUserManager . update ( ctx . author , { timezone : args [ 0 ] } ) . catch ( ( err ) => { Logger . log ( "error" , `Error updating user (userID: ${ ctx . author . id }) in database:\n${ err }` ) ; } ) ; if ( ! userSettings ) { sendReply ( ctx . client , `I had trouble updating your settings. Please try again later or contact my developer ${ process . env . OWNERNAME }${ process . env . OWNERTAG }` , ctx . msg ) ; return ; } sendMessage ( ctx . client , `Saved! Your new timezone is \`${ userSettings . settings . timezone }\`.` , ctx . channel ) ; return ; } if ( userSettings . settings . timezone ) { sendMessage ( ctx . client , `Your currently saved timezone is \`${ userSettings . settings . timezone }\`.` , ctx . channel ) ; } const timezonePromptMessage = await sendMessage ( ctx . client , stripIndents `What timezone are you in?
        (Visit https://en.wikipedia.org/wiki/List_of_tz_database_time_zones and copy and paste your \`TZ database name\`)` , ctx . channel ) ; if ( ! timezonePromptMessage ) return ; const timezoneMessage = await waitResponse ( ctx . client , ctx . channel , ctx . author , 120 ) ; if ( ! timezoneMessage ) { sendReply ( ctx . client , "Cancelling settimezone." , timezonePromptMessage ) ; return ; } if ( ! moment . tz . zone ( timezoneMessage . content ) ) { sendReply ( ctx . client , "I couldn't understand that timezone, cancelling command. Please check to make sure you copied your timezone correctly." , timezoneMessage ) ; return ; } userSettings = await this . DBUserManager . update ( ctx . author , { timezone : timezoneMessage . content , } ) . catch ( ( err ) => { Logger . log ( "error" , `Error updating user (userID: ${ ctx . author . id }) in database:\n${ err }` ) ; } ) ; if ( ! userSettings ) { sendReply ( ctx . client , `I had trouble updating your settings. Please try again later or contact my developer ${ process . env . OWNERNAME }${ process . env . OWNERTAG }` , ctx . msg ) ; return ; } sendMessage ( ctx . client , `Saved! Your new timezone is \`${ userSettings . settings . timezone }\`.` , ctx . channel ) ; return ; } }