Add support for adding attachment storage types in plugins

master
Dragory 2019-09-18 01:52:16 +03:00
parent 5244cce31c
commit 0f61966d4e
2 changed files with 21 additions and 6 deletions

View File

@ -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
};

View File

@ -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)`);