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

89 lines
2.8 KiB
Python
Raw Normal View History

from util import hook
from util import http
2011-11-21 12:24:23 +01:00
import string
import socket
import struct
def mcping_connect(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':
2012-04-02 18:17:55 +02:00
return "Server gave invalid response: " + repr(response)
length = struct.unpack('!h', sock.recv(2))[0]
2012-04-02 18:17:55 +02:00
values = sock.recv(length * 2).decode('utf-16be').split(u'\xa7')
sock.close()
2012-04-02 18:17:55 +02:00
return "%s - %d/%d players"\
% (values[0], int(values[1]), int(values[2]))
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)
2011-11-21 12:03:41 +01:00
def mcstatus(inp, bot=None):
2012-02-28 03:03:43 +01:00
".mcstatus -- 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-04-02 18:17:55 +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
2011-11-20 10:23:31 +01:00
@hook.command
def mclogin(inp, say=None):
2012-04-02 18:17:55 +02:00
".mclogin <username> <password> -- Attempts to log in to Minecraft with "\
" <username> and <password> (This is NOT logged)."
2011-11-20 10:23:31 +01:00
inp = inp.split(" ")
username = inp[0]
password = inp[1]
say("Attempting to log in using " + username)
2012-04-02 18:17:55 +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():
2011-11-20 10:23:31 +01:00
return "I logged in with " + username
else:
2012-04-02 18:17:55 +02:00
return "I couldn't log in using " + username + ", either"\
" the password is wrong or Minecraft login servers are down!"
2011-11-21 12:03:41 +01:00
@hook.command
def mcpaid(inp):
2012-04-02 18:17:55 +02:00
".mcpaid <username> -- Checks if <username> has a "\
"premium Minecraft account."
2011-11-21 12:24:23 +01:00
login = http.get("http://www.minecraft.net/haspaid.jsp?user=" + inp)
2011-11-21 12:03:41 +01:00
if "true" in login:
2012-04-02 18:17:55 +02:00
return "The account \'" + inp + "\' is a "\
"premium Minecraft account!"
2011-11-21 12:03:41 +01:00
else:
2012-04-02 18:17:55 +02:00
return "The account \'" + inp + "\' is not a "\
"premium Minecraft account!"
from util import hook
@hook.command
def mcping(inp):
2012-03-19 04:09:26 +01: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
port = 25565
return mcping_connect(host, port)