Add plugin auto-detection and loading

This will probably break something later, because it loads the plugins after the bot is initialized
This commit is contained in:
7cc5c4f330d47060 2024-10-20 17:41:10 -04:00
parent 040bb752f1
commit 1a4d9bc52e
Signed by: 7cc5c4f330d47060
SSH key fingerprint: SHA256:e+4tcZut1nBpe10PqjaO+Rvie0Q7W4qIvFzcUw+7riA
2 changed files with 19 additions and 0 deletions

View file

@ -2,9 +2,24 @@ import { createClient } from "minecraft-protocol";
import { default as settings } from './settings.json' with {type: "json"} import { default as settings } from './settings.json' with {type: "json"}
import { generateUser } from './util/usergen.js' import { generateUser } from './util/usergen.js'
import EventEmitter from 'node:events' import EventEmitter from 'node:events'
import { readdirSync } from "node:fs";
const bots = []; 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 createBot = function createBot (host, oldId) {
const bot = new EventEmitter() const bot = new EventEmitter()
bot._client = createClient({ bot._client = createClient({

4
plugins/helloworld.js Normal file
View file

@ -0,0 +1,4 @@
const load = (b) => {
console.log(`Test plugin loaded on bot ${b.id}`)
}
export { load }