Command-fix/src/data.ts

56 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-05-02 07:35:33 +02:00
import * as fs from 'fs';
import state from './state';
import logger from './logging';
2020-05-02 07:35:33 +02:00
export function readWarnings () {
// Load the warnings file into the application state.
const 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.`);
}
});
}
2020-05-02 07:35:33 +02:00
export function readBans () {
// Load the ban file into the application state.
const 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.`);
}
});
}
2020-05-02 07:35:33 +02:00
export function readCustomResponses () {
// Load the responses file into the responses variable.
try {
state.responses = require(`./responses/${process.env.TENANT}.json`);
logger.debug(`Loaded responses file for ${process.env.TENANT} from external source.`);
} catch (e) {
logger.error(`Failed to load ${process.env.TENANT}.json! Custom responses are disabled.`);
}
}
2020-05-02 07:35:33 +02:00
export function flushWarnings () {
const warningsJson = JSON.stringify(state.warnings, null, 4);
fs.writeFile('/data/discordWarnings.json', warningsJson, 'utf8', function (err) {
if (err) { throw err; }
});
}
2020-05-02 07:35:33 +02:00
export function flushBans () {
const bansJson = JSON.stringify(state.bans, null, 4);
fs.writeFile('/data/discordBans.json', bansJson, 'utf8', function (err) {
if (err) { throw err; }
});
}