forked from amalbansode/Discord-Minecraft-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminestat.js
122 lines (94 loc) · 4.16 KB
/
minestat.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
/*
* minestat.js - A Minecraft server status checker
* Copyright (C) 2016 Lloyd Dilley
* http://www.dilley.me/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
const net = require('net');
const NUM_FIELDS = 6; // number of values expected from server
const DEFAULT_TIMEOUT = 5; // default TCP timeout in seconds
/*
* You do not need these. this.<var_name> creates a new variable on the object.
* address = null;
* port = null;
* online = null; // online or offline?
* version = null; // server version
* motd = null; // message of the day
* current_players = null; // current number of players online
* max_players = null; // maximum player capacity
* latency = null; // ping time to server in milliseconds
*/
module.exports = {
init(address, port, timeout, callback) {
this.address = address;
this.port = port;
// if 3rd argument is a function, it's the callback (timeout is optional)
// 'typeof' returns a string; you can directly compare it.
// A better/worse way (depending on how you think of it) is using
// Function{}.length which returns the number of arguments of a function.
if (typeof timeout === 'function') {
callback = timeout;
timeout = DEFAULT_TIMEOUT;
}
// Using 'const' and 'var' declarations together may cause problems with
// scoping.
const startTime = new Date();
const client = net.connect(port, address, () => {
this.latency = Math.round(new Date() - startTime);
client.write(Buffer.from([ 0xFE, 0x01 ])); // Magic Numbers. Explain why you're using these.
});
client.setTimeout(timeout * 1000);
client.on('data', data => {
// '' and null are falsy types. You can use !data
// but that won't prevent you from explicit undefined and pass-by-reference
// null/undef checks.
if (data) {
// \x00 and \u0000 are null terminators. Specifiy that.
// One in raw hex and the other in unicode points.
const serverInfo = data.toString().split('\x00\x00\x00');
if (serverInfo && serverInfo.length >= NUM_FIELDS) {
this.online = true;
this.version = serverInfo[2].replace(/\u0000/g, '');
this.motd = serverInfo[3].replace(/\u0000/g, '');
this.current_players = serverInfo[4].replace(/\u0000/g, '');
this.max_players = serverInfo[5].replace(/\u0000/g, '');
} else {
this.online = false;
}
}
callback();
client.end();
});
client.on('timeout', () => {
client.end();
callback();
// It's always good to specify an exit code.
process.exit(1);
});
// EOT (end of transmission) is important. You can do cleanups here after a cycle.
client.on('end', () => {});
client.on('error', err => {
switch (err.code) {
case 'ENOTFOUND':
console.log(`Unable to resolve: %s.`, this.host);
break;
case 'ECONNREFUSED':
console.log(`Unable to connect to %s:%s.`, this.host, this.port);
}
callback(err);
});
}
};