This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/mcping.py
2012-02-27 22:46:47 -08:00

37 lines
1 KiB
Python
Executable file

import socket
import struct
from util import hook
def get_info(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, port))
sock.send('\xfe')
response = sock.recv(1)
if response != '\xff':
return "Server gave invalid response: "+repr(response)
length = struct.unpack('!h', sock.recv(2))[0]
values = sock.recv(length*2).decode('utf-16be').split(u'\xa7')
sock.close()
return "%s - %d/%d players" % (values[0], int(values[1]), int(values[2]))
except:
return "Error pinging "+host+":"+str(port)+", is it up? Double-check your address!"
@hook.command
def mcping(inp):
".mcping server[:port] - Ping a Minecraft server to check status."
inp = inp.strip().split(" ")[0]
if ":" in inp:
host, port = inp.split(":", 1)
try:
port = int(port)
except:
return "Invalid port!"
else:
host = inp
port = 25565
return get_info(host, port)