import type { EmbedOptions , Emoji , Member , Message , TextChannel } from "eris" ; import type { LocaleSystem } from "../../classes/Locale" ; import type { ResponseData } from "../../typings/utils" ; import { Command } from "../../classes/Command" ; import { askForLocale , askForValue , askYesNo } from "../../utils/ask" ; import { localizeItemTypes , localizeProfileItems } from "../../utils/format" ; import { validItems } from "../../utils/validItems" ; import { timeoutHandler , waitFor } from "../../utils/waitFor" ; const deleteEmoji = "🗑" ; const items = validItems . filter ( ( item ) => item . category === "profile" ) ; export class ProfileCommand extends Command { description = "Updates or views your profile's configuration." ; aliases = [ "locale" , "usercfg" , "userconfig" ] ; cooldown = 5000 ; allowdisable = false ; async run ( msg ) { const localeEmojis = { } ; const localeNames = { } ; Object . keys ( this . bot . localeSystem . locales ) . forEach ( ( locale ) => { localeEmojis [ this . bot . localeSystem . getLocale ( locale , "EMOJI" ) ] = this . bot . localeSystem . getLocale ( locale , "NAME" ) ; localeNames [ this . bot . localeSystem . getLocale ( locale , "EMOJI" ) ] = locale ; } ) ; let userconfig = await this . bot . db . getUserConfig ( msg . author . id ) ; if ( ! userconfig ) { await this . bot . db . insertBlankUserConfig ( msg . author . id ) ; userconfig = { id : msg . author . id } ; } const pronouns = [ msg . string ( "global.NO_PREFERENCE" ) , msg . string ( "global.PRONOUNS_HE" ) , msg . string ( "global.PRONOUNS_SHE" ) , msg . string ( "global.PRONOUNS_THEY" ) , ] ; const pronounEmojis = { "1️⃣" : pronouns [ 0 ] , "2️⃣" : pronouns [ 1 ] , "3️⃣" : pronouns [ 2 ] , "4️⃣" : pronouns [ 3 ] , } ; const emojiArray = Object . keys ( pronounEmojis ) ; let reacting = false ; function localizeItem ( item , localeSystem ? ) { if ( item . type === "pronouns" && typeof userconfig [ item . id ] != "undefined" ) return pronouns [ userconfig [ item . id ] ] ; else if ( item . type === "locale" && userconfig [ item . id ] && localeSystem ?. getLocale ) { return localeSystem . getLocale ( userconfig [ item . id ] , "NAME" ) ; } else if ( item . id === "gayLevel" && userconfig [ item . id ] ) return `${ userconfig [ item . id ] }%` ; else if ( userconfig [ item . id ] ) return userconfig [ item . id ] ; } function editEmbed ( localeSystem ? ) { const primaryEmbed = { embed : { title : `👤 ${ msg . string ( "general.PROFILE" ) }` , description : msg . string ( "general.PROFILE_DESCRIPTION" ) , color : msg . convertHex ( "general" ) , fields : ( items as any ) . concat ( [ { emoji : deleteEmoji , label : msg . string ( "global.DELETE" ) , type : "delete" , id : "delete" } ] ) . map ( ( item ) => ( { name : `${ item . emoji } ${ localizeProfileItems ( msg . string , item . id , true ) }` , value : localizeItem ( item , localeSystem ) || localizeProfileItems ( msg . string , item . id ) , } ) ) , footer : { text : msg . string ( "global.RAN_BY" , { author : msg . tagUser ( msg . author ) } ) , icon_url : msg . author . dynamicAvatarURL ( ) , } , } as EmbedOptions , } ; return primaryEmbed ; } const omsg = await msg . channel . createMessage ( editEmbed ( this . bot . localeSystem ) ) ; async function addEmojis ( ) { for await ( const item of items ) { if ( ! reacting ) await omsg . addReaction ( item . emoji ) ; } if ( ! reacting ) await omsg . addReaction ( deleteEmoji ) ; } addEmojis ( ) ; await waitFor ( "messageReactionAdd" , 120000 , async ( m , emoji , user ) => { if ( m . id !== omsg . id ) return ; if ( user . id !== msg . author . id ) return ; if ( ! emoji . name ) return ; if ( reacting ) return ; if ( emoji . name === deleteEmoji ) { omsg . editEmbed ( `❓ ${ msg . string ( "global.CONFIRMATION" ) }` , msg . string ( "general.PROFILE_DELETE_CONFIRMATION" ) ) ; const response = ( await askYesNo ( this . bot , msg . string , msg . author . id , msg . channel . id ) . catch ( ( err ) => timeoutHandler ( err , msg , msg . string ) ) ) as ResponseData ; if ( ! response || response ?. response === false ) { omsg . editEmbed ( msg . string ( "global.CANCELLED" ) , msg . string ( "general.PROFILE_CANCELLED_DELETING" ) , "error" ) ; } else { await this . bot . db . deleteUserConfig ( msg . author . id ) ; userconfig = { id : msg . author . id } ; omsg . removeReaction ( deleteEmoji , user . id ) ; omsg . editEmbed ( msg . string ( "global.SUCCESS" ) , msg . string ( "general.PROFILE_DELETED" ) , "success" ) ; } setTimeout ( ( ) => { omsg . edit ( editEmbed ( this . bot . localeSystem ) ) ; } , 3000 ) ; } const setting = items . find ( ( item ) => item . emoji === emoji . name ) ; if ( ! setting ) return ; await omsg . removeReaction ( setting . emoji , user . id ) ; if ( setting . type === "boolean" ) { if ( typeof userconfig [ setting . id ] === "boolean" ) userconfig [ setting . id ] = ! userconfig [ setting . id ] ; else userconfig [ setting . id ] = typeof setting . default === "undefined" ? true : ! setting . default ; await this . bot . db . updateUserConfig ( msg . author . id , userconfig ) ; omsg . editEmbed ( localizeProfileItems ( msg . string , setting . id , true ) , `${ setting . id } ${ msg . string ( "global.HAS_BEEN" ) } **${ userconfig [ setting . id ] ? `${ msg . string ( "global.ENABLED" ) }` : `${ msg . string ( "global.DISABLED" ) }` }**.` , "success" ) ; reacting = false ; addEmojis ( ) ; setTimeout ( ( ) => { omsg . edit ( editEmbed ( this . bot . localeSystem ) ) ; } , 3000 ) ; } else if ( setting . type === "pronouns" ) { reacting = true ; await omsg . removeReactions ( ) ; emojiArray . forEach ( async ( emoji ) => omsg . addReaction ( emoji ) ) ; omsg . editEmbed ( msg . string ( "general.PROFILE_REACT_PRONOUNS" ) , Object . entries ( pronounEmojis ) . map ( ( p ) => `${ p [ 0 ] }: ${ p [ 1 ] }` ) . join ( "\n" ) ) ; await waitFor ( "messageReactionAdd" , 15000 , async ( _m , _emoji , _user ) => { if ( _m . id !== omsg . id ) return ; if ( _user . id !== msg . author . id ) return ; if ( ! emoji . name ) return ; const pronounss = pronouns . indexOf ( pronounEmojis [ _emoji . name ] ) ; if ( typeof pronounss === "undefined" ) return ; userconfig . pronouns = pronounss ; this . bot . db . updateUserConfig ( msg . author . id , userconfig ) ; await omsg . edit ( editEmbed ( this . bot . localeSystem ) ) ; await omsg . removeReactions ( ) ; reacting = false ; addEmojis ( ) ; return true ; } , this . bot ) . catch ( ( err ) => { reacting = false ; if ( err !== "timeout" ) throw err ; } ) ; } else if ( setting . type === "locale" ) { reacting = true ; await askForLocale ( omsg , msg , this . bot , userconfig , editEmbed ) ; reacting = false ; addEmojis ( ) ; } else { omsg . editEmbed ( `👤 ${ msg . string ( "general.PROFILE" ) }` , localizeItemTypes ( msg . string , setting . type ) ) ; await askForValue ( msg , omsg , this . bot , "profile" , userconfig , editEmbed , setting ) ; } } , this . bot ) . catch ( ( err ) => timeoutHandler ( err , omsg , msg . string ) ) ; } }