diff --git a/src/data/attachments.js b/src/data/attachments.js index dd27080..330f71f 100644 --- a/src/data/attachments.js +++ b/src/data/attachments.js @@ -16,6 +16,8 @@ const localAttachmentDir = config.attachmentDir || `${__dirname}/../../attachmen const attachmentSavePromises = {}; +const attachmentStorageTypes = {}; + function getErrorResult(msg = null) { return { url: `Attachment could not be saved${msg ? ': ' + msg : ''}`, @@ -171,10 +173,8 @@ function saveAttachment(attachment) { return attachmentSavePromises[attachment.id]; } - if (config.attachmentStorage === 'local') { - attachmentSavePromises[attachment.id] = saveLocalAttachment(attachment); - } else if (config.attachmentStorage === 'discord') { - attachmentSavePromises[attachment.id] = saveDiscordAttachment(attachment); + if (attachmentStorageTypes[config.attachmentStorage]) { + attachmentSavePromises[attachment.id] = Promise.resolve(attachmentStorageTypes[config.attachmentStorage](attachment)); } else { throw new Error(`Unknown attachment storage option: ${config.attachmentStorage}`); } @@ -186,8 +186,16 @@ function saveAttachment(attachment) { return attachmentSavePromises[attachment.id]; } +function addStorageType(name, handler) { + attachmentStorageTypes[name] = handler; +} + +attachmentStorageTypes.local = saveLocalAttachment; +attachmentStorageTypes.discord = saveDiscordAttachment; + module.exports = { getLocalAttachmentPath, attachmentToFile, - saveAttachment + saveAttachment, + addStorageType }; diff --git a/src/main.js b/src/main.js index cb09a4b..992f71f 100644 --- a/src/main.js +++ b/src/main.js @@ -11,6 +11,7 @@ const { createCommandManager } = require('./commands'); 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,7 +266,13 @@ function initPlugins() { } plugins.forEach(pluginFn => { - pluginFn({ bot, knex, config, commands }); + pluginFn({ + bot, + knex, + config, + commands, + attachments, + }); }); console.log(`Loaded ${plugins.length} plugins (${builtInPlugins.length} built-in plugins, ${plugins.length - builtInPlugins.length} external plugins)`);