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.
master
Dragory 2019-08-13 20:34:46 +03:00
parent eea6a0c131
commit 5085cf363a
15 changed files with 22 additions and 20 deletions

View File

@ -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.

View File

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

View File

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

View File

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

View File

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

View File

@ -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) => {

View File

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

View File

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

View File

@ -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', '<category:string$>', async (msg, args, thread) => {

View File

@ -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', '<userId:userId>', async (msg, args, thread) => {
const user = bot.users.get(args.userId);
if (! user) {

View File

@ -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) => {

View File

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

View File

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

View File

@ -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) => {

View File

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