Added Unit Tests & Logging
- Added own logger class that includes the module name - added unit tests for MessageHandler, Command and Answer - removed unit tests for GuildHandler and lib/cmd - removed old command templatespull/51/head
parent
6f890dacee
commit
e74fa83ed3
@ -1,109 +0,0 @@
|
||||
{
|
||||
"utils": {
|
||||
"help": {
|
||||
"name": "help",
|
||||
"permission": "all",
|
||||
"description": "Shows this help command.",
|
||||
"category": "Utility",
|
||||
"args": [
|
||||
"command"
|
||||
]
|
||||
},
|
||||
"say": {
|
||||
"name": "say",
|
||||
"permission": "all",
|
||||
"description": "Says something. ~say [String].",
|
||||
"category": "Utility"
|
||||
},
|
||||
"addpresence": {
|
||||
"name": "addpresence",
|
||||
"permission": "owner",
|
||||
"description": "Adds a presence to presences.",
|
||||
"category": "Utility"
|
||||
},
|
||||
"shutdown": {
|
||||
"name": "shutdown",
|
||||
"description": "Shuts the bot down.",
|
||||
"permission": "owner",
|
||||
"category": "Utility"
|
||||
},
|
||||
"rotate": {
|
||||
"name": "rotate",
|
||||
"description": "Forces a presence rotation",
|
||||
"permission": "owner",
|
||||
"category": "Utility"
|
||||
},
|
||||
"createUser": {
|
||||
"name": "createUser",
|
||||
"permission": "owner",
|
||||
"description": "Creates a new user for the webinterface.",
|
||||
"category": "Utility",
|
||||
"args": [
|
||||
"username",
|
||||
"password",
|
||||
"scope"
|
||||
]
|
||||
},
|
||||
"bugreport": {
|
||||
"name": "bug",
|
||||
"permission": "all",
|
||||
"description": "Get info about how to report a bug",
|
||||
"category": "Utility",
|
||||
"response": {
|
||||
"bug_report": "Please report your bugs [here](https://github.com/Trivernis/discordbot.js/issues)"
|
||||
}
|
||||
}
|
||||
},
|
||||
"info": {
|
||||
"about": {
|
||||
"name": "about",
|
||||
"permission": "all",
|
||||
"description": "Shows information about this bot.",
|
||||
"category": "Info",
|
||||
"response": {
|
||||
"about_icon": "This icon war created by [blackrose14344](https://www.deviantart.com/blackrose14344). \n [Original](https://www.deviantart.com/blackrose14344/art/2B-Chi-B-685771489)",
|
||||
"about_creator": "This bot was created by Trivernis. More about this bot [here](https://github.com/Trivernis/discordbot.js)."
|
||||
}
|
||||
},
|
||||
"ping": {
|
||||
"name": "ping",
|
||||
"permission": "owner",
|
||||
"description": "Answers with the current average ping of the bot.",
|
||||
"category": "Info"
|
||||
},
|
||||
"uptime": {
|
||||
"name": "uptime",
|
||||
"permission": "owner",
|
||||
"description": "Answers with the current uptime of the bot.",
|
||||
"category": "Info"
|
||||
},
|
||||
"guilds": {
|
||||
"name": "guilds",
|
||||
"permission": "owner",
|
||||
"description": "Answers with the number of guilds the bot has joined.",
|
||||
"category": "Info"
|
||||
}
|
||||
},
|
||||
"api": {
|
||||
"AniList": {
|
||||
"animeSearch": {
|
||||
"name": "anime",
|
||||
"permission": "all",
|
||||
"description": "Answers the anime found for that name on AniList.",
|
||||
"category": "AniList",
|
||||
"response": {
|
||||
"not_found": "The Anime was not found :("
|
||||
}
|
||||
},
|
||||
"mangaSearch": {
|
||||
"name": "manga",
|
||||
"permission": "all",
|
||||
"description": "Answers the manga found for that name on AniList.",
|
||||
"category": "AniList",
|
||||
"response": {
|
||||
"not_found": "The Manga was not found :("
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,203 +0,0 @@
|
||||
{
|
||||
"utils": {
|
||||
"roles": {
|
||||
"name": "roles",
|
||||
"permission": "all",
|
||||
"description": "Shows the roles used for commands on the server.",
|
||||
"category": "Utility"
|
||||
},
|
||||
"savecmd": {
|
||||
"name": "savecmd",
|
||||
"permission": "moderator",
|
||||
"description": "Saves a sequence of commands under a new name. ~save [cmdsequence] [cmdname]. Semicoli must be escaped with \\ (Backslash)",
|
||||
"category": "Utility",
|
||||
"response": {
|
||||
"no_recursion": "You are **not** allowed to execute another saved command in this sequence. This is a safety measure to avoid endlessly recursive calls.",
|
||||
"sequence_too_long": "This command sequence is too long!"
|
||||
}
|
||||
},
|
||||
"savedcmd": {
|
||||
"name": "savedcmd",
|
||||
"permission": "all",
|
||||
"description": "Displays the saved commands.",
|
||||
"category": "Utility",
|
||||
"response": {
|
||||
"no_commands": "There are no saved commands."
|
||||
}
|
||||
},
|
||||
"deletecmd": {
|
||||
"name": "deletecmd",
|
||||
"permission": "moderator",
|
||||
"description": "Delete a saved command.",
|
||||
"args": [
|
||||
"cmdname"
|
||||
],
|
||||
"category": "Utility"
|
||||
},
|
||||
"execute": {
|
||||
"name": "execute",
|
||||
"permission": "all",
|
||||
"args": [
|
||||
"cmdname"
|
||||
],
|
||||
"description": "Execute saved commands.",
|
||||
"category": "Utility",
|
||||
"response": {
|
||||
"not_found": "This command could not be found."
|
||||
}
|
||||
}
|
||||
},
|
||||
"music": {
|
||||
"play": {
|
||||
"name": "play",
|
||||
"permission": "all",
|
||||
"args": [
|
||||
"url"
|
||||
],
|
||||
"description": "Adds the url to the YouTube video/playlist into the queue.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"success": "Added Song/Playlist to the queue.",
|
||||
"failure": "Failed adding Song/Playlist to the queue.",
|
||||
"url_invalid": "This is not a valid url!",
|
||||
"no_url": "I need an url to a video to play!",
|
||||
"no_voicechannel": "You need to join a voicechannel to do that!"
|
||||
}
|
||||
},
|
||||
"playnext": {
|
||||
"name": "playnext",
|
||||
"permission": "all",
|
||||
"args": [
|
||||
"url"
|
||||
],
|
||||
"description": "Adds the url to the YouTube video as next song to the queue.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"success": "Added Song as next Song to the queue.",
|
||||
"failure": "Failed adding Song as next Song to the queue.",
|
||||
"url_invalid": "This is not a valid url!",
|
||||
"no_url": "I need an url to a video to play",
|
||||
"no_voicechannel": "You need to join a voicechannel to do that!"
|
||||
}
|
||||
},
|
||||
"join": {
|
||||
"name": "join",
|
||||
"permission": "all",
|
||||
"description": "Joins the VC you are in.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"not_connected": "You are not connected to a Voice Channel."
|
||||
}
|
||||
},
|
||||
"stop": {
|
||||
"name": "stop",
|
||||
"permission": "dj",
|
||||
"description": "Stops playing music and leaves.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"success": "Stopping now...",
|
||||
"not_playing": "I'm not playing music at the moment."
|
||||
}
|
||||
},
|
||||
"pause": {
|
||||
"name": "pause",
|
||||
"permission": "all",
|
||||
"description": "Pauses playing.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"success": "Pausing playback.",
|
||||
"not_playing": "I'm not playing music at the moment."
|
||||
}
|
||||
},
|
||||
"resume": {
|
||||
"name": "resume",
|
||||
"permission": "all",
|
||||
"description": "Resumes playing.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"success": "Resuming playback.",
|
||||
"not_playing": "I'm not playing music at the moment."
|
||||
}
|
||||
},
|
||||
"skip": {
|
||||
"name": "skip",
|
||||
"permission": "dj",
|
||||
"description": "Skips the current song.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"success": "Skipping to the next song.",
|
||||
"not_playing": "I'm not playing music at the moment."
|
||||
}
|
||||
},
|
||||
"clear": {
|
||||
"name": "clear",
|
||||
"permission": "dj",
|
||||
"description": "Clears the queue.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"success": "The Queue has been cleared."
|
||||
}
|
||||
},
|
||||
"playlist": {
|
||||
"name": "queue",
|
||||
"permission": "all",
|
||||
"description": "Shows the next ten songs.",
|
||||
"category": "Music"
|
||||
},
|
||||
"current": {
|
||||
"name": "np",
|
||||
"permission": "all",
|
||||
"description": "Shows the currently playing song.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"not_playing": "I'm not playing music at the moment."
|
||||
}
|
||||
},
|
||||
"shuffle": {
|
||||
"name": "shuffle",
|
||||
"permission": "all",
|
||||
"description": "Shuffles the playlist.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"success": "The Queue has been shuffled."
|
||||
}
|
||||
},
|
||||
"repeat": {
|
||||
"name": "repeat",
|
||||
"permission": "all",
|
||||
"description": "Toggle listening on repeat.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"repeat_true": "Listening on repeat now!",
|
||||
"repeat_false": "Not listening on repeat anymore."
|
||||
}
|
||||
},
|
||||
"savemedia": {
|
||||
"name": "savemedia",
|
||||
"permission": "dj",
|
||||
"args": [
|
||||
"url"
|
||||
],
|
||||
"description": "Saves the YouTube song/playlist with a specific name",
|
||||
"category": "Music"
|
||||
},
|
||||
"savedmedia": {
|
||||
"name": "savedmedia",
|
||||
"permission": "all",
|
||||
"description": "Prints out all saved playlists and songs.",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"no_saved": "There are no saved songs/playlists :("
|
||||
}
|
||||
},
|
||||
"deletemedia": {
|
||||
"name": "deletemedia",
|
||||
"permission": "dj",
|
||||
"description": "Deletes a saved media entry. ~deletemedia [name]",
|
||||
"category": "Music",
|
||||
"response": {
|
||||
"no_name": "You must provide a name for the media that shall be deleted."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,66 +1,122 @@
|
||||
/* eslint-disable no-unused-vars */
|
||||
const winston = require('winston'),
|
||||
DailyRotateFile = require('winston-daily-rotate-file'),
|
||||
args = require('args-parser')(process.argv),
|
||||
args = require('args-parser')(process.argv);
|
||||
|
||||
fileLoggingFormat = winston.format.printf(info => {
|
||||
return `${info.timestamp} ${info.level.toUpperCase()}: ${JSON.stringify(info.message)}`; // the logging format for files
|
||||
}),
|
||||
consoleLoggingFormat = winston.format.printf(info => {
|
||||
return `${info.timestamp} {${info.label}} [${info.level}] ${JSON.stringify(info.message)}`; //the logging format for the console
|
||||
/**
|
||||
* Set console format to simple string format
|
||||
* @type {Format}
|
||||
*/
|
||||
const consoleLoggingFormat = winston.format.printf(info => {
|
||||
return `${info.timestamp} {${info.module || info.m || 'DEFAULT'}} [${info.level}] ${JSON.stringify(info.message)}`; //the logging format for the console
|
||||
});
|
||||
|
||||
/**
|
||||
* Set full format to combination of formats
|
||||
* @type {Format}
|
||||
*/
|
||||
const loggingFullFormat = winston.format.combine(
|
||||
winston.format.timestamp({
|
||||
format: 'YY-MM-DD HH:mm:ss.SSS'
|
||||
}),
|
||||
loggingFullFormat = winston.format.combine(
|
||||
winston.format.splat(),
|
||||
winston.format.timestamp({
|
||||
format: 'YY-MM-DD HH:mm:ss.SSS'
|
||||
}),
|
||||
winston.format.label({label: ''}),
|
||||
winston.format.json()
|
||||
);
|
||||
let logger = winston.createLogger({
|
||||
level: winston.config.npm.levels, // logs with npm levels
|
||||
format: loggingFullFormat,
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.colorize(),
|
||||
winston.format.splat(),
|
||||
winston.format.timestamp({
|
||||
format: 'YY-MM-DD HH:mm:ss.SSS'
|
||||
}),
|
||||
winston.format.label({label: ''}),
|
||||
consoleLoggingFormat
|
||||
),
|
||||
level: args.loglevel || 'info'
|
||||
}),
|
||||
new winston.transports.File({
|
||||
level: 'debug',
|
||||
filename: './.log/latest.log',
|
||||
options: {flags: 'w'} // overwrites the file on restart
|
||||
winston.format.json()
|
||||
);
|
||||
/**
|
||||
* Define all transports used.
|
||||
* @type {any[]}
|
||||
*/
|
||||
let transports = [
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.colorize(),
|
||||
winston.format.splat(),
|
||||
winston.format.timestamp({
|
||||
format: 'YY-MM-DD HH:mm:ss.SSS'
|
||||
}),
|
||||
new DailyRotateFile({
|
||||
level: 'verbose',
|
||||
filename: './.log/%DATE%.log',
|
||||
datePattern: 'YYYY-MM-DD',
|
||||
zippedArchive: true,
|
||||
maxSize: '32m',
|
||||
maxFiles: '30d',
|
||||
json: true
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
//class SpecialLogger extends winston.
|
||||
winston.format.label({label: ''}),
|
||||
consoleLoggingFormat
|
||||
),
|
||||
level: args.loglevel || 'info'
|
||||
}),
|
||||
new winston.transports.File({
|
||||
level: 'debug',
|
||||
filename: './.log/latest.log',
|
||||
options: {flags: 'w'} // overwrites the file on restart
|
||||
}),
|
||||
new DailyRotateFile({
|
||||
level: 'verbose',
|
||||
filename: './.log/%DATE%.log',
|
||||
datePattern: 'YYYY-MM-DD',
|
||||
zippedArchive: true,
|
||||
maxSize: '32m',
|
||||
maxFiles: '30d',
|
||||
json: true
|
||||
})
|
||||
];
|
||||
|
||||
/**
|
||||
* A function to return the logger that has been created after appending an exception handler
|
||||
* @returns {Object}
|
||||
* Define the logger
|
||||
* @type {winston.Logger}
|
||||
*/
|
||||
exports.getLogger = function () {
|
||||
logger.exceptions.handle(
|
||||
new winston.transports.File({
|
||||
filename: './.log/exceptions.log'
|
||||
})
|
||||
);
|
||||
return logger;
|
||||
};
|
||||
let logger = winston.createLogger({
|
||||
level: winston.config.npm.levels,
|
||||
format: loggingFullFormat,
|
||||
transports: transports
|
||||
});
|
||||
|
||||
|
||||
// Define exception handling
|
||||
logger.exceptions.handle(
|
||||
new winston.transports.File({
|
||||
filename: './.log/exceptions.log'
|
||||
})
|
||||
);
|
||||
|
||||
class ModuleLogger {
|
||||
|
||||
constructor(moduleInstance) {
|
||||
this.logger = logger;
|
||||
if (moduleInstance.constructor) {
|
||||
switch (moduleInstance.constructor.name) {
|
||||
case 'String':
|
||||
this.logName = moduleInstance;
|
||||
break;
|
||||
case 'Number':
|
||||
this.logName = moduleInstance.toString();
|
||||
break;
|
||||
default:
|
||||
this.logName = moduleInstance.constructor.name;
|
||||
}
|
||||
} else {
|
||||
this.logName = moduleInstance.toString();
|
||||
}
|
||||
}
|
||||
|
||||
silly(msg, meta) {
|
||||
logger.silly(msg, {m: this.logName, ...meta});
|
||||
}
|
||||
|
||||
debug(msg, meta) {
|
||||
logger.debug(msg, {m: this.logName, ...meta});
|
||||
}
|
||||
|
||||
verbose(msg, meta) {
|
||||
logger.verbose(msg, {m: this.logName, ...meta});
|
||||
}
|
||||
|
||||
info(msg, meta) {
|
||||
logger.info(msg, {m: this.logName, ...meta});
|
||||
}
|
||||
warn(msg, meta) {
|
||||
logger.warn(msg, {m: this.logName, ...meta});
|
||||
}
|
||||
|
||||
error(msg, meta) {
|
||||
logger.error(msg, {m: this.logName, ...meta});
|
||||
}
|
||||
}
|
||||
|
||||
Object.assign(exports, {
|
||||
logger: logger,
|
||||
Logger: ModuleLogger
|
||||
});
|
||||
|
Loading…
Reference in New Issue