12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 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(() => {
- // 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()
|