ramirez/src/config.js

137 lines
3.2 KiB
JavaScript
Raw Normal View History

const json5 = require('json5');
const fs = require('fs');
const path = require('path');
2017-09-19 13:23:55 -04:00
let userConfig;
// Try to find our config file from several options
const configFiles = [
'config.json',
'config.json5',
'config.json.json',
2018-08-07 18:32:22 -04:00
'config.json.txt',
'config.js'
];
let foundConfigFile;
for (const configFile of configFiles) {
try {
fs.accessSync(__dirname + '/../' + configFile);
foundConfigFile = configFile;
break;
} catch (e) {}
}
if (! foundConfigFile) {
throw new Error(`Could not find config.json!`);
}
// Parse the config using JSON5
2017-09-19 13:23:55 -04:00
try {
2018-08-07 18:32:22 -04:00
if (foundConfigFile.endsWith('.js')) {
userConfig = require(`../${foundConfigFile}`);
} else {
const raw = fs.readFileSync(__dirname + '/../' + foundConfigFile);
userConfig = json5.parse(raw);
}
2017-09-19 13:23:55 -04:00
} catch (e) {
throw new Error(`Error reading config file! The error given was: ${e.message}`);
2017-09-19 13:23:55 -04:00
}
const defaultConfig = {
"token": null,
"mailGuildId": null,
"mainGuildId": null,
"logChannelId": null,
"prefix": "!",
"snippetPrefix": "!!",
"status": "Message me for help!",
"responseMessage": "Thank you for your message! Our mod team will reply to you here as soon as possible.",
"closeMessage": null,
2017-09-19 13:23:55 -04:00
2017-09-22 15:18:15 -04:00
"newThreadCategoryId": null,
2018-03-11 16:08:59 -04:00
"mentionRole": "here",
"pingOnBotMention": true,
2017-09-22 15:18:15 -04:00
2017-09-19 13:23:55 -04:00
"inboxServerPermission": null,
"alwaysReply": false,
"alwaysReplyAnon": false,
"useNicknames": false,
"ignoreAccidentalThreads": false,
"threadTimestamps": false,
2018-02-18 17:23:29 -05:00
"allowMove": false,
2018-01-22 17:17:24 -05:00
"typingProxy": false,
"typingProxyReverse": false,
2017-09-19 13:23:55 -04:00
"enableGreeting": false,
2017-09-19 14:32:48 -04:00
"greetingMessage": null,
"greetingAttachment": null,
2017-09-19 13:23:55 -04:00
"requiredAccountAge": null, // In hours
2018-07-27 12:48:45 -04:00
"accountAgeDeniedMessage": "Your Discord account is not old enough to contact modmail.",
"relaySmallAttachmentsAsAttachments": false,
"smallAttachmentLimit": 1024 * 1024 * 2,
2017-09-19 13:23:55 -04:00
"port": 8890,
"url": null,
"dbDir": path.join(__dirname, '..', 'db'),
"knex": null,
"logDir": path.join(__dirname, '..', 'logs'),
2017-09-19 13:23:55 -04:00
};
2017-12-31 19:16:05 -05:00
const required = ['token', 'mailGuildId', 'mainGuildId', 'logChannelId'];
2017-09-19 13:23:55 -04:00
const finalConfig = Object.assign({}, defaultConfig);
for (const [prop, value] of Object.entries(userConfig)) {
if (! defaultConfig.hasOwnProperty(prop)) {
throw new Error(`Invalid option: ${prop}`);
}
finalConfig[prop] = value;
}
// Default knex config
if (! finalConfig['knex']) {
finalConfig['knex'] = {
client: 'sqlite',
connection: {
filename: path.join(finalConfig.dbDir, 'data.sqlite')
},
useNullAsDefault: true
};
}
// Make sure migration settings are always present in knex config
Object.assign(finalConfig['knex'], {
migrations: {
directory: path.join(finalConfig.dbDir, 'migrations')
}
});
// Make sure all of the required config options are present
for (const opt of required) {
if (! finalConfig[opt]) {
console.error(`Missing required config.json value: ${opt}`);
process.exit(1);
}
}
2017-09-19 13:23:55 -04:00
if (finalConfig.smallAttachmentLimit > 1024 * 1024 * 8) {
finalConfig.smallAttachmentLimit = 1024 * 1024 * 8;
console.log('[WARN] smallAttachmentLimit capped at 8MB');
}
// Make sure mainGuildId is internally always an array
if (! Array.isArray(finalConfig['mainGuildId'])) {
finalConfig['mainGuildId'] = [finalConfig['mainGuildId']];
}
2017-09-19 13:23:55 -04:00
module.exports = finalConfig;