Move plugin loading to its own file. Define plugin API more explicitly.

master
Dragory 2019-09-18 02:15:22 +03:00
parent 45e314b942
commit 2ea858daac
4 changed files with 36 additions and 16 deletions

View File

@ -67,7 +67,7 @@ class Thread {
let savedAttachment; let savedAttachment;
await Promise.all([ await Promise.all([
attachments.attachmentToFile(attachment).then(file => { attachments.attachmentToDiscordFileObject(attachment).then(file => {
files.push(file); files.push(file);
}), }),
attachments.saveAttachment(attachment).then(result => { attachments.saveAttachment(attachment).then(result => {
@ -146,7 +146,7 @@ class Thread {
logContent += formatted; // Logs always contain the link logContent += formatted; // Logs always contain the link
if (config.relaySmallAttachmentsAsAttachments && attachment.size <= 1024 * 1024 * 2) { if (config.relaySmallAttachmentsAsAttachments && attachment.size <= 1024 * 1024 * 2) {
const file = await attachments.attachmentToFile(attachment); const file = await attachments.attachmentToDiscordFileObject(attachment);
attachmentFiles.push(file); attachmentFiles.push(file);
} else { } else {
threadContent += formatted; threadContent += formatted;

View File

@ -128,7 +128,7 @@ async function saveDiscordAttachment(attachment) {
throw new Error('Attachment storage channel must be a text channel!'); throw new Error('Attachment storage channel must be a text channel!');
} }
const file = await attachmentToFile(attachment); const file = await attachmentToDiscordFileObject(attachment);
const savedAttachment = await createDiscordAttachmentMessage(attachmentChannel, file); const savedAttachment = await createDiscordAttachmentMessage(attachmentChannel, file);
if (! savedAttachment) return getErrorResult(); if (! savedAttachment) return getErrorResult();
@ -156,7 +156,7 @@ async function createDiscordAttachmentMessage(channel, file, tries = 0) {
* @param {Object} attachment * @param {Object} attachment
* @returns {Promise<{file, name: string}>} * @returns {Promise<{file, name: string}>}
*/ */
async function attachmentToFile(attachment) { async function attachmentToDiscordFileObject(attachment) {
const downloadResult = await downloadAttachment(attachment); const downloadResult = await downloadAttachment(attachment);
const data = await readFile(downloadResult.path); const data = await readFile(downloadResult.path);
downloadResult.cleanup(); downloadResult.cleanup();
@ -195,7 +195,8 @@ attachmentStorageTypes.discord = saveDiscordAttachment;
module.exports = { module.exports = {
getLocalAttachmentPath, getLocalAttachmentPath,
attachmentToFile, attachmentToDiscordFileObject,
saveAttachment, saveAttachment,
addStorageType addStorageType,
downloadAttachment
}; };

View File

@ -7,11 +7,11 @@ const knex = require('./knex');
const {messageQueue} = require('./queue'); const {messageQueue} = require('./queue');
const utils = require('./utils'); const utils = require('./utils');
const { createCommandManager } = require('./commands'); const { createCommandManager } = require('./commands');
const { getPluginAPI, loadPlugin } = require('./plugins');
const blocked = require('./data/blocked'); const blocked = require('./data/blocked');
const threads = require('./data/threads'); const threads = require('./data/threads');
const updates = require('./data/updates'); const updates = require('./data/updates');
const attachments = require('./data/attachments');
const reply = require('./modules/reply'); const reply = require('./modules/reply');
const close = require('./modules/close'); const close = require('./modules/close');
@ -265,15 +265,8 @@ function initPlugins() {
} }
} }
plugins.forEach(pluginFn => { const pluginApi = getPluginAPI({ bot, knex, config, commands });
pluginFn({ plugins.forEach(pluginFn => loadPlugin(pluginFn, pluginApi));
bot,
knex,
config,
commands,
attachments,
});
});
console.log(`Loaded ${plugins.length} plugins (${builtInPlugins.length} built-in plugins, ${plugins.length - builtInPlugins.length} external plugins)`); console.log(`Loaded ${plugins.length} plugins (${builtInPlugins.length} built-in plugins, ${plugins.length - builtInPlugins.length} external plugins)`);

26
src/plugins.js Normal file
View File

@ -0,0 +1,26 @@
const attachments = require('./data/attachments');
module.exports = {
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
},
};
},
loadPlugin(plugin, api) {
plugin(api);
}
};