From 2ea858daac8f9e760dac0bcdea9f8ccdb7efb4db Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Wed, 18 Sep 2019 02:15:22 +0300 Subject: [PATCH] Move plugin loading to its own file. Define plugin API more explicitly. --- src/data/Thread.js | 4 ++-- src/data/attachments.js | 9 +++++---- src/main.js | 13 +++---------- src/plugins.js | 26 ++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 src/plugins.js diff --git a/src/data/Thread.js b/src/data/Thread.js index 4a2badd..23e9e4a 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -67,7 +67,7 @@ class Thread { let savedAttachment; await Promise.all([ - attachments.attachmentToFile(attachment).then(file => { + attachments.attachmentToDiscordFileObject(attachment).then(file => { files.push(file); }), attachments.saveAttachment(attachment).then(result => { @@ -146,7 +146,7 @@ class Thread { logContent += formatted; // Logs always contain the link if (config.relaySmallAttachmentsAsAttachments && attachment.size <= 1024 * 1024 * 2) { - const file = await attachments.attachmentToFile(attachment); + const file = await attachments.attachmentToDiscordFileObject(attachment); attachmentFiles.push(file); } else { threadContent += formatted; diff --git a/src/data/attachments.js b/src/data/attachments.js index 330f71f..40b13b0 100644 --- a/src/data/attachments.js +++ b/src/data/attachments.js @@ -128,7 +128,7 @@ async function saveDiscordAttachment(attachment) { 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); if (! savedAttachment) return getErrorResult(); @@ -156,7 +156,7 @@ async function createDiscordAttachmentMessage(channel, file, tries = 0) { * @param {Object} attachment * @returns {Promise<{file, name: string}>} */ -async function attachmentToFile(attachment) { +async function attachmentToDiscordFileObject(attachment) { const downloadResult = await downloadAttachment(attachment); const data = await readFile(downloadResult.path); downloadResult.cleanup(); @@ -195,7 +195,8 @@ attachmentStorageTypes.discord = saveDiscordAttachment; module.exports = { getLocalAttachmentPath, - attachmentToFile, + attachmentToDiscordFileObject, saveAttachment, - addStorageType + addStorageType, + downloadAttachment }; diff --git a/src/main.js b/src/main.js index 992f71f..16d7058 100644 --- a/src/main.js +++ b/src/main.js @@ -7,11 +7,11 @@ const knex = require('./knex'); const {messageQueue} = require('./queue'); const utils = require('./utils'); const { createCommandManager } = require('./commands'); +const { getPluginAPI, loadPlugin } = require('./plugins'); const blocked = require('./data/blocked'); const threads = require('./data/threads'); const updates = require('./data/updates'); -const attachments = require('./data/attachments'); const reply = require('./modules/reply'); const close = require('./modules/close'); @@ -265,15 +265,8 @@ function initPlugins() { } } - plugins.forEach(pluginFn => { - pluginFn({ - bot, - knex, - config, - commands, - attachments, - }); - }); + const pluginApi = getPluginAPI({ bot, knex, config, commands }); + plugins.forEach(pluginFn => loadPlugin(pluginFn, pluginApi)); console.log(`Loaded ${plugins.length} plugins (${builtInPlugins.length} built-in plugins, ${plugins.length - builtInPlugins.length} external plugins)`); diff --git a/src/plugins.js b/src/plugins.js new file mode 100644 index 0000000..64c9df6 --- /dev/null +++ b/src/plugins.js @@ -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); + } +};