-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (159 loc) · 5.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
require('dotenv').config();
const mineflayer = require('mineflayer');
const { mineflayer: mineflayerViewer } = require('prismarine-viewer');
const db = require('./queries');
require('./heroku');
/**
* Create the bot instance.
*/
const bot = mineflayer.createBot({
host: process.env.MC_HOST,
username: process.env.MC_USERNAME,
password: process.env.MC_PASSWORD,
auth: process.env.MC_AUTH,
});
const createViewer = () => {
console.log('--- VIEWER --- Creating the viewer...');
// Destroy the current viewer if exists.
/*if (bot.viewer) {
console.log('--- VIEWER --- Deleting the old viewer');
bot.viewer.close();
bot.viewer = undefined;
}*/
mineflayerViewer(bot, { port: process.env.PORT || 3000 });
};
/**
* Load any plugins for the bot.
*/
/*bot.loadPlugin(require('mineflayer-dashboard')({
chatPattern: /^» \w+? » /
}));*/
/*bot.once('inject_allowed', () => {
if (bot.dashboard) {
// Override the default console.log to display on the dashboard if available.
global.console.log = bot.dashboard.log;
global.console.error = bot.dashboard.log;
}
});*/
//bot.once('spawn', createViewer);
const getPlayers = (playersList) => {
return Object.keys(playersList).filter(p => !p.startsWith('|')).map(u => {
return {
uuid: playersList[u].uuid,
username: playersList[u].username,
};
});
};
const registerPlayers = (playersArr) => {
playersArr.forEach(player => {
db.createPlayer(player);
});
};
let status = null;
let reconnectCount = 0;
let mapName = '';
bot.on('spawn', () => {
createViewer();
const playersArr = getPlayers(bot.players);
console.log('PLAYERS', playersArr.length, JSON.stringify(playersArr));
//registerPlayers(playersArr);
bot.chat('/pgm:mapinfo'); // Spits out the map info.
status = 'MAPINFO';
});
/*bot.on('chat', (username, message) => {
if (username === bot.username) return;
console.log('CHAT', username + ': ' + message);
});*/
const sanitizePlayerName = (playerName) => {
const filter = /([_a-zA-Z0-9]+)/i; // Filters out stars and mod symbols.
return playerName.match(filter)[0];
};
const traversal = (obj, cb = fn => fn) => {
if (obj.hasOwnProperty('extra')) {
obj.extra.forEach(o => {
cb(o);
traversal(o, cb);
});
}
};
const filterMessages = [
'was shot by', 'was blown up', 'was slain by', 'was punched out', 'was knocked out', 'was shot out', 'was picked up',
'was knocked off', 'fell out', 'was blown off', 'was shot off', 'joined the game', 'left the game', 'hit the ground',
'blocks and died', 'out of the world', 'fell off a high place', 'was sniped off', 'was punched off', 'tripped and fell',
'was sniped by', 'went splat', 'died', 'was killed by', 'was knocked into', 'was shot into', 'felt the fury', 'was spleefed off',
'went up in flames'
];
bot.on('message', (jsonMsg) => {
const stringMessage = jsonMsg.toString();
// Filter out death messages
if (stringMessage.search(filterMessages.join('|')) >= 0) {
return;
}
// Filter out other messages.
if (stringMessage.startsWith('<')
|| stringMessage.startsWith('[')
|| stringMessage.startsWith('(')
) {
return;
}
// Check for statuses emitted by the bot.
if (status === 'MAPINFO') {
let mapString = stringMessage.trim();
mapName = mapString;
status = null;
return;
}
// Checks for private messages.
if (stringMessage.startsWith('From ')) {
const [, playerData, playerMessage] = stringMessage.split(' ', 3);
let [playerName,] = playerData.split(':');
playerName = sanitizePlayerName(playerName);
// Skip messages that aren't flags ("!").
if (!playerMessage.startsWith('!')) {
console.log('MSG', stringMessage);
return;
}
const [command, extra] = playerMessage.substr(1).split(' ');
switch (command) {
case 'tp':
if (typeof extra !== 'undefined') {
// Teleport to a player.
bot.chat(`/tp ${extra}`);
} else {
// Teleport to the same player.
bot.chat(`/tp ${playerName}`);
}
break;
case 'hello':
bot.chat(`/msg ${playerName} Greetings, ${playerName}!`);
break;
case 'map':
bot.chat(`/msg ${playerName} Now playing: ${mapName}`);
break;
case 'players':
const playersArr = getPlayers(bot.players);
bot.chat(`/msg ${playerName} There are currently ${playersArr.length} players online.`);
break;
default:
bot.chat(`/msg ${playerName} Sorry, I don't recognize that command.`);
return;
}
console.log('COMMAND', command, extra);
}
// Try to rejoin the server after a certain period.
if (stringMessage.includes('Reconnecting to Overcast Community')) {
if (reconnectCount > 12) {
bot.chat('/occ');
reconnectCount = 0;
} else {
reconnectCount++;
}
}
console.log('SERVER', stringMessage);
});
/*bot.on('whisper', (username, message) => {
if (username === bot.username) return;
console.log('WHISPER', username + ': ' + message);
});*/
bot.on('kicked', console.log);
bot.on('error', console.log);