chomens-bot-js/util/loadPlugins.js

31 lines
977 B
JavaScript
Raw Normal View History

/* eslint-disable max-len */
2022-11-08 05:43:24 -05:00
const fs = require('fs/promises');
const util = require('util');
const path = require('path');
2022-11-15 21:33:16 -05:00
/**
* load plugins
* @param {object} bot the bot object
* @param {object} dcclient discord client
* @param {object} config the config
2022-11-20 02:29:11 -05:00
* @param {object} rl readline.
* @param {boolean} oneTime load plugins one time
2022-11-15 21:33:16 -05:00
*/
2022-11-20 02:29:11 -05:00
async function loadPlugins(bot, dcclient, config, rl, oneTime) {
2022-11-17 19:46:52 -05:00
const dir = path.join(__dirname, '..', 'plugins');
const plugins = await fs.readdir(dir);
2022-11-17 08:05:47 -05:00
plugins.forEach((plugin) => {
if (!plugin.endsWith('.js')) return;
try {
2022-11-17 19:46:52 -05:00
const plug = require(path.join(dir, plugin));
2022-11-20 02:29:11 -05:00
if (!oneTime) plug.inject(bot, dcclient, config, rl);
if (oneTime && plug.oneTimeInject) plug.oneTimeInject(bot, dcclient, config, rl);
2022-11-17 08:05:47 -05:00
} catch (e) {
console.log(`Plugin ${plugin} is having exception loading the plugin:`);
console.log(util.inspect(e));
}
});
2022-11-08 05:43:24 -05:00
};
module.exports = {loadPlugins};