2020-08-12 17:08:37 -04:00
|
|
|
const attachments = require("./data/attachments");
|
2020-09-22 20:16:26 -04:00
|
|
|
const logs = require("./data/logs");
|
2020-08-12 17:08:37 -04:00
|
|
|
const { beforeNewThread } = require("./hooks/beforeNewThread");
|
2020-09-22 19:28:41 -04:00
|
|
|
const { afterThreadClose } = require("./hooks/afterThreadClose");
|
2020-08-12 17:08:37 -04:00
|
|
|
const formats = require("./formatters");
|
2020-10-03 19:10:13 -04:00
|
|
|
const { server: webserver } = require("./modules/webserver");
|
2020-10-04 09:46:43 -04:00
|
|
|
const childProcess = require("child_process");
|
|
|
|
const pacote = require("pacote");
|
|
|
|
const path = require("path");
|
2020-10-12 13:00:25 -04:00
|
|
|
const threads = require("./data/threads");
|
2020-10-04 09:46:43 -04:00
|
|
|
|
|
|
|
const pluginSources = {
|
|
|
|
npm: {
|
|
|
|
install(plugins) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
console.log(`Installing ${plugins.length} plugins from NPM...`);
|
|
|
|
|
|
|
|
let stderr = "";
|
|
|
|
const npmProcess = childProcess.spawn("npm", ["install", "--no-save", ...plugins], { cwd: process.cwd() });
|
|
|
|
npmProcess.stderr.on("data", data => { stderr += String(data) });
|
|
|
|
npmProcess.on("close", code => {
|
|
|
|
if (code !== 0) {
|
|
|
|
return reject(new Error(stderr));
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
async load(plugin, pluginApi) {
|
|
|
|
const manifest = await pacote.manifest(plugin);
|
|
|
|
const packageName = manifest.name;
|
|
|
|
const pluginFn = require(packageName);
|
|
|
|
if (typeof pluginFn !== "function") {
|
|
|
|
throw new Error(`Plugin '${plugin}' is not a valid plugin`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pluginFn(pluginApi);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
file: {
|
|
|
|
install(plugins) {},
|
|
|
|
load(plugin, pluginApi) {
|
|
|
|
const requirePath = path.join(__dirname, "..", plugin);
|
|
|
|
const pluginFn = require(requirePath);
|
|
|
|
if (typeof pluginFn !== "function") {
|
|
|
|
throw new Error(`Plugin '${plugin}' is not a valid plugin`);
|
|
|
|
}
|
|
|
|
return pluginFn(pluginApi);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultPluginSource = "file";
|
|
|
|
|
|
|
|
function splitPluginSource(pluginName) {
|
|
|
|
for (const pluginSource of Object.keys(pluginSources)) {
|
|
|
|
if (pluginName.startsWith(`${pluginSource}:`)) {
|
|
|
|
return {
|
|
|
|
source: pluginSource,
|
|
|
|
plugin: pluginName.slice(pluginSource.length + 1),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
source: defaultPluginSource,
|
|
|
|
plugin: pluginName,
|
|
|
|
};
|
|
|
|
}
|
2019-09-17 19:15:22 -04:00
|
|
|
|
|
|
|
module.exports = {
|
2020-10-04 09:46:43 -04:00
|
|
|
async installPlugins(plugins) {
|
|
|
|
const pluginsBySource = {};
|
|
|
|
|
|
|
|
for (const pluginName of plugins) {
|
|
|
|
const { source, plugin } = splitPluginSource(pluginName);
|
|
|
|
pluginsBySource[source] = pluginsBySource[source] || [];
|
|
|
|
pluginsBySource[source].push(plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [source, sourcePlugins] of Object.entries(pluginsBySource)) {
|
|
|
|
await pluginSources[source].install(sourcePlugins);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async loadPlugins(plugins, pluginApi) {
|
|
|
|
for (const pluginName of plugins) {
|
|
|
|
const { source, plugin } = splitPluginSource(pluginName);
|
|
|
|
await pluginSources[source].load(plugin, pluginApi);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-08-16 16:26:04 -04:00
|
|
|
/**
|
|
|
|
* @param bot
|
|
|
|
* @param knex
|
|
|
|
* @param config
|
|
|
|
* @param commands
|
|
|
|
* @returns {PluginAPI}
|
|
|
|
*/
|
2019-09-17 19:15:22 -04:00
|
|
|
getPluginAPI({ bot, knex, config, commands }) {
|
|
|
|
return {
|
|
|
|
bot,
|
|
|
|
knex,
|
|
|
|
config,
|
|
|
|
commands: {
|
|
|
|
manager: commands.manager,
|
|
|
|
addGlobalCommand: commands.addGlobalCommand,
|
|
|
|
addInboxServerCommand: commands.addInboxServerCommand,
|
|
|
|
addInboxThreadCommand: commands.addInboxThreadCommand,
|
|
|
|
addAlias: commands.addAlias
|
|
|
|
},
|
|
|
|
attachments: {
|
|
|
|
addStorageType: attachments.addStorageType,
|
|
|
|
downloadAttachment: attachments.downloadAttachment
|
|
|
|
},
|
2020-09-22 20:16:26 -04:00
|
|
|
logs: {
|
|
|
|
addStorageType: logs.addStorageType,
|
|
|
|
saveLogToStorage: logs.saveLogToStorage,
|
|
|
|
getLogUrl: logs.getLogUrl,
|
|
|
|
getLogFile: logs.getLogFile,
|
|
|
|
getLogCustomResponse: logs.getLogCustomResponse,
|
|
|
|
},
|
2020-07-13 17:14:31 -04:00
|
|
|
hooks: {
|
|
|
|
beforeNewThread,
|
2020-09-22 19:28:41 -04:00
|
|
|
afterThreadClose,
|
2020-07-13 17:14:31 -04:00
|
|
|
},
|
2020-07-13 18:11:48 -04:00
|
|
|
formats,
|
2020-10-03 19:10:13 -04:00
|
|
|
webserver,
|
2020-10-12 13:00:25 -04:00
|
|
|
threads,
|
2019-09-17 19:15:22 -04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|