import { Model , ModelOptions , QueryContext } from 'objection' ; import * as DefaultConfig from '../../config/config.json' ; import { ConfigurationManager } from '../managers/configuration.manager' ; interface IConfiguration { created_at : Date ; updated_at : Date ; guild ? : string ; key : string ; value ? : string ; } export default class Configuration extends Model implements IConfiguration { created_at : Date ; updated_at : Date ; guild ? : string ; key : string ; value ? : string ; /**
     * Get the name of the table.
     */ static get tableName ( ) : string { return 'configurations' ; } param opt param queryContext $beforeUpdate ( opt , queryContext ) { this . updated_at = new Date ( ) ; } /**
     * Create configurations for the bot with default values.
     *
     * This method does not take care of the local storage.
     */ static async createDefaultConfigurations ( ) { for ( const key in DefaultConfig ) { const config = ConfigurationManager . getDefaultConfiguration ( key ) ; if ( ! config . guild && ! await Configuration . getConfiguration ( key ) ) { await this . setConfiguration ( key , config . defaultValue ) ; } } } param key param guild static async getConfiguration ( key , guild ? ) { const first = await this . query ( ) . where ( 'key' , key ) . where ( 'guild' , this . resolveGuild ( guild ) ) . first ( ) ; return first !== undefined ? first . value : '' ; } param key param guild static async doesExist ( key , guild ? ) { const query = await this . query ( ) . where ( 'key' , key ) . where ( 'guild' , this . resolveGuild ( guild ) ) . count ( '*' , { as : 'count' } ) . execute ( ) ; const count = query [ 0 ] [ 'count' ] ; return count > 0 ; } param key param value param guild static async setConfiguration ( key , value ? , guild ? ) { if ( await this . doesExist ( key , guild ) ) { await this . query ( ) . where ( 'key' , key ) . where ( 'guild' , this . resolveGuild ( guild ) ) . update ( { value } ) ; } else { await this . query ( ) . insert ( { key , value , guild , } ) ; } } param key param guild static async deleteConfiguration ( key , guild ? ) { return this . query ( ) . where ( 'key' , key ) . where ( 'guild' , this . resolveGuild ( guild ) ) . del ( ) ; } param guild static async deleteGuildConfigurations ( guild ) { return this . query ( ) . where ( 'guild' , guild ) . del ( ) ; } param guild static async getGuildConfigurations ( guild ) { return this . query ( ) . where ( 'guild' , guild ) . select ( ) ; } /**
     * Get all configuration rows.
     */ static async all ( ) { return this . query ( ) . select ( ) ; } param guild protected static resolveGuild ( guild ? ) { return guild === undefined ? null : guild ; } }