load files load plugins real11!

This commit is contained in:
ChomeNS 2022-11-08 17:43:24 +07:00
parent d173d980da
commit 380c0ba483
3 changed files with 31 additions and 32 deletions

View file

@ -11,8 +11,7 @@ const sleep = require('sleep-promise');
const {containsIllegalCharacters} = require('./util/containsIllegalCharacters');
const generateEaglerUsername = require('./util/generateEaglerUsername');
const {EventEmitter} = require('events');
const fs = require('fs');
const path = require('path');
const {loadPlugins} = require('./util/loadPlugins');
const uuid = require('uuid-by-string');
const readline = require('node:readline');
const {stdin: input, stdout: output} = require('node:process');
@ -87,26 +86,7 @@ function botThings() {
bot.visibility = false;
bot.getplayerusername = {};
// allink's plugin loader
const plugins = []; // NOTE: DO NOT CHANGE, PLUGINS ARE LOADED AUTOMATICALLY
fs.readdirSync(
path.join(__dirname, 'plugins'),
).forEach(function(file) { // populate plugins array
if (file.endsWith('.js')) {
plugins.push(path.join(__dirname, 'plugins', file));
}
});
plugins.forEach(function(plugin) { // load plugins
let name = plugin.split('/');
name = name[name.length - 1];
try {
const plug = require(plugin);
plug.inject(bot, dcclient, config);
} catch (e) {
console.log(`Plugin loader: Plugin ${name} is having exception loading the plugin:`);
console.log(util.inspect(e));
}
});
loadPlugins(bot, dcclient, config);
}
let messageloggingEnabled = true;

19
util/loadPlugins.js Normal file
View file

@ -0,0 +1,19 @@
const fs = require('fs/promises');
const util = require('util');
const path = require('path');
async function loadPlugins(bot, dcclient, config) {
const plugins = await fs.readdir(path.join(__dirname, '..', 'plugins'));
plugins.forEach((plugin) => {
try {
const plug = require(path.join(__dirname, '..', 'plugins', plugin));
plug.inject(bot, dcclient, config);
} catch (e) {
console.log(`Plugin ${plugin} is having exception loading the plugin:`);
console.log(util.inspect(e));
}
});
};
module.exports = {loadPlugins};

View file

@ -1,20 +1,20 @@
const fs = require('fs')
const path = require('path')
const fs = require('fs');
const path = require('path');
function loadPlugins(directory) {
const plugins = []
const plugins = [];
for (const filename of fs.readdirSync(directory)) {
if (!filename.endsWith('.js')) continue
if (!filename.endsWith('.js')) continue;
const filepath = path.join(directory, filename)
const filepath = path.join(directory, filename);
const plugin = require(filepath)
const plugin = require(filepath);
plugins.push(plugin)
plugins.push(plugin);
}
return plugins
return plugins;
}
module.exports = loadPlugins
module.exports = loadPlugins;