|
|
|
@ -29,22 +29,21 @@ cfg.botOptions = options |
|
|
|
|
|
|
|
|
|
let plugins = {} |
|
|
|
|
|
|
|
|
|
function loadplugin(pluginname, pluginpath = './plugins/' + pluginname) { |
|
|
|
|
function loadplugin(pluginname, pluginpath) { |
|
|
|
|
try { |
|
|
|
|
plugins[pluginname] = require(pluginpath) |
|
|
|
|
plugins[pluginname].load(cfg) |
|
|
|
|
} catch (error) { |
|
|
|
|
if (error.code == 'MODULE_NOT_FOUND') { |
|
|
|
|
console.warn('plugin not used:', pluginpath) |
|
|
|
|
} else if (plugins[pluginname] && !plugins[pluginname].load) { |
|
|
|
|
unloadplugin(pluginname, pluginpath) |
|
|
|
|
} else { |
|
|
|
|
console.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function unloadplugin(pluginname, pluginpath = './plugins/' + pluginname) { |
|
|
|
|
function unloadplugin(pluginname, pluginpath) { |
|
|
|
|
pluginpath = pluginpath ? pluginpath : './plugins/' + pluginname |
|
|
|
|
const plugin = require.resolve(pluginpath) |
|
|
|
|
try { |
|
|
|
|
if (plugin && require.cache[plugin]) { |
|
|
|
@ -59,28 +58,20 @@ function unloadplugin(pluginname, pluginpath = './plugins/' + pluginname) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function reloadplugin(event, filename, pluginpath = './plugins/') { |
|
|
|
|
reloadplugin = (event, filename, pluginpath) => { |
|
|
|
|
if (!/\.js$/.test(filename)) { return } |
|
|
|
|
filename = filename.replace("\\", "/") // windows
|
|
|
|
|
if (!cfg.fsTimeout) { |
|
|
|
|
console.info(event, filename) |
|
|
|
|
const fullpluginpath = pluginpath + filename |
|
|
|
|
const pluginname = (filename.split(".js")[0]).replace(/\/.+/, "") |
|
|
|
|
pluginpath = pluginpath + pluginname |
|
|
|
|
const hassubplugin = fullpluginpath.replace(/\.js$/, "") !== pluginpath |
|
|
|
|
pluginpath = (pluginpath ? pluginpath : './plugins/') + filename |
|
|
|
|
const check = Object.keys(cfg.plugins) |
|
|
|
|
console.info(`reload file: ./lib/${fullpluginpath}`) |
|
|
|
|
const plugin = require.resolve(fullpluginpath) |
|
|
|
|
console.info(`reload file: ./lib/${pluginpath}`) |
|
|
|
|
const plugin = require.resolve(pluginpath) |
|
|
|
|
if (plugin && require.cache[plugin]) { |
|
|
|
|
// console.debug(Object.keys(cfg.plugins))
|
|
|
|
|
unloadplugin(pluginname) |
|
|
|
|
unloadplugin(filename.split(".js")[0], pluginpath) |
|
|
|
|
console.assert(Object.keys(cfg.plugins).length == check.length - 1, "plugin not removed, potential memory leak") |
|
|
|
|
if (hassubplugin) { |
|
|
|
|
console.info("reload: also unloading sub") |
|
|
|
|
unloadplugin(pluginname, fullpluginpath) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
loadplugin(pluginname) |
|
|
|
|
loadplugin(filename.split(".js")[0], pluginpath) |
|
|
|
|
if (Object.keys(cfg.plugins).length != check.length) { |
|
|
|
|
// If left < right :
|
|
|
|
|
// - new plugin that's not registered in cfg.plugins, so added
|
|
|
|
@ -94,7 +85,7 @@ function reloadplugin(event, filename, pluginpath = './plugins/') { |
|
|
|
|
// console.log('file.js %s event', event)
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fs.watch('./lib/plugins', { recursive: true }, reloadplugin) |
|
|
|
|
fs.watch('./lib/plugins', reloadplugin) |
|
|
|
|
|
|
|
|
|
cfg.bot = bot |
|
|
|
|
// TODO better name, or switch to array
|
|
|
|
@ -121,8 +112,6 @@ bot.once("spawn", () => { |
|
|
|
|
|
|
|
|
|
cfg.plugins = plugins |
|
|
|
|
cfg.botAddressRegex = new RegExp(`^${bot.username} (${cfg.botAddressPrefix}.+)`) |
|
|
|
|
// FIXME leaks every load, so adding here instead of command.js to load only once
|
|
|
|
|
bot.addChatPattern("web", /\[WEB\] (\[.+\])?\s*([\w.]+): (.+)/, { parse: true }) |
|
|
|
|
|
|
|
|
|
for (const plugin of Object.values(plugins)) { |
|
|
|
|
try { |
|
|
|
|