minerbot2/plugins/opinion.py

40 lines
1.1 KiB
Python

import plugin, dictdata, hashlib
from bot import IRCLine
BOT = None
sha256 = lambda s: hashlib.sha256(s.encode("utf-8")).hexdigest()
fixed_opinions = dictdata.DictData("fixed_opinions.json")
BOLD = b"\x02".decode("ascii")
RESET = b"\x0f".decode("ascii")
OPINIONS = ["suck","neat","cool","bad"]
def suckify(s):
return BOLD+" ".join(list(s))+RESET
def chunkify(o,s):
ret = []
for i in range(0,len(o),s):
ret.append(o[i:i+s])
return ret
def get_opinion(s):
if s in fixed_opinions: return fixed_opinions[s]
hash = chunkify(sha256(s),2)
h = int(hash[len(s)%len(hash)],16)
return OPINIONS[h%len(OPINIONS)]
def opinion(event):
if not BOT: return None
channel = event.target if event.target.startswith("#") else event.hostmask.nick
nick = event.hostmask.nick
args = event.parts
if not args: return
BOT.socket.send(IRCLine("PRIVMSG",channel,":"+("{}: ".format(nick) if channel!=nick else " ".strip())+"{} {}".format(" ".join(args),suckify(get_opinion(" ".join(args))))))
def register(bot):
global BOT
BOT = bot
bot.event_manager.on("command_opinion",opinion)