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/chat_parsing.js
Views: 788
1
/*
2
this example is to show the different ways to parse chat messages
3
4
Expected output in console:
5
6
bot has just joined!
7
bot has just left!
8
bot has just rejoined!
9
bot has just left!
10
*/
11
const mineflayer = require('mineflayer')
12
const bot = mineflayer.createBot({
13
host: 'localhost',
14
username: 'bot'
15
})
16
17
bot.once('spawn', () => {
18
bot.addChatPattern('bot_left_the_game', /bot. left the game/)
19
bot.addChatPatternSet('bot_rejoins_the_game', [/bot. left the game/, /bot. joined the game/])
20
bot.addChatPattern('who_just_joined', /(.+) joined the game/, { parse: true, repeat: false })
21
makeChatMessages()
22
})
23
24
bot.on('chat:bot_left_the_game', matches => {
25
// => ['bot left the game']
26
console.log('bot has just left!')
27
})
28
29
bot.on('chat:bot_rejoins_the_game', matches => {
30
// => ['bot left the game', 'bot joined the game']
31
console.log('bot has just rejoined!')
32
})
33
34
bot.on('chat:who_just_joined', matches => {
35
console.log(`${matches[0]} has just joined!`) // should only run once
36
})
37
38
async function makeChatMessages () {
39
let bot1 = mineflayer.createBot({
40
host: 'localhost',
41
username: 'bot1'
42
})
43
let bot2
44
setTimeout(() => bot1.quit(), 1500)
45
setTimeout(() => {
46
bot1 = mineflayer.createBot({
47
host: 'localhost',
48
username: 'bot1'
49
})
50
}, 1750)
51
setTimeout(() => {
52
bot2 = mineflayer.createBot({
53
host: 'localhost',
54
username: 'bot2'
55
}, 2000)
56
})
57
setTimeout(() => bot2.quit(), 2500)
58
}
59
60
bot.on('error', console.log)
61
62