Compare commits

..

No commits in common. "5b3804893b3ff6e71c1f277b93661b6ae107051b" and "ae1f7cf269b97822802a4457dcaef23160d5a651" have entirely different histories.

3 changed files with 17 additions and 51 deletions

View File

@ -29,22 +29,21 @@ cfg.botOptions = options
let plugins = {} let plugins = {}
function loadplugin(pluginname, pluginpath = './plugins/' + pluginname) { function loadplugin(pluginname, pluginpath) {
try { try {
plugins[pluginname] = require(pluginpath) plugins[pluginname] = require(pluginpath)
plugins[pluginname].load(cfg) plugins[pluginname].load(cfg)
} catch (error) { } catch (error) {
if (error.code == 'MODULE_NOT_FOUND') { if (error.code == 'MODULE_NOT_FOUND') {
console.warn('plugin not used:', pluginpath) console.warn('plugin not used:', pluginpath)
} else if (plugins[pluginname] && !plugins[pluginname].load) {
unloadplugin(pluginname, pluginpath)
} else { } else {
console.error(error) console.error(error)
} }
} }
} }
function unloadplugin(pluginname, pluginpath = './plugins/' + pluginname) { function unloadplugin(pluginname, pluginpath) {
pluginpath = pluginpath ? pluginpath : './plugins/' + pluginname
const plugin = require.resolve(pluginpath) const plugin = require.resolve(pluginpath)
try { try {
if (plugin && require.cache[plugin]) { if (plugin && require.cache[plugin]) {
@ -59,28 +58,20 @@ function unloadplugin(pluginname, pluginpath = './plugins/' + pluginname) {
} }
} }
function reloadplugin(event, filename, pluginpath = './plugins/') { reloadplugin = (event, filename, pluginpath) => {
if (!/\.js$/.test(filename)) { return } if (!/\.js$/.test(filename)) { return }
filename = filename.replace("\\", "/") // windows
if (!cfg.fsTimeout) { if (!cfg.fsTimeout) {
console.info(event, filename) console.info(event, filename)
const fullpluginpath = pluginpath + filename pluginpath = (pluginpath ? pluginpath : './plugins/') + filename
const pluginname = (filename.split(".js")[0]).replace(/\/.+/, "")
pluginpath = pluginpath + pluginname
const hassubplugin = fullpluginpath.replace(/\.js$/, "") !== pluginpath
const check = Object.keys(cfg.plugins) const check = Object.keys(cfg.plugins)
console.info(`reload file: ./lib/${fullpluginpath}`) console.info(`reload file: ./lib/${pluginpath}`)
const plugin = require.resolve(fullpluginpath) const plugin = require.resolve(pluginpath)
if (plugin && require.cache[plugin]) { if (plugin && require.cache[plugin]) {
// console.debug(Object.keys(cfg.plugins)) // console.debug(Object.keys(cfg.plugins))
unloadplugin(pluginname) unloadplugin(filename.split(".js")[0], pluginpath)
console.assert(Object.keys(cfg.plugins).length == check.length - 1, "plugin not removed, potential memory leak") console.assert(Object.keys(cfg.plugins).length == check.length - 1, "plugin not removed, potential memory leak")
if (hassubplugin) {
console.info("reload: also unloading sub")
unloadplugin(pluginname, fullpluginpath)
} }
} loadplugin(filename.split(".js")[0], pluginpath)
loadplugin(pluginname)
if (Object.keys(cfg.plugins).length != check.length) { if (Object.keys(cfg.plugins).length != check.length) {
// If left < right : // If left < right :
// - new plugin that's not registered in cfg.plugins, so added // - new plugin that's not registered in cfg.plugins, so added
@ -94,7 +85,7 @@ function reloadplugin(event, filename, pluginpath = './plugins/') {
// console.log('file.js %s event', event) // console.log('file.js %s event', event)
} }
fs.watch('./lib/plugins', { recursive: true }, reloadplugin) fs.watch('./lib/plugins', reloadplugin)
cfg.bot = bot cfg.bot = bot
// TODO better name, or switch to array // TODO better name, or switch to array
@ -121,8 +112,6 @@ bot.once("spawn", () => {
cfg.plugins = plugins cfg.plugins = plugins
cfg.botAddressRegex = new RegExp(`^${bot.username} (${cfg.botAddressPrefix}.+)`) cfg.botAddressRegex = new RegExp(`^${bot.username} (${cfg.botAddressPrefix}.+)`)
// FIXME leaks every load, so adding here instead of command.js to load only once
bot.addChatPattern("web", /\[WEB\] (\[.+\])?\s*([\w.]+): (.+)/, { parse: true })
for (const plugin of Object.values(plugins)) { for (const plugin of Object.values(plugins)) {
try { try {

View File

@ -56,29 +56,6 @@ const events = {
}, 15 * 60 * 1000, bot, cfg); }, 15 * 60 * 1000, bot, cfg);
} }
} }
, message: systemMessage
, "chat:web": commandWeb
}
function systemMessage(...args) {
if (args[0]?.text) return
const metadata = (args[0]?.extra || args[0]?.with)?.map(v => v.text)
console.log(
"cmd msg:",
args[0]?.text || args[0]?.translate,
args[0]?.extra?.length || args[0]?.with?.length || Object.keys(args[0]?.json).length - 1,
metadata.length > 0 && metadata || args[0],
args.slice(1)
)
}
function commandWeb([[mode, username, message]]) {
console.log("web msg:", mode, username, message)
message && command(username, message)
}
function _clientSystemMessage(...args) {
console.log("cmd msg:", args[0]?.message, args[0]?.extra?.length, args[0]?.extra, args[0], args.slice(1))
} }
const events_registered = [] const events_registered = []

View File

@ -30,16 +30,16 @@
}, },
"homepage": "https://github.com/PrismarineJS/prismarine-template#readme", "homepage": "https://github.com/PrismarineJS/prismarine-template#readme",
"devDependencies": { "devDependencies": {
"@types/node": "^14.14.41", "@types/node": "^14.14.35",
"jest": "^26.6.3", "jest": "^26.6.3",
"require-self": "^0.2.3", "require-self": "^0.2.3",
"typescript": "^4.2.4" "typescript": "^4.2.3"
}, },
"dependencies": { "dependencies": {
"dotenv-packed": "^1.2.2", "dotenv-packed": "^1.2.1",
"minecraft-data": "^2.84.0", "minecraft-data": "^2.80.0",
"mineflayer": "^3.6.0", "mineflayer": "^3.4.0",
"mineflayer-armor-manager": "^1.4.1", "mineflayer-armor-manager": "^1.4.0",
"mineflayer-pathfinder": "^1.6.1", "mineflayer-pathfinder": "^1.6.1",
"mineflayer-pvp": "^1.0.2", "mineflayer-pvp": "^1.0.2",
"prismarine-block": "^1.8.0", "prismarine-block": "^1.8.0",
@ -49,7 +49,7 @@
"prismarine-nbt": "^1.5.0", "prismarine-nbt": "^1.5.0",
"prismarine-recipe": "^1.1.0", "prismarine-recipe": "^1.1.0",
"vec3": "^0.1.7", "vec3": "^0.1.7",
"xstate": "^4.18.0" "xstate": "^4.17.0"
}, },
"files": [ "files": [
"lib/**/*" "lib/**/*"