import Configuration from '../models/Configuration' ; import * as DefaultConfig from '../../config/config.json' ; export type GuildConfigurations = Map < string , string > ; /**
 * Takes care of the local storage of configurations per a guild.
 *
 * The bot global configuration is stored under the "bot" key.
 */ export class ConfigurationManager { /**
     * Singleton instance of ConfigurationManager.
     */ private static instance : ConfigurationManager | undefined ; /**
     * Stores configurations per guild.
     * It is a map in the nested order: guild -> key -> value
     */ protected guilds : Map < string , GuildConfigurations > ; protected constructor ( ) { this . guilds = new Map < string , GuildConfigurations > ( ) ; this . guilds . set ( this . resolveGuild ( ) , new Map < string , string > ( ) ) ; } /**
     * Get the instance of ConfigurationManager.
     */ static get ( ) { if ( this . instance === undefined ) { this . instance = new ConfigurationManager ; } return this . instance ; } param results fetchConfigurations ( results ) { for ( const result of results ) { this . updateConfiguration ( result . key , result . value , result . guild ) ; } } param key param guild getConfiguration ( key , guild ? ) { guild = this . resolveGuild ( guild ) ; if ( ! this . guilds . has ( guild ) || ! this . guilds . get ( guild ) . has ( key ) ) { return this . getDefault ( key , guild ) ; } return this . guilds . get ( guild ) . get ( key ) ; } param key param value param guild updateConfiguration ( key , value ? , guild ? ) { guild = this . resolveGuild ( guild ) ; this . addGuild ( guild ) ; this . guilds . get ( guild ) . set ( key , value ) ; } param key param guild removeConfiguration ( key , guild ? ) { guild = this . resolveGuild ( guild ) ; if ( this . guilds . has ( guild ) ) { this . guilds . get ( guild ) . delete ( key ) ; } } param ignoreGlobalConfig returns getGuilds ( ignoreGlobalConfig = false ) { const guilds = new Map ( this . guilds ) ; if ( ignoreGlobalConfig ) { guilds . delete ( this . resolveGuild ( ) ) ; } return guilds . keys ( ) ; } param guild getGuild ( guild ? ) { guild = this . resolveGuild ( guild ) ; return this . guilds . has ( guild ) ? this . guilds . get ( guild ) : new Map < string , string > ( ) ; } param guild addGuild ( guild ) { if ( ! this . guilds . has ( guild ) ) { this . guilds . set ( guild , new Map < string , string > ( ) ) ; } } param guild removeGuild ( guild ) { this . guilds . delete ( this . resolveGuild ( guild ) ) ; } param key param guild isConfigurationModified ( key , guild ? ) { guild = this . resolveGuild ( guild ) ; if ( ! ConfigurationManager . doesConfigurationExist ( key , guild !== 'bot' ) ) { return false ; } const value = ConfigurationManager . get ( ) . getConfiguration ( key , guild ) ; const defaultConfigEntry = ConfigurationManager . getDefaultConfiguration ( key ) ; if ( ! defaultConfigEntry ) { return false ; } return value !== defaultConfigEntry . defaultValue ; } param key param perGuild static doesConfigurationExist ( key , perGuild ? ) { if ( ! DefaultConfig . hasOwnProperty ( key ) ) { return false ; } if ( perGuild === undefined ) { return true ; } const config = this . getDefaultConfiguration ( key ) ; return ( ! perGuild && ! config . guild ) || ( perGuild && config . guild ) ; } param key static getDefaultConfiguration ( key ) { if ( ! this . doesConfigurationExist ( key ) ) { return null ; } return DefaultConfig [ key ] ; } param key static getDefaultValue ( key ) { const configuration = this . getDefaultConfiguration ( key ) ; if ( ! configuration ) { return null ; } return configuration . defaultValue ; } param key static isGuildOnly ( key ) { if ( ! this . doesConfigurationExist ( key , true ) ) { return false ; } const config = this . getDefaultConfiguration ( key ) ; return config . guild ; } param key static isHidden ( key ) { if ( ! this . doesConfigurationExist ( key ) ) { return false ; } const config = this . getDefaultConfiguration ( key ) ; return config . hidden ; } param guild returns protected resolveGuild ( guild ? ) { return ! guild ? 'bot' : guild ; } param key param guild protected getDefault ( key , guild ? ) { if ( ! DefaultConfig . hasOwnProperty ( key ) ) { return null ; } const config = ConfigurationManager . getDefaultConfiguration ( key ) ; if ( ( config . guild && guild === 'bot' ) || ( ! config . guild && guild !== 'bot' ) ) { return null ; } return config . defaultValue ; } }