2016-03-29 11:56:58 +00:00
|
|
|
import re, time
|
2016-04-10 16:40:58 +00:00
|
|
|
import Utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2016-04-20 23:09:30 +00:00
|
|
|
REGEX_KARMA = re.compile("(.*)(\+{2,}|\-{2,})$")
|
2016-03-29 11:56:58 +00:00
|
|
|
KARMA_DELAY_SECONDS = 3
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
def __init__(self, bot):
|
2016-04-06 11:02:44 +00:00
|
|
|
self.bot = bot
|
2016-03-29 11:56:58 +00:00
|
|
|
bot.events.on("new").on("user").hook(self.new_user)
|
|
|
|
bot.events.on("received").on("message").on("channel").hook(
|
|
|
|
self.channel_message)
|
|
|
|
bot.events.on("received").on("command").on("karma").hook(
|
2016-04-06 11:02:44 +00:00
|
|
|
self.karma, help="Get your or someone else's karma",
|
|
|
|
usage="[target]")
|
|
|
|
bot.events.on("boot").on("done").hook(self.boot_done)
|
|
|
|
|
|
|
|
def boot_done(self, event):
|
|
|
|
self.bot.events.on("postboot").on("configure").on(
|
2016-05-01 21:47:59 +00:00
|
|
|
"channelset").call(setting="karma-verbose",
|
2016-04-06 11:02:44 +00:00
|
|
|
help="Disable/Enable automatically responding to karma changes",
|
2016-04-10 16:40:58 +00:00
|
|
|
validate=Utils.bool_or_none)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def new_user(self, event):
|
|
|
|
event["user"].last_karma = None
|
|
|
|
|
|
|
|
def channel_message(self, event):
|
2016-04-20 23:09:30 +00:00
|
|
|
match = re.match(REGEX_KARMA, event["message"].strip())
|
2016-03-29 11:56:58 +00:00
|
|
|
if match:
|
2016-05-01 21:47:59 +00:00
|
|
|
verbose = event["channel"].get_setting("karma-verbose", False)
|
2016-03-29 11:56:58 +00:00
|
|
|
if not event["user"].last_karma or (time.time()-event["user"
|
|
|
|
].last_karma) >= KARMA_DELAY_SECONDS:
|
2016-03-29 20:06:07 +00:00
|
|
|
target = match.group(1).lower().strip()
|
|
|
|
if not target == event["user"].name:
|
|
|
|
positive = match.group(2)[0] == "+"
|
|
|
|
setting = "karma-%s" % target
|
|
|
|
karma = event["server"].get_setting(setting, 0)
|
|
|
|
if positive:
|
|
|
|
karma += 1
|
|
|
|
else:
|
|
|
|
karma -= 1
|
|
|
|
if karma:
|
|
|
|
event["server"].set_setting(setting, karma)
|
|
|
|
else:
|
|
|
|
event["server"].del_setting(setting)
|
2016-04-06 11:02:44 +00:00
|
|
|
if verbose:
|
|
|
|
self.bot.events.on("send").on("stdout").call(
|
|
|
|
module_name="Karma", target=event["channel"],
|
|
|
|
message="%s now has %d karma" % (target, karma))
|
2016-03-29 20:06:07 +00:00
|
|
|
event["user"].last_karma = time.time()
|
2016-04-06 11:02:44 +00:00
|
|
|
elif verbose:
|
|
|
|
self.bot.events.on("send").on("stderr").call(module_name="Karma",
|
|
|
|
target=event["channel"], message="You cannot change your own karma")
|
2016-04-20 23:09:30 +00:00
|
|
|
elif verbose:
|
|
|
|
event["stderr"].write("Try again in a couple of seconds")
|
2016-03-29 11:56:58 +00:00
|
|
|
def karma(self, event):
|
|
|
|
if event["args"]:
|
|
|
|
target = event["args"]
|
|
|
|
else:
|
|
|
|
target = event["user"].nickname
|
|
|
|
karma = event["server"].get_setting("karma-%s" % target, 0)
|
|
|
|
event["stdout"].write("%s has %s karma" % (target, karma))
|