1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- const path = require('path')
- const fs = require('mz/fs')
- const json = require('comment-json')
- const typecheck = require('./util/typecheck')
- class Config {
- async load() {
- const jsonStr = await fs.readFile('config.json', 'utf8')
- .catch(err => {
- // Make the error more human-friendly
- return Promise.reject('Could not read config.json file')
- })
- try {
- this.obj = json.parse(jsonStr)
- } catch (err) {
- throw `Error parsing config.json (${err.message})`
- }
- // Sanitize
- try {
- typecheck(this.obj.database_uri, {type: String}, 'for database_uri')
- typecheck(this.obj.discord_server_id, {type: String}, 'for discord_server_id')
- typecheck(this.obj.discord_emote_store_server_ids, {type: Array}, 'for discord_emote_store_server_ids')
- typecheck(this.obj.discord_bot_tokens, {type: Array}, 'for discord_bot_tokens')
- typecheck(this.obj.protected_ids, {type: Array}, 'for protected_ids')
- if (this.obj.protected_ids.length === 0) throw new TypeError('Please add at least one ID to protected_ids')
- typecheck(this.obj.skip_cleanup, {type: Boolean}, 'for skip_cleanup')
- } catch (err) {
- throw 'Error in config.json:\n' + err.message // It's human-readable!
- }
- }
- get(key) {
- if (!this.obj) {
- throw new Error('Config not yet loaded')
- }
- return this.obj[key]
- }
- }
- module.exports = new Config()
|