From 5085cf363adcf4a810acf62ccdb9d3ea6c15d18d Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Tue, 13 Aug 2019 20:34:46 +0300 Subject: [PATCH] Call plugin functions with an object instead Previously, plugin functions were called with 4 arguments. These 4 arguments are now part of the passed object instead, making the plugin system much more scalable in the future. --- README.md | 14 ++++++++------ src/main.js | 2 +- src/modules/alert.js | 2 +- src/modules/block.js | 2 +- src/modules/close.js | 2 +- src/modules/greeting.js | 2 +- src/modules/id.js | 2 +- src/modules/logs.js | 2 +- src/modules/move.js | 2 +- src/modules/newthread.js | 2 +- src/modules/reply.js | 2 +- src/modules/snippets.js | 2 +- src/modules/suspend.js | 2 +- src/modules/typingProxy.js | 2 +- src/modules/version.js | 2 +- 15 files changed, 22 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ccbb66f..b7777ed 100644 --- a/README.md +++ b/README.md @@ -126,21 +126,23 @@ The path is relative to the bot's folder. ### Creating a plugin Create a `.js` file that exports a function. -This function will be called when the plugin is loaded with the following arguments: `(bot, knex, config, commands)` -where `bot` is the [Eris Client object](https://abal.moe/Eris/docs/Client), -`knex` is the [Knex database object](https://knexjs.org/#Builder), -`config` is the loaded config object, -and `commands` is an object with functions to add and manage commands (see bottom of [src/commands.js](src/commands.js)) +This function will be called when the plugin is loaded with an object that has the following properties: +* `bot` - the [Eris Client object](https://abal.moe/Eris/docs/Client) +* `knex` - the [Knex database object](https://knexjs.org/#Builder) +* `config` - the loaded config +* `commands` - an object with functions to add and manage commands (see bottom of [src/commands.js](src/commands.js)) #### Example plugin file ```js -module.exports = function(bot, knex, config, commands) { +module.exports = function({ bot, knex, config, commands }) { commands.addInboxThreadCommand('mycommand', [], (msg, args, thread) => { thread.replyToUser(msg.author, 'Reply from my custom plugin!'); }); } ``` +(Note the use of [object destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Unpacking_fields_from_objects_passed_as_function_parameter) in the function parameters) + ### Work in progress The current plugin API is fairly rudimentary and will be expanded in the future. The API can change in non-major releases during this early stage. Keep an eye on [CHANGELOG.md](CHANGELOG.md) for any changes. diff --git a/src/main.js b/src/main.js index 23085d1..cb09a4b 100644 --- a/src/main.js +++ b/src/main.js @@ -265,7 +265,7 @@ function initPlugins() { } plugins.forEach(pluginFn => { - pluginFn(bot, knex, config, commands); + pluginFn({ bot, knex, config, commands }); }); console.log(`Loaded ${plugins.length} plugins (${builtInPlugins.length} built-in plugins, ${plugins.length - builtInPlugins.length} external plugins)`); diff --git a/src/modules/alert.js b/src/modules/alert.js index f2c49c0..a844230 100644 --- a/src/modules/alert.js +++ b/src/modules/alert.js @@ -1,4 +1,4 @@ -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { commands.addInboxThreadCommand('alert', '[opt:string]', async (msg, args, thread) => { if (args.opt && args.opt.startsWith('c')) { await thread.setAlert(null); diff --git a/src/modules/block.js b/src/modules/block.js index 2658136..94913c4 100644 --- a/src/modules/block.js +++ b/src/modules/block.js @@ -3,7 +3,7 @@ const moment = require('moment'); const blocked = require("../data/blocked"); const utils = require("../utils"); -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { async function removeExpiredBlocks() { const expiredBlocks = await blocked.getExpiredBlocks(); const logChannel = utils.getLogChannel(); diff --git a/src/modules/close.js b/src/modules/close.js index fad5a91..60feb54 100644 --- a/src/modules/close.js +++ b/src/modules/close.js @@ -6,7 +6,7 @@ const threads = require('../data/threads'); const blocked = require('../data/blocked'); const {messageQueue} = require('../queue'); -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { // Check for threads that are scheduled to be closed and close them async function applyScheduledCloses() { const threadsToBeClosed = await threads.getThreadsThatShouldBeClosed(); diff --git a/src/modules/greeting.js b/src/modules/greeting.js index 6371638..540d9ac 100644 --- a/src/modules/greeting.js +++ b/src/modules/greeting.js @@ -2,7 +2,7 @@ const path = require('path'); const fs = require('fs'); const config = require('../config'); -module.exports = bot => { +module.exports = ({ bot }) => { if (! config.enableGreeting) return; bot.on('guildMemberAdd', (guild, member) => { diff --git a/src/modules/id.js b/src/modules/id.js index 9024fd1..e3cf2e2 100644 --- a/src/modules/id.js +++ b/src/modules/id.js @@ -1,4 +1,4 @@ -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { commands.addInboxThreadCommand('id', [], async (msg, args, thread) => { thread.postSystemMessage(thread.user_id); }); diff --git a/src/modules/logs.js b/src/modules/logs.js index 76009e8..0664c07 100644 --- a/src/modules/logs.js +++ b/src/modules/logs.js @@ -4,7 +4,7 @@ const utils = require("../utils"); const LOG_LINES_PER_PAGE = 10; -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { const logsCmd = async (msg, args, thread) => { let userId = args.userId || (thread && thread.user_id); if (! userId) return; diff --git a/src/modules/move.js b/src/modules/move.js index dd63166..973143d 100644 --- a/src/modules/move.js +++ b/src/modules/move.js @@ -3,7 +3,7 @@ const Eris = require('eris'); const transliterate = require("transliteration"); const erisEndpoints = require('eris/lib/rest/Endpoints'); -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { if (! config.allowMove) return; commands.addInboxThreadCommand('move', '', async (msg, args, thread) => { diff --git a/src/modules/newthread.js b/src/modules/newthread.js index ccd63db..aca6f54 100644 --- a/src/modules/newthread.js +++ b/src/modules/newthread.js @@ -1,7 +1,7 @@ const utils = require("../utils"); const threads = require("../data/threads"); -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { commands.addInboxServerCommand('newthread', '', async (msg, args, thread) => { const user = bot.users.get(args.userId); if (! user) { diff --git a/src/modules/reply.js b/src/modules/reply.js index 1e1d5ea..bc2afe3 100644 --- a/src/modules/reply.js +++ b/src/modules/reply.js @@ -1,7 +1,7 @@ const attachments = require("../data/attachments"); const utils = require('../utils'); -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { // Mods can reply to modmail threads using !r or !reply // These messages get relayed back to the DM thread between the bot and the user commands.addInboxThreadCommand('reply', '[text$]', async (msg, args, thread) => { diff --git a/src/modules/snippets.js b/src/modules/snippets.js index e7cc3ba..e9c1271 100644 --- a/src/modules/snippets.js +++ b/src/modules/snippets.js @@ -7,7 +7,7 @@ const { parseArguments } = require('knub-command-manager'); const whitespaceRegex = /\s/; const quoteChars = ["'", '"']; -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { /** * "Renders" a snippet by replacing all argument placeholders e.g. {1} {2} with their corresponding arguments. * The number in the placeholder is the argument's order in the argument list, i.e. {1} is the first argument (= index 0) diff --git a/src/modules/suspend.js b/src/modules/suspend.js index fed3859..7edd096 100644 --- a/src/modules/suspend.js +++ b/src/modules/suspend.js @@ -5,7 +5,7 @@ const config = require('../config'); const {THREAD_STATUS} = require('../data/constants'); -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { // Check for threads that are scheduled to be suspended and suspend them async function applyScheduledSuspensions() { const threadsToBeSuspended = await threads.getThreadsThatShouldBeSuspended(); diff --git a/src/modules/typingProxy.js b/src/modules/typingProxy.js index d2e32cd..5881808 100644 --- a/src/modules/typingProxy.js +++ b/src/modules/typingProxy.js @@ -2,7 +2,7 @@ const config = require('../config'); const threads = require("../data/threads"); const Eris = require("eris"); -module.exports = bot => { +module.exports = ({ bot }) => { // Typing proxy: forwarding typing events between the DM and the modmail thread if(config.typingProxy || config.typingProxyReverse) { bot.on("typingStart", async (channel, user) => { diff --git a/src/modules/version.js b/src/modules/version.js index 10c4cfd..033fbf0 100644 --- a/src/modules/version.js +++ b/src/modules/version.js @@ -10,7 +10,7 @@ const readFile = promisify(fs.readFile); const GIT_DIR = path.join(__dirname, '..', '..', '.git'); -module.exports = (bot, knex, config, commands) => { +module.exports = ({ bot, knex, config, commands }) => { commands.addInboxServerCommand('version', [], async (msg, args, thread) => { const packageJson = require('../../package.json'); const packageVersion = packageJson.version;