2016-03-29 11:56:58 +00:00
|
|
|
import re, time
|
2018-09-24 14:13:27 +00:00
|
|
|
from src import EventManager, Utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-09-09 17:39:56 +00:00
|
|
|
REGEX_KARMA = re.compile("^(.*[^-+])[-+]*(\+{2,}|\-{2,})$")
|
2016-03-29 11:56:58 +00:00
|
|
|
KARMA_DELAY_SECONDS = 3
|
|
|
|
|
|
|
|
class Module(object):
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2016-04-06 11:02:44 +00:00
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events = events
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("new.user").hook(self.new_user)
|
|
|
|
events.on("received.message.channel").hook(
|
2018-09-09 16:09:04 +00:00
|
|
|
self.channel_message, priority=EventManager.PRIORITY_MONITOR)
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("received.command.karma").hook(
|
2016-04-06 11:02:44 +00:00
|
|
|
self.karma, help="Get your or someone else's karma",
|
|
|
|
usage="[target]")
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("received.command.resetkarma").hook(
|
2018-08-18 17:28:27 +00:00
|
|
|
self.reset_karma, permission="resetkarma",
|
|
|
|
min_args=1, help="Reset a specified karma to 0",
|
|
|
|
usage="<target>")
|
2016-04-06 11:02:44 +00:00
|
|
|
|
2018-09-02 18:54:45 +00:00
|
|
|
exports.add("channelset", {"setting": "karma-verbose",
|
|
|
|
"help": "Disable/Enable automatically responding to "
|
|
|
|
"karma changes", "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-06-24 12:10:36 +00:00
|
|
|
if match and not event["action"]:
|
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()
|
2016-06-24 12:10:36 +00:00
|
|
|
if not target == event["user"].name and target:
|
2016-03-29 20:06:07 +00:00
|
|
|
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:
|
2018-09-19 12:25:12 +00:00
|
|
|
self.events.on("send.stdout").call(
|
2016-04-06 11:02:44 +00:00
|
|
|
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:
|
2016-06-24 12:10:36 +00:00
|
|
|
if target:
|
2018-09-09 16:34:44 +00:00
|
|
|
self.events.on("send.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:
|
2018-09-09 16:34:44 +00:00
|
|
|
self.events.on("send.stderr").call(module_name="Karma",
|
|
|
|
target=event["channel"],
|
|
|
|
message="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))
|
2018-08-18 17:28:27 +00:00
|
|
|
|
|
|
|
def reset_karma(self, event):
|
|
|
|
setting = "karma-%s" % event["args_split"][0]
|
|
|
|
karma = event["server"].get_setting(setting, 0)
|
|
|
|
if karma == 0:
|
|
|
|
event["stderr"].write("%s already has 0 karma" % event[
|
|
|
|
"args_split"][0])
|
|
|
|
else:
|
|
|
|
event["server"].del_setting(setting)
|
|
|
|
event["stdout"].write("Reset karma for %s" % event[
|
|
|
|
"args_split"][0])
|