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/mctools.py

111 lines
3.5 KiB
Python
Raw Normal View History

from util import hook, http
import socket
2012-08-27 14:29:03 +02:00
import json
import struct
2012-08-27 14:32:04 +02:00
def mcping_connect(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, port))
sock.send('\xfe\x01')
response = sock.recv(1)
print response
if response[0] != '\xff':
2012-04-02 18:17:55 +02:00
return "Server gave invalid response: " + repr(response)
length = struct.unpack('!h', sock.recv(2))[0]
values = sock.recv(length * 2).decode('utf-16be')
data = values.split(u'\x00') # try to decode data using new format
if len(data) == 1:
# failed to decode data, server is using old format
data = values.split(u'\xa7')
message = u"{} - {}/{} players".format(data[0], data[1], data[2])
else:
# decoded data, server is using new format
message = u"{} - {} - {}/{} players".format(data[3], data[2], data[4], data[5])
sock.close()
return message
except:
2012-04-02 18:17:55 +02:00
return "Error pinging " + host + ":" + str(port) +\
", is it up? Double-check your address!"
2011-11-20 10:23:31 +01:00
2011-11-21 07:56:14 +01:00
@hook.command(autohelp=False)
2012-08-27 14:29:03 +02:00
def mclogin(inp, bot=None):
"mclogin -- Checks the status of Minecraft's login servers."
2012-02-24 04:17:59 +01:00
username = bot.config.get("api_keys", {}).get("mc_user", None)
password = bot.config.get("api_keys", {}).get("mc_pass", None)
2012-02-24 04:23:16 +01:00
if password is None:
return "error: no login set"
2012-02-24 04:17:59 +01:00
2012-05-15 00:16:41 +02:00
login = http.get("https://login.minecraft.net/", user=username,
password=password, version=13)
2011-11-21 12:24:23 +01:00
if username.lower() in login.lower():
return "Minecraft login servers appear to be online!"
2011-11-20 10:23:31 +01:00
else:
2012-02-28 03:03:43 +01:00
return "Minecraft login servers appear to be offline!"
2011-11-20 10:23:31 +01:00
2012-04-02 18:17:55 +02:00
2012-08-27 14:29:03 +02:00
@hook.command(autohelp=False)
def mcstatus(inp, say=None):
"mcstatus -- Checks the status of various Mojang (the creators of Minecraft) servers."
2012-05-15 00:16:41 +02:00
2012-09-05 21:16:56 +02:00
try:
request = http.get("http://status.mojang.com/check")
except (http.URLError, http.HTTPError) as e:
return "Unable to get Minecraft server status: {}".format(e)
2012-09-05 21:16:56 +02:00
# change the json from a list of dictionaies to a dictionary
2012-08-27 14:29:03 +02:00
data = json.loads(request.replace("}", "").replace("{", "").replace("]", "}").replace("[", "{"))
2012-05-15 00:16:41 +02:00
2012-08-27 14:29:03 +02:00
out = []
# use a loop so we don't have to update it if they add more servers
for server, status in data.items():
if status == "green":
out.append("{} is \x033\x02online\x02\x03".format(server))
2012-08-27 14:29:03 +02:00
else:
out.append("{} is \x034\x02offline\x02\x03".format(server))
2012-08-27 14:29:03 +02:00
return ", ".join(out) + "."
2012-04-02 18:17:55 +02:00
2011-11-21 12:03:41 +01:00
2012-08-27 14:32:04 +02:00
@hook.command("haspaid")
2011-11-21 12:03:41 +01:00
@hook.command
def mcpaid(inp):
"mcpaid <username> -- Checks if <username> has a premium Minecraft account."
2012-05-15 00:16:41 +02:00
user = inp.strip()
try:
status = http.get("http://www.minecraft.net/haspaid.jsp", user=user)
except (http.URLError, http.HTTPError) as e:
return "Unable to get user registration status: {}".format(e)
if "true" in status:
return 'The account "{}" is a premium Minecraft account!'.format(inp)
2011-11-21 12:03:41 +01:00
else:
return 'The account "{}" is not a premium Minecraft account!'.format(inp)
@hook.command
def mcping(inp):
2012-05-16 05:07:27 +02:00
"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:
2012-03-19 04:09:26 +01:00
return "error: invalid port!"
else:
host = inp
2012-08-27 14:32:04 +02:00
port = 25565
return mcping_connect(host, port)