From 1a4d9bc52e4359692fa5367e49fb6e3bb3474b6d Mon Sep 17 00:00:00 2001 From: 7cc5c4f330d47060 Date: Sun, 20 Oct 2024 17:41:10 -0400 Subject: [PATCH] Add plugin auto-detection and loading This will probably break something later, because it loads the plugins after the bot is initialized --- index.js | 15 +++++++++++++++ plugins/helloworld.js | 4 ++++ 2 files changed, 19 insertions(+) create mode 100644 plugins/helloworld.js diff --git a/index.js b/index.js index d3b30a9..cf0b652 100644 --- a/index.js +++ b/index.js @@ -2,9 +2,24 @@ import { createClient } from "minecraft-protocol"; import { default as settings } from './settings.json' with {type: "json"} import { generateUser } from './util/usergen.js' import EventEmitter from 'node:events' +import { readdirSync } from "node:fs"; const bots = []; +const bpl = readdirSync('plugins') +for (const plugin of bpl) { + if (!plugin.endsWith('.js')) { + continue + } + try { + import(`./plugins/${plugin}`).then((pluginItem)=>{ + for(const bot of bots){ + pluginItem.load(bot) + } + }) + } catch (e) { console.log(e) } +} + const createBot = function createBot (host, oldId) { const bot = new EventEmitter() bot._client = createClient({ diff --git a/plugins/helloworld.js b/plugins/helloworld.js new file mode 100644 index 0000000..7287595 --- /dev/null +++ b/plugins/helloworld.js @@ -0,0 +1,4 @@ +const load = (b) => { + console.log(`Test plugin loaded on bot ${b.id}`) +} +export { load } \ No newline at end of file