CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

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