Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/examples/chatterbox.js
Views: 789
/*1* This example demonstrates how easy it is to create a bot2* that sends chat messages whenever something interesting happens3* on the server you are connected to.4*5* Below you can find a wide range of different events you can watch6* but remember to check out the API documentation to find even more!7*8* Some events may be commented out because they are very frequent and9* may flood the chat, feel free to check them out for other purposes though.10*11* This bot also replies to some specific chat messages so you can ask him12* a few information while you are in game.13*/14const mineflayer = require('mineflayer')15const { Vec3 } = require('vec3')1617if (process.argv.length < 4 || process.argv.length > 6) {18console.log('Usage : node chatterbot.js <host> <port> [<name>] [<password>]')19process.exit(1)20}2122const bot = mineflayer.createBot({23host: process.argv[2],24port: parseInt(process.argv[3]),25username: process.argv[4] ? process.argv[4] : 'chatterbox',26password: process.argv[5]27})2829bot.on('chat', (username, message) => {30if (username === bot.username) return31const result = /canSee (-?[0-9]+),(-?[0-9]+),(-?[0-9]+)/.exec(message)32if (result) {33canSee(new Vec3(result[1], result[2], result[3]))34return35}36switch (message) {37case 'pos':38sayPosition(username)39break40case 'wearing':41sayEquipment()42break43case 'nick':44sayNick()45break46case 'spawn':47saySpawnPoint()48break49case 'block':50sayBlockUnder(username)51break52case 'quit':53quit(username)54break55default:56bot.chat("That's nice")57}5859function canSee (pos) {60const block = bot.blockAt(pos)61const r = bot.canSeeBlock(block)62if (r) {63bot.chat(`I can see the block of ${block.displayName} at ${pos}`)64} else {65bot.chat(`I cannot see the block of ${block.displayName} at ${pos}`)66}67}6869function sayPosition (username) {70bot.chat(`I am at ${bot.entity.position}`)71bot.chat(`You are at ${bot.players[username].entity.position}`)72}7374function sayEquipment () {75const eq = bot.players[username].entity.equipment76const eqText = []77if (eq[0]) eqText.push(`holding a ${eq[0].displayName}`)78if (eq[1]) eqText.push(`wearing a ${eq[1].displayName} on your feet`)79if (eq[2]) eqText.push(`wearing a ${eq[2].displayName} on your legs`)80if (eq[3]) eqText.push(`wearing a ${eq[3].displayName} on your torso`)81if (eq[4]) eqText.push(`wearing a ${eq[4].displayName} on your head`)82if (eqText.length) {83bot.chat(`You are ${eqText.join(', ')}.`)84} else {85bot.chat('You are naked!')86}87}8889function saySpawnPoint () {90bot.chat(`Spawn is at ${bot.spawnPoint}`)91}9293function sayBlockUnder () {94const block = bot.blockAt(bot.players[username].entity.position.offset(0, -1, 0))95bot.chat(`Block under you is ${block.displayName} in the ${block.biome.name} biome`)96console.log(block)97}9899function quit (username) {100bot.quit(`${username} told me to`)101}102103function sayNick () {104bot.chat(`My name is ${bot.player.displayName}`)105}106})107108bot.on('whisper', (username, message, rawMessage) => {109console.log(`I received a message from ${username}: ${message}`)110bot.whisper(username, 'I can tell secrets too.')111})112bot.on('nonSpokenChat', (message) => {113console.log(`Non spoken chat: ${message}`)114})115116bot.on('login', () => {117bot.chat('Hi everyone!')118})119bot.on('spawn', () => {120bot.chat('I spawned, watch out!')121})122bot.on('spawnReset', (message) => {123bot.chat('Oh noez! My bed is broken.')124})125bot.on('forcedMove', () => {126bot.chat(`I have been forced to move to ${bot.entity.position}`)127})128bot.on('health', () => {129bot.chat(`I have ${bot.health} health and ${bot.food} food`)130})131bot.on('death', () => {132bot.chat('I died x.x')133})134bot.on('kicked', (reason) => {135console.log(`I got kicked for ${reason}`)136})137138bot.on('time', () => {139bot.chat('Current time: ' + bot.time.timeOfDay)140})141bot.on('rain', () => {142if (bot.isRaining) {143bot.chat('It started raining.')144} else {145bot.chat('It stopped raining.')146}147})148bot.on('noteHeard', (block, instrument, pitch) => {149bot.chat(`Music for my ears! I just heard a ${instrument.name}`)150})151bot.on('chestLidMove', (block, isOpen) => {152const action = isOpen ? 'open' : 'close'153bot.chat(`Hey, did someone just ${action} a chest?`)154})155bot.on('pistonMove', (block, isPulling, direction) => {156const action = isPulling ? 'pulling' : 'pushing'157bot.chat(`A piston is ${action} near me, i can hear it.`)158})159160bot.on('playerJoined', (player) => {161if (player.username !== bot.username) {162bot.chat(`Hello, ${player.username}! Welcome to the server.`)163}164})165bot.on('playerLeft', (player) => {166if (player.username === bot.username) return167bot.chat(`Bye ${player.username}`)168})169bot.on('playerCollect', (collector, collected) => {170if (collector.type === 'player') {171const item = collected.getDroppedItem()172bot.chat(`${collector.username !== bot.username ? ("I'm so jealous. " + collector.username) : 'I '} collected ${item.count} ${item.displayName}`)173}174})175176bot.on('entitySpawn', (entity) => {177if (entity.type === 'mob') {178console.log(`Look out! A ${entity.displayName} spawned at ${entity.position}`)179} else if (entity.type === 'player') {180bot.chat(`Look who decided to show up: ${entity.username}`)181} else if (entity.type === 'object') {182console.log(`There's a ${entity.displayName} at ${entity.position}`)183} else if (entity.type === 'global') {184bot.chat('Ooh lightning!')185} else if (entity.type === 'orb') {186bot.chat('Gimme dat exp orb!')187}188})189bot.on('entityHurt', (entity) => {190if (entity.type === 'mob') {191bot.chat(`Haha! The ${entity.displayName} got hurt!`)192} else if (entity.type === 'player') {193bot.chat(`Aww, poor ${entity.username} got hurt. Maybe you shouldn't have a ping of ${bot.players[entity.username].ping}`)194}195})196bot.on('entitySwingArm', (entity) => {197bot.chat(`${entity.username}, I see that your arm is working fine.`)198})199bot.on('entityCrouch', (entity) => {200bot.chat(`${entity.username}: you so sneaky.`)201})202bot.on('entityUncrouch', (entity) => {203bot.chat(`${entity.username}: welcome back from the land of hunchbacks.`)204})205bot.on('entitySleep', (entity) => {206bot.chat(`Good night, ${entity.username}`)207})208bot.on('entityWake', (entity) => {209bot.chat(`Top of the morning, ${entity.username}`)210})211bot.on('entityEat', (entity) => {212bot.chat(`${entity.username}: OM NOM NOM NOMONOM. That's what you sound like.`)213})214bot.on('entityAttach', (entity, vehicle) => {215if (entity.type === 'player' && vehicle.type === 'object') {216bot.chat(`Sweet, ${entity.username} is riding that ${vehicle.displayName}`)217}218})219bot.on('entityDetach', (entity, vehicle) => {220if (entity.type === 'player' && vehicle.type === 'object') {221bot.chat(`Lame, ${entity.username} stopped riding the ${vehicle.displayName}`)222}223})224bot.on('entityEquipmentChange', (entity) => {225console.log('entityEquipmentChange', entity)226})227bot.on('entityEffect', (entity, effect) => {228console.log('entityEffect', entity, effect)229})230bot.on('entityEffectEnd', (entity, effect) => {231console.log('entityEffectEnd', entity, effect)232})233234235