Command-fix/src/data.js

54 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-04-04 02:14:57 +02:00
const fs = require('fs');
const state = require('./state.js');
const logger = require('./logging.js');
function readWarnings() {
// Load the warnings file into the application state.
var readFilePath = '/data/discordWarnings.json';
fs.readFile(readFilePath, 'utf8', function (err, data) {
if (err) { throw err; }
if (data) {
state.warnings = JSON.parse(data);
logger.debug('Loaded warnings file.');
} else {
logger.warn(`${readFilePath} appears to be an empty file.`);
}
});
}
function readBans() {
// Load the ban file into the application state.
var readFilePath = '/data/discordBans.json';
fs.readFile(readFilePath, 'utf8', function (err, data) {
if (err) { throw err; }
if (data) {
state.bans = JSON.parse(data);
logger.debug('Loaded bans file.');
} else {
logger.warn(`${readFilePath} appears to be an empty file.`);
}
});
}
function readCustomResponses() {
// Load the responses file into the responses variable.
2018-07-30 00:47:19 +02:00
state.responses = require(`./${process.env.TENANT}.json`);
logger.debug(`Loaded responses file for ${process.env.TENANT} from external source.`);
}
function flushWarnings() {
var warningsJson = JSON.stringify(state.warnings, null, 4);
fs.writeFile('/data/discordWarnings.json', warningsJson, 'utf8', function (err) {
if (err) { throw err; }
});
}
function flushBans() {
var bansJson = JSON.stringify(state.bans, null, 4);
fs.writeFile('/data/discordBans.json', bansJson, 'utf8', function (err) {
if (err) { throw err; }
});
}
module.exports = { readWarnings: readWarnings, readBans: readBans, readCustomResponses: readCustomResponses, flushWarnings: flushWarnings, flushBans: flushBans };