Last active
August 29, 2015 14:02
-
-
Save zekesonxx/01dfc62626d1fd429ec6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hexchat | |
import json | |
import httplib | |
# HexChat Escapecraft IRC Plugin | |
# Copyright (C) 2014 Zeke Sonxx <github.com/zekesonxx> | |
# License: MIT - choosealicense.com/licenses/mit/ | |
# ## Features ### | |
# - Rewrites messages from the bots to look nicer. | |
# Before: TNABot|[G] [DaemonLee]: Why am I saying things? | |
# After: [S] DaemonLee|Why am I saying things? | |
# Before: TNABot|[Server] Now saving the map. | |
# After: [S] [Server]|Now saving the map. | |
# | |
# - Adds the /players command | |
# Lists players currently on the MC server. | |
# Based on https://github.com/TingPing/plugins/blob/master/HexChat/twitch.py | |
__module_name__ = 'Escapecraft' | |
__module_author__ = 'Zeke Sonxx' | |
__module_version__ = '0.1.1' | |
__module_description__ = 'Better looks on the Escapecraft IRC channel' | |
def ecOnly(func): | |
"""Only execute in the #mine channel""" | |
def if_ec(*args, **kwargs): | |
channel = hexchat.get_info('channel') | |
if channel and (channel == '#mine'): | |
return func(*args, **kwargs) | |
else: | |
return hexchat.EAT_NONE | |
return if_ec | |
@ecOnly | |
def players_cb(word, word_eol, userdata): | |
"""Function used to list connected players""" | |
conn = httplib.HTTPConnection("www.escapecraft.net", 80) | |
conn.connect() | |
conn.request("GET", "/status.php?s=mc") | |
res = conn.getresponse() | |
data = json.loads(res.read()) | |
conn.close() | |
if data['players'] is 1: | |
# One person online | |
hexchat.prnt(data['playerlist'][0]+" is the only one online") | |
elif data['players'] is 2: | |
# Two people online | |
hexchat.prnt(data['playerlist'][0] + " and " + data['playerlist'][1] | |
+ " sitting in a tree") | |
pass | |
elif data['players'] > 0: | |
# Multiple people online | |
playerlist = ", ".join(data['playerlist']) | |
count = str(data['players']) | |
hexchat.prnt("" + count + " players online: " + playerlist) | |
else: | |
# No people online | |
hexchat.prnt("No players connected") | |
return hexchat.EAT_ALL | |
def prefix(name): | |
if hexchat.nickcmp(name, "TNABot") is 0: | |
return "[S]" | |
elif hexchat.nickcmp(name, "CreativeBot") is 0: | |
return "[C]" | |
elif hexchat.nickcmp(name, "EnigmaBot") is 0: | |
return "[E]" | |
else: | |
return None | |
@ecOnly | |
def msg_cb(word, word_eol, userdata): | |
bot = hexchat.strip(word[0]) | |
pref = prefix(bot) | |
if pref is not None: | |
words = word[1].split() | |
if words[0] == "[Server]": | |
# Server Message | |
msg = word[1][9:] | |
hexchat.emit_print("Channel Message", pref+" [Server]", msg) | |
else: | |
# Player Message | |
player = words[1][1:-2] | |
# "[G]", the username + "[]:", and two spaces | |
offset = len(words[0]) + len(words[1]) + 2 | |
msg = word[1][offset:] | |
hexchat.emit_print("Channel Message", pref+" "+player, msg) | |
return hexchat.EAT_HEXCHAT | |
else: | |
return hexchat.EAT_NONE | |
hexchat.hook_command("players", players_cb, | |
help="/players Lists connected players on the Escapecraft server") # NOQA | |
hexchat.hook_print("Channel Message", msg_cb) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment