From 380c0ba48323d2d2f805efe66dce86cb8dfb5ae3 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Tue, 8 Nov 2022 17:43:24 +0700 Subject: [PATCH] load files load plugins real11! --- index.js | 24 ++---------------------- util/loadPlugins.js | 19 +++++++++++++++++++ util/load_files.js | 20 ++++++++++---------- 3 files changed, 31 insertions(+), 32 deletions(-) create mode 100644 util/loadPlugins.js diff --git a/index.js b/index.js index e032a0f..1bb2264 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/util/loadPlugins.js b/util/loadPlugins.js new file mode 100644 index 0000000..606d717 --- /dev/null +++ b/util/loadPlugins.js @@ -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}; diff --git a/util/load_files.js b/util/load_files.js index 1f16e43..e991c65 100644 --- a/util/load_files.js +++ b/util/load_files.js @@ -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 = [] +function loadPlugins(directory) { + 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 \ No newline at end of file +module.exports = loadPlugins;