2019-12-02 12:01:36 -05:00
|
|
|
/**
|
|
|
|
* !!! NOTE !!!
|
|
|
|
*
|
|
|
|
* If you're setting up the bot, DO NOT EDIT THIS FILE DIRECTLY!
|
|
|
|
*
|
|
|
|
* Create a configuration file in the same directory as the example file.
|
|
|
|
* You never need to edit anything under src/ to use the bot.
|
|
|
|
*
|
|
|
|
* !!! NOTE !!!
|
|
|
|
*/
|
|
|
|
|
2018-05-03 12:55:21 -04:00
|
|
|
const fs = require('fs');
|
2017-12-24 15:04:08 -05:00
|
|
|
const path = require('path');
|
|
|
|
|
2017-09-19 13:23:55 -04:00
|
|
|
let userConfig;
|
|
|
|
|
2019-12-02 12:01:36 -05:00
|
|
|
// Config files to search for, in priority order
|
2018-05-03 12:55:21 -04:00
|
|
|
const configFiles = [
|
2019-12-02 18:51:11 -05:00
|
|
|
'config.ini',
|
|
|
|
'config.ini.ini',
|
|
|
|
'config.ini.txt',
|
2018-05-03 12:55:21 -04:00
|
|
|
'config.json',
|
|
|
|
'config.json5',
|
|
|
|
'config.json.json',
|
2018-08-07 18:32:22 -04:00
|
|
|
'config.json.txt',
|
|
|
|
'config.js'
|
2018-05-03 12:55:21 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
let foundConfigFile;
|
|
|
|
for (const configFile of configFiles) {
|
|
|
|
try {
|
|
|
|
fs.accessSync(__dirname + '/../' + configFile);
|
|
|
|
foundConfigFile = configFile;
|
|
|
|
break;
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
2019-12-02 12:01:36 -05:00
|
|
|
// Load config file
|
2019-12-02 19:27:55 -05:00
|
|
|
if (foundConfigFile) {
|
|
|
|
console.log(`Loading configuration from ${foundConfigFile}...`);
|
|
|
|
try {
|
|
|
|
if (foundConfigFile.endsWith('.js')) {
|
|
|
|
userConfig = require(`../${foundConfigFile}`);
|
2019-12-02 12:01:36 -05:00
|
|
|
} else {
|
2019-12-02 19:27:55 -05:00
|
|
|
const raw = fs.readFileSync(__dirname + '/../' + foundConfigFile, {encoding: "utf8"});
|
|
|
|
if (foundConfigFile.endsWith('.ini') || foundConfigFile.endsWith('.ini.txt')) {
|
|
|
|
userConfig = require('ini').decode(raw);
|
|
|
|
} else {
|
|
|
|
userConfig = require('json5').parse(raw);
|
|
|
|
}
|
2019-12-02 12:01:36 -05:00
|
|
|
}
|
2019-12-02 19:27:55 -05:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Error reading config file! The error given was: ${e.message}`);
|
2018-08-07 18:32:22 -04:00
|
|
|
}
|
2017-09-19 13:23:55 -04:00
|
|
|
}
|
|
|
|
|
2019-12-02 19:27:55 -05:00
|
|
|
const required = ['token', 'mailGuildId', 'mainGuildId', 'logChannelId'];
|
|
|
|
const numericOptions = ['requiredAccountAge', 'requiredTimeOnServer', 'smallAttachmentLimit', 'port'];
|
|
|
|
|
2017-09-19 13:23:55 -04:00
|
|
|
const defaultConfig = {
|
|
|
|
"token": null,
|
|
|
|
"mailGuildId": null,
|
|
|
|
"mainGuildId": null,
|
|
|
|
"logChannelId": null,
|
|
|
|
|
|
|
|
"prefix": "!",
|
|
|
|
"snippetPrefix": "!!",
|
2018-09-20 15:54:47 -04:00
|
|
|
"snippetPrefixAnon": "!!!",
|
2017-09-19 13:23:55 -04:00
|
|
|
|
|
|
|
"status": "Message me for help!",
|
|
|
|
"responseMessage": "Thank you for your message! Our mod team will reply to you here as soon as possible.",
|
2018-07-05 12:34:12 -04:00
|
|
|
"closeMessage": null,
|
2018-09-20 16:31:14 -04:00
|
|
|
"allowUserClose": false,
|
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",
|
2018-05-03 13:26:12 -04:00
|
|
|
"pingOnBotMention": true,
|
2018-09-20 15:07:38 -04:00
|
|
|
"botMentionResponse": null,
|
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,
|
2018-02-18 16:29:24 -05:00
|
|
|
"threadTimestamps": false,
|
2018-02-18 17:23:29 -05:00
|
|
|
"allowMove": false,
|
2019-08-13 13:37:00 -04:00
|
|
|
"syncPermissionsOnMove": true,
|
2018-01-22 17:17:24 -05:00
|
|
|
"typingProxy": false,
|
|
|
|
"typingProxyReverse": false,
|
2018-09-20 15:03:28 -04:00
|
|
|
"mentionUserInThreadHeader": false,
|
2019-06-09 09:04:17 -04:00
|
|
|
"rolesInThreadHeader": 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
|
|
|
|
2019-06-09 08:56:04 -04:00
|
|
|
"guildGreetings": {},
|
|
|
|
|
2018-07-27 13:35:20 -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.",
|
2018-07-10 05:59:29 -04:00
|
|
|
|
2019-04-15 12:06:59 -04:00
|
|
|
"requiredTimeOnServer": null, // In minutes
|
|
|
|
"timeOnServerDeniedMessage": "You haven't been a member of the server for long enough to contact modmail.",
|
2019-04-15 09:43:22 -04:00
|
|
|
|
2018-02-18 13:06:24 -05:00
|
|
|
"relaySmallAttachmentsAsAttachments": false,
|
2018-08-07 17:59:02 -04:00
|
|
|
"smallAttachmentLimit": 1024 * 1024 * 2,
|
2019-03-06 16:31:24 -05:00
|
|
|
"attachmentStorage": "local",
|
|
|
|
"attachmentStorageChannelId": null,
|
2018-02-18 13:06:24 -05:00
|
|
|
|
2019-03-27 22:54:12 -04:00
|
|
|
"categoryAutomation": {},
|
|
|
|
|
2019-06-09 10:31:17 -04:00
|
|
|
"updateNotifications": true,
|
2019-06-09 10:53:49 -04:00
|
|
|
"plugins": [],
|
2019-06-09 10:31:17 -04:00
|
|
|
|
2019-06-09 12:31:16 -04:00
|
|
|
"commandAliases": {},
|
|
|
|
|
2017-09-19 13:23:55 -04:00
|
|
|
"port": 8890,
|
2017-12-24 15:04:08 -05:00
|
|
|
"url": null,
|
|
|
|
|
|
|
|
"dbDir": path.join(__dirname, '..', 'db'),
|
|
|
|
"knex": null,
|
|
|
|
|
|
|
|
"logDir": path.join(__dirname, '..', 'logs'),
|
2017-09-19 13:23:55 -04:00
|
|
|
};
|
|
|
|
|
2019-12-02 19:27:55 -05:00
|
|
|
// Load config values from environment variables
|
|
|
|
const envKeyPrefix = 'MM_';
|
|
|
|
let loadedEnvValues = 0;
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(process.env)) {
|
|
|
|
if (! key.startsWith(envKeyPrefix)) continue;
|
|
|
|
|
|
|
|
// MM_CLOSE_MESSAGE -> closeMessage
|
|
|
|
// MM_COMMAND_ALIASES__MV => commandAliases.mv
|
|
|
|
const configKey = key.slice(envKeyPrefix.length)
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/([a-z])_([a-z])/g, (m, m1, m2) => `${m1}${m2.toUpperCase()}`)
|
|
|
|
.replace('__', '.');
|
|
|
|
|
2019-12-02 19:40:50 -05:00
|
|
|
userConfig[configKey] = value.includes('||')
|
|
|
|
? value.split('||')
|
|
|
|
: value;
|
2019-12-02 19:27:55 -05:00
|
|
|
|
2019-12-02 19:40:50 -05:00
|
|
|
loadedEnvValues++;
|
2019-12-02 19:27:55 -05:00
|
|
|
}
|
|
|
|
|
2019-12-02 19:40:50 -05:00
|
|
|
if (process.env.PORT && !process.env.MM_PORT) {
|
2019-12-02 19:27:55 -05:00
|
|
|
// Special case: allow common "PORT" environment variable without prefix
|
|
|
|
userConfig.port = process.env.PORT;
|
2019-12-02 19:40:50 -05:00
|
|
|
loadedEnvValues++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loadedEnvValues > 0) {
|
|
|
|
console.log(`Loaded ${loadedEnvValues} ${loadedEnvValues === 1 ? 'value' : 'values'} from environment variables`);
|
2019-12-02 19:27:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert config keys with periods to objects
|
|
|
|
// E.g. commandAliases.mv -> commandAliases: { mv: ... }
|
|
|
|
for (const [key, value] of Object.entries(userConfig)) {
|
|
|
|
if (! key.includes('.')) continue;
|
|
|
|
|
|
|
|
const keys = key.split('.');
|
|
|
|
let cursor = userConfig;
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
if (i === keys.length - 1) {
|
|
|
|
cursor[keys[i]] = value;
|
|
|
|
} else {
|
|
|
|
cursor[keys[i]] = cursor[keys[i]] || {};
|
|
|
|
cursor = cursor[keys[i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete userConfig[key];
|
|
|
|
}
|
2017-12-31 19:16:05 -05:00
|
|
|
|
2019-12-02 19:27:55 -05:00
|
|
|
// Combine user config with default config to form final config
|
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)) {
|
2019-12-02 12:01:36 -05:00
|
|
|
throw new Error(`Unknown option: ${prop}`);
|
2017-09-19 13:23:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
finalConfig[prop] = value;
|
|
|
|
}
|
|
|
|
|
2018-04-21 08:38:21 -04:00
|
|
|
// Default knex config
|
2017-12-24 15:04:08 -05:00
|
|
|
if (! finalConfig['knex']) {
|
|
|
|
finalConfig['knex'] = {
|
|
|
|
client: 'sqlite',
|
|
|
|
connection: {
|
|
|
|
filename: path.join(finalConfig.dbDir, 'data.sqlite')
|
|
|
|
},
|
|
|
|
useNullAsDefault: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-21 08:38:21 -04:00
|
|
|
// Make sure migration settings are always present in knex config
|
2017-12-24 15:04:08 -05:00
|
|
|
Object.assign(finalConfig['knex'], {
|
|
|
|
migrations: {
|
|
|
|
directory: path.join(finalConfig.dbDir, 'migrations')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-07 17:59:02 -04:00
|
|
|
if (finalConfig.smallAttachmentLimit > 1024 * 1024 * 8) {
|
|
|
|
finalConfig.smallAttachmentLimit = 1024 * 1024 * 8;
|
2019-03-06 16:31:24 -05:00
|
|
|
console.warn('[WARN] smallAttachmentLimit capped at 8MB');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specific checks
|
|
|
|
if (finalConfig.attachmentStorage === 'discord' && ! finalConfig.attachmentStorageChannelId) {
|
|
|
|
console.error('Config option \'attachmentStorageChannelId\' is required with attachment storage \'discord\'');
|
|
|
|
process.exit(1);
|
2018-08-07 17:59:02 -04:00
|
|
|
}
|
|
|
|
|
2018-04-21 08:38:21 -04:00
|
|
|
// Make sure mainGuildId is internally always an array
|
|
|
|
if (! Array.isArray(finalConfig['mainGuildId'])) {
|
|
|
|
finalConfig['mainGuildId'] = [finalConfig['mainGuildId']];
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:27:59 -04:00
|
|
|
// Make sure inboxServerPermission is always an array
|
|
|
|
if (! Array.isArray(finalConfig['inboxServerPermission'])) {
|
|
|
|
if (finalConfig['inboxServerPermission'] == null) {
|
|
|
|
finalConfig['inboxServerPermission'] = [];
|
|
|
|
} else {
|
|
|
|
finalConfig['inboxServerPermission'] = [finalConfig['inboxServerPermission']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 08:56:04 -04:00
|
|
|
// Move greetingMessage/greetingAttachment to the guildGreetings object internally
|
|
|
|
// Or, in other words, if greetingMessage and/or greetingAttachment is set, it is applied for all servers that don't
|
|
|
|
// already have something set up in guildGreetings. This retains backwards compatibility while allowing you to override
|
|
|
|
// greetings for specific servers in guildGreetings.
|
|
|
|
if (finalConfig.greetingMessage || finalConfig.greetingAttachment) {
|
|
|
|
for (const guildId of finalConfig.mainGuildId) {
|
|
|
|
if (finalConfig.guildGreetings[guildId]) continue;
|
|
|
|
finalConfig.guildGreetings[guildId] = {
|
|
|
|
message: finalConfig.greetingMessage,
|
2019-12-02 18:51:11 -05:00
|
|
|
attachment: finalConfig.greetingAttachment
|
2019-06-09 08:56:04 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 18:51:11 -05:00
|
|
|
// Convert arrays of lines to multiline strings in greetings
|
|
|
|
for (const obj of Object.values(finalConfig.guildGreetings)) {
|
|
|
|
if (Array.isArray(obj.message)) {
|
|
|
|
obj.message = obj.message.join('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 22:54:12 -04:00
|
|
|
// newThreadCategoryId is syntactic sugar for categoryAutomation.newThread
|
|
|
|
if (finalConfig.newThreadCategoryId) {
|
|
|
|
finalConfig.categoryAutomation.newThread = finalConfig.newThreadCategoryId;
|
|
|
|
delete finalConfig.newThreadCategoryId;
|
|
|
|
}
|
|
|
|
|
2019-12-02 19:00:00 -05:00
|
|
|
// Turn empty string options to null (i.e. "option=" without a value in config.ini)
|
|
|
|
for (const [key, value] of Object.entries(finalConfig)) {
|
|
|
|
if (value === '') {
|
|
|
|
finalConfig[key] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast numeric options to numbers
|
|
|
|
for (const numericOpt of numericOptions) {
|
|
|
|
if (finalConfig[numericOpt] != null) {
|
|
|
|
const number = parseFloat(finalConfig[numericOpt]);
|
|
|
|
if (Number.isNaN(number)) {
|
|
|
|
console.error(`Invalid numeric value for ${numericOpt}: ${finalConfig[numericOpt]}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
finalConfig[numericOpt] = number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Configuration ok!");
|
2019-12-02 12:01:36 -05:00
|
|
|
|
2017-09-19 13:23:55 -04:00
|
|
|
module.exports = finalConfig;
|