var logger = require('winston'); var fs = require('fs'); var Promise = require('promise'); class PluginManager { constructor(config) { this.enabledPlugins = []; this.enabledPmPlugins = []; this.plugins = []; this.pmPlugins = []; this.config = config; var that = this; config.plugins.forEach(function(pluginName) { logger.info('checking ' + pluginName + '...'); if(fs.existsSync('./plugins/'+pluginName.toLowerCase()+'.js')) { logger.info('* Exists. Enabling...'); that.enabledPlugins.push(pluginName.toLowerCase()); } if(fs.existsSync('./plugins/pm'+pluginName.toLowerCase()+'.js')) { logger.info('* Exists as PM plugin. Enabling...'); that.enabledPmPlugins.push(pluginName.toLowerCase()); } }); } loadAllPlugins() { var that = this; this.enabledPlugins.forEach(function(pluginName) { var Plugin = require('./' + pluginName + '.js'); var plugin = new Plugin(that.config); that.plugins.push(plugin); }); this.enabledPmPlugins.forEach(function(pluginName) { var Plugin = require('./pm' + pluginName + '.js'); var plugin = new Plugin(that.config); that.pmPlugins.push(plugin); }); } testPlugins(input, publicChat) { if (typeof publicChat === 'undefined') { publicChat = true } var promises = []; if (publicChat) { for(let i = 0; i < this.plugins.length; i++) { var plugin = this.plugins[i]; if(plugin.test(input)) { logger.info('* Plugin ' + plugin.name + ' reported hit'); promises.push(plugin.result(input)); } } } else { for(let i = 0; i < this.pmPlugins.length; i++) { var plugin = this.pmPlugins[i]; if(plugin.test(input)) { logger.info('* Plugin ' + plugin.name + ' reported hit'); promises.push(plugin.result(input)); } } } return Promise.all(promises); } } // export default BasePlugin; module.exports = PluginManager;