Archived
0
0
Fork 0

feat: added checking of config JSON schema in startup

This commit is contained in:
Daryl Ronningen 2021-06-19 15:54:25 -07:00
parent fc46232b38
commit c418cbbc90
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
6 changed files with 88 additions and 10 deletions

View file

@ -1,4 +1,4 @@
{ {
"token": "", "token": "",
"loglevel": "info" "logLevel": "info"
} }

View file

@ -1,3 +1,3 @@
{ {
"loglevel": "verbose" "logLevel": "verbose"
} }

View file

@ -16,19 +16,44 @@
*/ */
require('module-alias/register'); require('module-alias/register');
import process from 'process';
import { Client } from 'discord.js'; import { Client } from 'discord.js';
import figlet from 'figlet'; import figlet from 'figlet';
import gradient from 'gradient-string'; import gradient from 'gradient-string';
// import config from 'config'; import config from 'config';
import { debug, error, info } from '@utils/logger'; import { Validator } from 'jsonschema';
import { DateTime } from 'luxon';
import chalk from 'chalk';
import { debug, error, info, verbose } from '@utils/logger';
import { ELoggingScope } from '@utils/types'; import { ELoggingScope } from '@utils/types';
import { Defaults } from '@utils/defaults';
debug('Starting bot... Please wait!', ELoggingScope.Startup); info('Starting bot... Please wait!', ELoggingScope.Startup);
debug('Checking config JSON schema', ELoggingScope.Startup);
const mergedConfig = config.util.extendDeep(Defaults.config, config.util.loadFileConfigs(`${process.cwd()}/config`));
const schemaValidator = new Validator();
const validate = schemaValidator.validate(mergedConfig, Defaults.configSchema);
if (validate.valid) {
debug('Config matches JSON schema', ELoggingScope.Startup);
} else {
// Manually send fatal message in case someone messes up the logLevel JSON schema
const message = `Found some errors with the config! Please check errors below.\n${validate.errors}`;
const date = DateTime.now().toLocal().setLocale(Intl.DateTimeFormat().resolvedOptions().locale).toFormat('yyyy-LL-dd HH:mm:ss');
const splitMultiline = message.split('\n');
splitMultiline.forEach((val) => {
console.log(chalk`{grey (${date})} {magenta.bold ${ELoggingScope.Startup}} {red.bold.underline [FATAL]}: {red.underline ${val}}`);
});
}
figlet('Argon Bot', (err, data) => { figlet('Argon Bot', (err, data) => {
if (err) error(`Figlet encountered an error!\n${err.message}`); if (err) error(`Figlet encountered an error!\n${err.message}`);
info(gradient.rainbow.multiline(`\n${data}`)); info(gradient.rainbow.multiline(`\n${data}`), ELoggingScope.Startup);
}); });
const client = new Client({ const client = new Client({
@ -36,7 +61,14 @@ const client = new Client({
}); });
client.on('ready', () => { client.on('ready', () => {
debug(`Total number of Servers: ${client.guilds.cache.size}`, ELoggingScope.Startup);
debug(`Total number of Users: ${client.users.cache.size}`, ELoggingScope.Startup);
info('Bot is ready!', ELoggingScope.Startup); info('Bot is ready!', ELoggingScope.Startup);
}); });
//client.login(token); client.on('raw', (payload) => {
verbose(JSON.stringify(payload, null, 2), ELoggingScope.Payload);
});
client.login(config.get('token'));

46
src/lib/utils/defaults.ts Normal file
View file

@ -0,0 +1,46 @@
/*
* This file is part of ArgonBot
*
* ArgonBot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ArgonBot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>.
*/
export const Defaults = {
config: {
logLevel: 'info',
},
configSchema: {
$id: 'http://example.com/example.json',
$schema: 'http://json-schema.org/draft-07/schema',
required: ['token', 'logLevel'],
type: 'object',
properties: {
token: {
$id: '#/properties/token',
type: 'string',
},
logLevel: {
$id: '#/properties/logLevel',
type: 'string',
enum: [
'verbose',
'debug',
'info',
'warn',
'error',
'fatal',
],
},
},
additionalProperties: false,
},
};

View file

@ -20,8 +20,6 @@ import { DateTime } from 'luxon';
import type { ELoggingScope } from './types'; import type { ELoggingScope } from './types';
chalk.Level = 3;
let verboseLevel = false; let verboseLevel = false;
let debugLevel = false; let debugLevel = false;
let infoLevel = false; let infoLevel = false;
@ -29,7 +27,7 @@ let warnLevel = false;
let errorLevel = false; let errorLevel = false;
let fatalLevel = false; let fatalLevel = false;
switch ((config.get('loglevel') as string).toLowerCase()) { switch ((config.get('logLevel') as string).toLowerCase()) {
case 'verbose': case 'verbose':
verboseLevel = true; verboseLevel = true;
debugLevel = true; debugLevel = true;

View file

@ -18,6 +18,8 @@
export enum ELoggingScope { export enum ELoggingScope {
Startup = 'STARTUP', Startup = 'STARTUP',
Command = 'COMMAND', Command = 'COMMAND',
Payload = 'PAYLOAD',
Query = 'QUERY',
} }
// Interfaces // Interfaces