node-minecraft-protocol/examples/server_ping/ping.js

29 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-04-11 16:55:53 -04:00
const protocol = require('minecraft-protocol') // Lets define protocol
if (process.argv.length < 3 || process.argv.length > 3) { // Check for args for prevent crashing etc.
console.log('Usage : node ping.js <host>:[<port>]')
process.exit(1)
}
function removeColorsFromString (text) { // Removing minecraft colors from strings, because console can`t read it and it will look crazy.
return text.replace(/§./g, '')
2021-04-11 16:55:53 -04:00
}
let host
let port
if (!process.argv[2].includes(':')) { // Spliting ip and port if available.
host = process.argv[2]
port = 25565
} else {
[host, port] = process.argv[2].split(':')
port = parseInt(port)
}
2022-11-11 17:24:15 -05:00
protocol.ping({ host, port }, (err, pingResults) => { // Pinging server and getting result
2021-04-11 16:55:53 -04:00
if (err) throw err
console.log(`${removeColorsFromString(JSON.stringify(pingResults.description.text))}`) // Printing motd to console
// Printing some infos to console
console.log(`${JSON.stringify(pingResults.latency)} ms | ${JSON.stringify(pingResults.players.online)}/${JSON.stringify(pingResults.players.max)} | ${JSON.stringify(removeColorsFromString(pingResults.version.name))}.${JSON.stringify(pingResults.version.protocol)}`)
})