angram/lib/plugins/command.js
jay 4f1e510386 feat: 🎉 init new repo
Dump of current working bot.

Warning: somewhat messy code! Lints haven't been run, no tests, etc.
2020-12-21 21:08:38 +05:00

323 lines
11 KiB
JavaScript

const v = require('vec3')
let mcData = require('minecraft-data')
let cfg = {}
let bot = {}
// bot.chatAddPattern(new RegExp(`^hi|hello ${bot.username}`, "i"), 'greet', "General greeting")
function todo() {
bot.chat("not implemented yet")
}
function checkBlockExists(name) {
if (mcData.blocksByName[name] === undefined) {
bot.chat(`${name} is not a block name`)
return false
} else {
return true
}
}
function checkItemExists(name) {
if (mcData.itemsByName[name] === undefined) {
bot.chat(`${name} is not an item name`)
return false
} else {
return true
}
}
const events = {
whisper: function command_whisper(username, message) {
if ([bot.username, "me"].includes(username)) return
if (/^gossip/.test(message)) return
// if (/^!/.test(message) && username === cfg.admin){
if (username === cfg.admin) {
message = message.replace("\\", "@")
console.info("whispered command", message)
bot.chat("/" + message)
} else {
bot.whisper(cfg.admin, `gossip ${username}: ${message}`)
console.info(username, "whispered", message)
}
}
, chat: command
, kicked: (reason, loggedIn) => console.warn(reason, loggedIn)
}
const events_registered = []
function command(username, message) {
if (username === bot.username && !message.startsWith("!")) return
const player = bot.players[username] ? bot.players[username].entity : null
if (message.startsWith("zzz")) {
bot.chat("/afk")
cfg.plugins.sleeper.sleep()
// if(cfg)
return
}
switch (message) {
case "hi":
case "hello":
case "howdy":
case "yo":
bot.swingArm()
return
case "69":
Math.random() > 0.5 && bot.chat("nice!")
return
case "awesome":
case "cool":
case "superb":
case "nice":
Math.random() > 0.1 && setTimeout(() => bot.chat("very nice!"), 1000)
return
default:
break;
}
if (message.startsWith("!") || cfg.botAddress.test(message)) {
message = cfg.botAddress.test(message) ? cfg.botAddress.exec(message)[1] : message
console.log(message)
message = message.slice(1)
message_parts = message.split(/\s+/)
switch (message_parts[0]) {
// case "follow":
// // if(username === cfg.admin)
// cfg.stateMachines.follow.transitions[4].trigger()
// // cfg.stateMachines.follow.transitions[1].trigger()
// break;
// case "stay":
// // if(username === cfg.admin)
// cfg.stateMachines.follow.transitions[3].trigger()
// break;
case "echo":
// bot.chat(message_parts[1])
// bot.chat(message.slice(1))
bot.chat(message)
break;
case "autosleep":
case "sleep":
message_parts[1] = message_parts[0] == "autosleep" ? "auto" : message_parts[1]
switch (message_parts[1]) {
case "auto":
cfg.sleep.auto = !cfg.sleep.auto
bot.chat(`ok, ${cfg.sleep.auto ? "" : "not "}auto sleeping `)
bot.chat("/afk")
break;
case "silent":
case "quiet":
cfg.sleep.quiet = !cfg.sleep.quiet
bot.chat(`ok, ${cfg.sleep.quiet ? "" : "not "}sleeping quietly`)
break;
case "timeout":
if (!message_parts[2]) {
bot.chat(`sleeping every ${Math.round(cfg.sleep.timeout / 60 / 1000)}mins`)
return
}
const timeout = parseFloat(message_parts[2], 10)
if (timeout) {
cfg.sleep.timeout = timeout * 60 * 1000
cfg.sleep.timeoutFn = null
cfg.plugins.sleeper.sleep(true) //reset timer
bot.chat(`ok, next sleep attempt in ${timeout}mins`)
break;
}
default:
bot.chat(`usage: !sleep [auto | quiet | timeout <mins>], or zzz for manual sleep`)
break;
}
break; //todo; needed?
case "zzz":
cfg.plugins.sleeper.sleep()
case "afk":
bot.chat("/afk")
break;
case "awaken":
case "wake":
case "wakeup":
cfg.plugins.sleeper.wake()
break;
case "attack":
case "rage":
case "ragemode":
case "guard":
switch (message_parts.length) {
case 1:
if (!player) {
bot.chat("can't see you.")
return
}
bot.chat('for glory!')
cfg.plugins.guard.guardArea(player.position)
break
case 2:
switch (message_parts[1]) {
case "self":
cfg.guard.self = !cfg.guard.self
bot.chat(`ok, ${cfg.guard.self?"no longer being altruistic":"self sacrifice!"}`)
return;
case "stop":
cfg.plugins.guard.stopGuarding()
bot.chat('no longer guarding this area.')
return;
default:
break;
}
default:
bot.chat("don't know wym")
}
// entity = new BehaviorGetClosestEntity(bot, bot.nearestEntity(), a => EntityFilters().MobsOnly(a))
// if (entity) {
// bot.attack(entity, true)
// } else {
// bot.chat('no nearby entities')
// }
// // bot.attack(new BehaviorGetClosestEntity(bot, {}, EntityFilters().MobsOnly()))
break;
case "find":
switch (message_parts.length) {
case 2:
cfg.plugins.finder.findBlocks(message_parts[1])
break
default:
break;
}
break
case "lookat":
// const coords = v(message_parts.splice(1))
switch (message_parts.length) {
case 2:
case 3:
todo()
// bot.lookAt({})
break
case 1:
bot.lookAt(bot.nearestEntity().position)
break
case 4:
bot.lookAt(v(message_parts.splice(1)))
break
default:
break
}
break
case "ride":
case "mount":
bot.mount(bot.nearestEntity())
break
case "getoff":
case "unmount":
case "dismount":
bot.dismount()
break
case "go":
bot.moveVehicle(0, 10)
break
// case "use":
// bot.useOn(bot.nearestEntity())
// break;
// // TODO move all inventory related tasks into own module
// case "give":
// // switch (message_parts[1]) {
// // case "hand":
// // case "right":
// // break;
// // case "left":
// // break;
// // // case "slot":
// // default:
// // let slot = parseInt(message_parts[1])
// // if (!isNaN(slot)) {
// // } else {
// // }
// // break;
// // }
// // break;
// case "take":
// // TODO take only what's requested, then throw all the rest
// // TODO take all
// case "toss":
// case "drop":
// if (!message_parts[1]) { return false } // FIXME, works but ugly
// if (!checkItemExists(message_parts[1])) { return false }
// switch (message_parts.length) {
// case 2:
// bot.toss(mcData.blocksByName[message_parts[1]].id)
// break
// case 3:
// bot.tossStack(
// mcData.itemsByName[message_parts[1]].id,
// (err) => {
// if (err) {
// console.log(err)
// bot.chat(err)
// }
// }
// )
// break
// default:
// break
// }
// break;
case "location":
switch (message_parts[1]) {
case "add":
case "record":
break;
default:
break;
}
case "where":
case "where?":
// TODO put in /lib/location
console.log(bot.entity.position)
bot.chat(bot.entity.position.floored().toString())
break;
case "warp":
// if(message_parts[1] == "spawn")
bot.chat("/" + message)
break;
default:
if (cfg.mods.includes(username))
bot.chat("/" + message)
break;
}
}
}
const load = (config) => {
cfg = config
bot = cfg.bot
mcData = mcData(bot.version)
for (const [key, fn] of Object.entries(events)) {
events_registered.push(
bot.on(key, fn)
)
}
mcData = require('minecraft-data')(bot.version)
}
const unload = () => {
console.log("events_registered:", events_registered.length, "of", bot._eventsCount)
for (const [key, fn] of Object.entries(events)) {
bot.off(key, fn)
events_registered.shift()
}
console.log("events_registered:", bot._eventsCount)
}
module.exports = { load, unload, events_registered, command }