import * as Discord from 'discord.js' ; import * as DefaultConfig from '../../../config/config.json' ; import { Command , CommandoClient , CommandoMessage } from 'discord.js-commando' ; import { PermissionsManager } from '../../managers/permission.manager' ; import { ConfigurationManager } from '../../managers/configuration.manager' ; import { Util } from '../../util/Util' ; import Configuration from '../../models/Configuration' ; module . exports = class ConfigCommand extends Command { constructor ( client ) { super ( client , { name : 'config' , group : 'manage' , memberName : 'config' , description : 'Configure the bot. Use the `config` command for more information.' , examples : [ 'config' , 'config options' , 'config get' , 'config get limit' , 'config set limit 10' , 'config remove limit' , ] , userPermissions : [ 'ADMINISTRATOR' ] , clientPermissions : [ 'EMBED_LINKS' ] , guildOnly : true , throttling : { usages : 5 , duration : 15 , } , args : [ { key : 'action' , prompt : 'Action to be taken regarding the configuration of the bot.' , type : 'string' , default : '' , } , { key : 'key' , prompt : 'Key of the configuration.' , type : 'string' , default : '' , } , { key : 'value' , prompt : 'Value of the configuration.' , type : 'string' , default : '' , } , ] , } ) ; } hasPermission ( message , ownerOverride ? ) { if ( PermissionsManager . isGuildManager ( message . member ) ) { return true ; } return super . hasPermission ( message , ownerOverride ) ; } async run ( message , args ) { if ( ! args . action ) { return message . say ( this . createHelp ( message . guild . commandPrefix ) ) ; } if ( args . action === 'options' ) { return this . displayOptions ( message ) ; } if ( args . action === 'get' ) { if ( args . key === '' ) { return this . getConfigurations ( message ) ; } return this . getConfiguration ( message , args . key ) ; } if ( args . action === 'set' ) { return this . setConfiguration ( message , args . key , args . value ) ; } if ( args . action === 'remove' ) { return this . removeConfiguration ( message , args . key ) ; } return message . replyEmbed ( this . createHelp ( message . guild . commandPrefix ) , 'the action is invalid!' ) ; } param message protected async displayOptions ( message ) { const fields = [ ] ; for ( const key in DefaultConfig ) { const config = ConfigurationManager . getDefaultConfiguration ( key ) ; if ( config . hidden ) { continue ; } fields . push ( { name : `${ config . name } (${ key })` + ( ! config . guild ? ' (**BOT**)' : '' ) , value : config . description + ( config . defaultValue ? `\nDefault value: \`${ config . defaultValue }\`` : '' ) , } ) ; } const embeds = Util . createEmbeds ( { color : global . BOT_COLOR , title : 'Configuration Options' } , fields ) ; for ( const embed of embeds ) { await message . embed ( embed ) ; } return null ; } param message param key protected async getConfiguration ( message , key ) { if ( key === '' ) { return message . replyEmbed ( this . createHelp ( message . guild . commandPrefix ) , 'no key has been provided!' ) ; } if ( ! ConfigurationManager . doesConfigurationExist ( key ) || ConfigurationManager . isHidden ( key ) ) { return message . reply ( 'the configuration of this key could not be found!' ) ; } let value = ConfigurationManager . get ( ) . getConfiguration ( key ) ; if ( ConfigurationManager . isGuildOnly ( key ) ) { value = ConfigurationManager . get ( ) . getConfiguration ( key , message . guild . id ) ; } const embed = new Discord . MessageEmbed ( ) . setTitle ( 'Configuration Variable' ) . setColor ( global . BOT_COLOR ) . setDescription ( `\`${ key }\` is set to ${ value }` ) ; return message . say ( embed ) ; } param message protected async getConfigurations ( message ) { const fields = [ ] ; ConfigurationManager . get ( ) . getGuild ( ) . forEach ( ( value , key ) => { if ( ! ConfigurationManager . isHidden ( key ) ) { fields . push ( { name : `${ key } (**BOT**)` , value : `\`${ value }\`` } ) ; } } ) ; ConfigurationManager . get ( ) . getGuild ( message . guild . id ) . forEach ( ( value , key ) => { if ( ! ConfigurationManager . isHidden ( key ) ) { fields . push ( { name : key , value : `\`${ value }\`` } ) ; } } ) ; const title = 'Configuration Values' ; if ( fields . length === 0 ) { const embed = new Discord . MessageEmbed ( ) . setTitle ( title ) . setColor ( global . BOT_COLOR ) . setDescription ( 'No configuration values have been modified.' ) ; return message . say ( embed ) ; } const embeds = Util . createEmbeds ( { color : global . BOT_COLOR , title } , fields ) ; for ( const embed of embeds ) { await message . embed ( embed ) ; } return null ; } param message param key param value protected async setConfiguration ( message , key , value ) { if ( key === '' ) { return message . replyEmbed ( this . createHelp ( message . guild . commandPrefix ) , 'no key has been provided!' ) ; } if ( value === '' ) { return message . replyEmbed ( this . createHelp ( message . guild . commandPrefix ) , 'no value has been provided!' ) ; } if ( ! ConfigurationManager . doesConfigurationExist ( key ) || ConfigurationManager . isHidden ( key ) ) { return message . reply ( 'the configuration of this key could not be found!' ) ; } if ( ! ConfigurationManager . isGuildOnly ( key ) && ! this . client . isOwner ( message . author ) ) { return message . reply ( 'you do not have sufficient permissions to change this configuration value!' ) ; } const guild = ConfigurationManager . isGuildOnly ( key ) ? message . guild . id : null ; await Configuration . setConfiguration ( key , value , guild ) ; ConfigurationManager . get ( ) . updateConfiguration ( key , value , guild ) ; const embed = new Discord . MessageEmbed ( ) . setTitle ( 'Success' ) . setColor ( global . SUCCESS_COLOR ) . setDescription ( `Set \`${ key }\` to \`${ value }\`` ) ; return message . say ( embed ) ; } param message param key protected async removeConfiguration ( message , key ) { if ( key === '' ) { return message . replyEmbed ( this . createHelp ( message . guild . commandPrefix ) , 'no key has been provided!' ) ; } if ( ! ConfigurationManager . doesConfigurationExist ( key ) || ConfigurationManager . isHidden ( key ) ) { return message . reply ( 'the configuration of this key could not be found!' ) ; } if ( ! ConfigurationManager . isGuildOnly ( key ) && ! this . client . isOwner ( message . author ) ) { return message . reply ( 'you do not have sufficient permissions to reset this configuration value!' ) ; } const guild = ConfigurationManager . isGuildOnly ( key ) ? message . guild . id : null ; if ( await Configuration . deleteConfiguration ( key , guild ) === 0 ) { if ( ! this . client . isOwner ( message . author ) || await Configuration . deleteConfiguration ( key ) === 0 ) { return message . reply ( 'the configuration of this key could not be found!' ) ; } } ConfigurationManager . get ( ) . removeConfiguration ( key , guild ) ; const embed = new Discord . MessageEmbed ( ) . setTitle ( 'Success' ) . setColor ( global . SUCCESS_COLOR ) . setDescription ( `Set \`${ key }\` to the default value` ) ; return message . say ( embed ) ; } /**
     * Create an embed containing help for the command.
     */ protected createHelp ( prefix ) { return new Discord . MessageEmbed ( ) . setTitle ( 'Configuration' ) . setColor ( global . BOT_COLOR ) . setDescription ( 'Modify changeable configuration variables for this bot.\n\n' + `Type \`${ prefix }config options\` to view a list of valid configuration variables.\n\n` + `To get all set configuration variables:\n\`${ prefix }config get\`\n\n` + `To get a configuration variable:\n\`${ prefix }config get key\`\n\n` + `To set a configuration variable:\n\`${ prefix }config set key value\`\n\n` + `To remove a configuration variable:\n\`${ prefix }config remove key\`` ) ; } } ;