diff --git a/appTest.js b/appTest.js index 34d2cdf..2b99072 100644 --- a/appTest.js +++ b/appTest.js @@ -31,7 +31,9 @@ function query () { if (!isPm) { pluginManager.testPlugins(message).then(function (results) { for (var i = 0; i < results.length; i++) { - console.log(results[i]) + let result = results[i] + + console.log('[' + result.to + ']', result.message) } query() }, function (error) { diff --git a/logger.js b/logger.js new file mode 100644 index 0000000..0e34f5e --- /dev/null +++ b/logger.js @@ -0,0 +1,52 @@ +const fs = require('fs') +const path = require('path') +const winston = require('winston') + +const logDirectory = path.join(__dirname, '/log/') + +// ensure log directory exists +if (!fs.existsSync(logDirectory)) { + fs.mkdirSync(logDirectory) +} + +let logger = winston.createLogger({ + level: process.env.NODE_ENV !== 'production' ? 'error' : 'debug', + transports: [ + new winston.transports.File({ + level: 'info', + filename: path.join(logDirectory, '/ircbot.log'), + handleExceptions: true, + json: true, + maxsize: 5242880, + maxFiles: 5, + colorize: false, + timestamp: true + }), + new winston.transports.Console({ + level: 'debug', + handleExceptions: true, + json: false, + colorize: true, + timestamp: true + }) + ], + exceptionHandlers: [ + new winston.transports.Console({ + json: false, + timestamp: true + }), + new winston.transports.File({ + filename: logDirectory + '/exceptions.log', + json: false + }) + ], + exitOnError: false +}) + +logger.stream = { + write: function (message, encoding) { + logger.info(message) + } +} + +module.exports = logger diff --git a/plugins/base.js b/plugins/base.js index 035a5f7..618441f 100644 --- a/plugins/base.js +++ b/plugins/base.js @@ -12,6 +12,10 @@ class BasePlugin { result (input) { } + + help () { + return 'No documentation' + } } // export default BasePlugin; diff --git a/plugins/character1.js b/plugins/character1.js index 475f83d..ef16812 100644 --- a/plugins/character1.js +++ b/plugins/character1.js @@ -6,6 +6,11 @@ class Character1 extends BasePlugin { this.name = 'Character1' } + help () { + // | Command | Description + return '.luohahmo Luo D&D-hahmon statsit' + } + test (input) { if (input.match(/^\.luohahmo$/i)) { return true diff --git a/plugins/character2.js b/plugins/character2.js index c4e25f9..c5bb46b 100644 --- a/plugins/character2.js +++ b/plugins/character2.js @@ -109,6 +109,11 @@ class Character2 extends BasePlugin { this.name = 'Character2' } + help () { + // | Command | Description + return '.luohahmo2 Luo D&D-hahmon statsit nimellä ja luokalla' + } + test (input) { if (input.match(/^\.luohahmo2$/i)) { return true diff --git a/plugins/dice.js b/plugins/dice.js index 98f3bfe..3032ffa 100644 --- a/plugins/dice.js +++ b/plugins/dice.js @@ -6,6 +6,11 @@ class Dice extends BasePlugin { this.name = 'Dice' } + help () { + // | Command | Description + return '.heita | .heitä 1d4 Heitä noppaa. Esim. 1d4' + } + test (input) { if (input.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)( ([ \w])*)*$/i)) { return true diff --git a/plugins/help.js b/plugins/help.js new file mode 100644 index 0000000..764faac --- /dev/null +++ b/plugins/help.js @@ -0,0 +1,33 @@ +var BasePlugin = require('./base.js') +const fs = require('fs') +class Help extends BasePlugin { + constructor (config) { + super(config) + this.name = 'Help' + } + test (input) { + if (input.match(/^\.apua/i)) { + return true + } + return false + } + + result (input) { + return new Promise((resolve, reject) => { + let help = [] + this.config.plugins.forEach((pluginName) => { + if (fs.existsSync('./plugins/' + pluginName.toLowerCase() + '.js')) { + let Plugin = require('./' + pluginName.toLowerCase() + '.js') + let plugin = new Plugin(this.config) + if (typeof plugin.help === 'function') { + help.push({message: plugin.help(), to: 'nick'}) + } + } + }) + + resolve(help) + }) + } +} + +module.exports = Help diff --git a/plugins/instantanswer.js b/plugins/instantanswer.js index efba720..b6ba980 100644 --- a/plugins/instantanswer.js +++ b/plugins/instantanswer.js @@ -7,6 +7,12 @@ class InstantAswer extends BasePlugin { super(config) this.name = 'InstantAswer' } + + help () { + // | Command | Description + return '.wtf Etsii vastauksen annetulle termille' + } + test (input) { if (input.match(/^\.wtf (.+)$/i)) { return true diff --git a/plugins/joke.js b/plugins/joke.js index ee46f08..d90aeb1 100644 --- a/plugins/joke.js +++ b/plugins/joke.js @@ -8,6 +8,11 @@ class Joke extends BasePlugin { this.name = 'Joke' } + help () { + // | Command | Description + return '.vitsi Antaa huikean vitsin!' + } + test (input) { if (input.match(/^\.vitsi$/i)) { return true diff --git a/plugins/kernel.js b/plugins/kernel.js index c86e517..1208f29 100644 --- a/plugins/kernel.js +++ b/plugins/kernel.js @@ -17,6 +17,11 @@ class Kernel extends BasePlugin { return promise } + help () { + // | Command | Description + return '.kernel Hakee viimeisimmän kernelin version' + } + test (input) { // Kernel version if (input.match(/^\.kernel( (.+))?/i)) { diff --git a/plugins/pluginManager.js b/plugins/pluginManager.js index bff0334..c452cfa 100644 --- a/plugins/pluginManager.js +++ b/plugins/pluginManager.js @@ -42,6 +42,31 @@ class PluginManager { }) } + validateResult (resultPromise) { + return new Promise((resolve) => { + let wrapperPromise = new Promise((resolve) => resolve(resultPromise)) + wrapperPromise.then((results) => { + if (!Array.isArray(results)) { + results = [results] + } + + let promises = [] + results.forEach((result) => { + let to = 'channel' + let message = result + if (typeof result === 'object' && typeof result.to !== 'undefined') { + to = result.to + message = result.message + } + + promises.push(new Promise({ to, message })) + }) + + resolve(promises) + }) + }) + } + testPlugins (input, publicChat) { if (typeof publicChat === 'undefined') { publicChat = true @@ -54,7 +79,7 @@ class PluginManager { let plugin = this.plugins[i] if (plugin.test(input)) { logger.info('* Plugin ' + plugin.name + ' reported hit') - promises.push(plugin.result(input)) + promises.push(this.validateResult(plugin.result(input))) } } } else { @@ -63,7 +88,7 @@ class PluginManager { if (plugin.test(input)) { logger.info('* Plugin ' + plugin.name + ' reported hit') - promises.push(plugin.result(input)) + promises.push(this.validateResult(plugin.result(input))) } } } diff --git a/plugins/pvm.js b/plugins/pvm.js index 09bc7e4..16c90b8 100644 --- a/plugins/pvm.js +++ b/plugins/pvm.js @@ -10,6 +10,11 @@ class Pwm extends BasePlugin { this.apikey = config.keys.owm.apiKey // owm for sunrise } + help () { + // | Command | Description + return '.pvm Kertoo päivän, nimipäivän ja muita tietoja' + } + test (input) { var res = input.match(/^\.pvm$/i) if (res) { diff --git a/plugins/weather.js b/plugins/weather.js index b724fdf..90d424e 100644 --- a/plugins/weather.js +++ b/plugins/weather.js @@ -31,6 +31,11 @@ class Weather extends BasePlugin { } } + help () { + // | Command | Description + return '.saa Antaa sään maapallon tärkeimmistä sijainneista' + } + test (input) { if (input.match(/^\.saa(.*)/i)) { return true