Changes to Config Syntax

- moved the discord token and youtube api key to ` api.botToken`  and `api.youTubeApiKey` in the `config.json` file
- added `lib.utils.ConfigChecker` class to check for missing required attributes in the config
- Updated README to new circumstances
pull/26/head
Trivernis 6 years ago
parent 5845144a83
commit a712f41a55

@ -3,19 +3,21 @@ discordbot [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blu
A bot that does the discord thing.
`node bot.js [--token=<DiscordBotToken>] [--ytapi=<GoogleApiKey>] [--owner=<DiscordTag>] [--prefix=<Char>] [--game=<String>]`
`node bot.js [--token=<DiscordBotToken>] [--ytapi=<GoogleApiKey>] [--owner=<DiscordTag>] [--prefix=<Char>] [--game=<String>] [-i=<Boolen>]`
The arguments are optional because the token and youtube-api-key that the bot needs to run can also be defined in the config.json in the bot's directory:
```json5
// config.json
{
"prefix": "_",
"token": "DISCORD BOT TOKEN",
"ytapikey": "YOUTUBE API KEY",
"presence": "THE DEFAULT GAME IF NO presences.txt IS FOUND IN ./data/",
"presence_duration": 300000,
"presence": "STRING", // this will be shown when no presences are set in data/presences.txt
"presence_duration": 300000, // how long does the bot have one presence
"api": {
"botToken": "YOUR DISCORD BOT TOKEN",
"youTubeApiKey": "YOUR YOUTUBE API KEY"
},
"owners": [
"SPECIFY A LIST OF BOT-OWNERS"
"DISCORD NAME" // specify a list of bot owners that can use the owner commands
],
"music": {
"timeout": 300000
@ -23,6 +25,17 @@ The arguments are optional because the token and youtube-api-key that the bot ne
}
```
If the keys are missing from the config file, the bot exits. This behaviour can be deactivated by setting the `-i` commandline flag.
Keys
---
You can get the API-Keys here:
[Discord Bot Token](https://discordapp.com/developers)
[YouTube API Key](https://console.developers.google.com)
Features
---

@ -7,8 +7,8 @@ const Discord = require("discord.js"),
config = require('./config.json'),
client = new Discord.Client(),
args = require('args-parser')(process.argv),
authToken = args.token || config.token,
prefix = args.prefix || config.prefix,
authToken = args.token || config.api.botToken,
prefix = args.prefix || config.prefix || '~',
gamepresence = args.game || config.presence;
let presences = [], // loaded from presences.txt file if the file exists
@ -20,6 +20,14 @@ function main() {
client.destroy();
});
cmd.setLogger(logger);
let configVerifyer = new utils.ConfigVerifyer(config, [
"api.botToken", "api.youTubeApiKey"
]);
if (configVerifyer.verifyConfig(logger)) {
if (!args.i) {
process.exit(1);
}
}
guilding.setLogger(logger);
cmd.init(prefix);
registerCommands();

@ -5,7 +5,7 @@ const Discord = require("discord.js"),
args = require('args-parser')(process.argv),
config = require('../config.json'),
utils = require('./utils.js'),
ytapiKey = args.ytapi || config.ytapikey;
ytapiKey = args.ytapi || config.api.youTubeApiKey;
/* Variable Definition */
let logger = require('winston');
let djs = {};

@ -26,6 +26,24 @@ exports.getExtension = function (filename) {
}
};
/**
* Walks the path to the objects attribute and returns the value.
* @param object
* @param attributePath
* @returns {undefined/Object}
*/
exports.objectDeepFind = function (object, attributePath) {
let current = object,
paths = attributePath.split('.');
for (let path of paths) {
if (current[path])
current = current[path];
else
return undefined;
}
return current;
};
/**
* lets you define a cleanup for your program exit
* @param {Function} callback the cleanup function
@ -73,6 +91,8 @@ exports.dirExistence = function (path, callback) {
})
};
/* Classes */
exports.YouTube = class {
/**
* returns if an url is a valid youtube url (without checking for an entity id)
@ -153,4 +173,43 @@ exports.YouTube = class {
static getVideoThumbnailUrlFromUrl(url) {
return `https://i3.ytimg.com/vi/${exports.YouTube.getVideoIdFromUrl(url)}/maxresdefault.jpg`
}
};
exports.ConfigVerifyer = class {
/**
* @param confFile {String} the file that needs to be verified
* @param required {Array} the attributes that are required for the bot to work
* @param optional {Array} the attributes that are optional (and may provide extra features)
*/
constructor(confObj, required) {
this.config = confObj;
this.requiredAttributes = required;
}
/**
* @param promtMissing {Boolean} true - everything is fine; false - missing attributes
*/
verifyConfig(logger) {
let missing = [];
for (let reqAttr of this.requiredAttributes) {
if (!exports.objectDeepFind(this.config, reqAttr))
missing.push(reqAttr);
}
this.missingAttributes = missing;
this.logMissing(logger);
return this.missingAttributes.length > 0;
}
/**
* Promts the user which attributes are missing
* @param logger
*/
logMissing(logger) {
if (this.missingAttributes.length > 0) {
logger.error("Missing required Attributes");
for (let misAttr of this.missingAttributes) {
logger.warn(`Missing Attribute ${misAttr} in config.json`);
}
}
}
};
Loading…
Cancel
Save