bitbot-3.11-fork/modules/karma.py

141 lines
5.1 KiB
Python
Raw Normal View History

#--depends-on commands
#--depends-on config
#--depends-on permissions
2016-03-29 11:56:58 +00:00
import re, time
from src import EventManager, ModuleManager, utils
2016-03-29 11:56:58 +00:00
WORD_STOP = [",", ":"]
2016-03-29 11:56:58 +00:00
KARMA_DELAY_SECONDS = 3
2019-09-27 13:32:09 +00:00
REGEX_KARMA = re.compile(r'(?:\(([^)]+)\)|(\S+))(\+\+|--)(\s+|$)')
@utils.export("channelset", utils.BoolSetting("karma-pattern",
"Enable/disable parsing ++/-- karma format"))
@utils.export("serverset", utils.BoolSetting("karma-nickname-only",
"Enable/disable karma being for nicknames only"))
class Module(ModuleManager.BaseModule):
def _karma_str(self, karma):
karma_str = str(karma)
if karma < 0:
return utils.irc.color(str(karma), utils.consts.RED)
elif karma > 0:
return utils.irc.color(str(karma), utils.consts.LIGHTGREEN)
return str(karma)
@utils.hook("new.user")
2016-03-29 11:56:58 +00:00
def new_user(self, event):
event["user"]._last_positive_karma = None
event["user"]._last_negative_karma = None
def _check_throttle(self, user, positive):
timestamp = None
if positive:
timestamp = user._last_positive_karma
else:
timestamp = user._last_negative_karma
return timestamp == None or (time.time()-timestamp
) >= KARMA_DELAY_SECONDS
def _set_throttle(self, user, positive):
if positive:
user._last_positive_karma = time.time()
else:
user._last_negative_karma = time.time()
2016-03-29 11:56:58 +00:00
def _karma(self, server, sender, target, positive):
if self._check_throttle(sender, positive):
nickname_only = server.get_setting("karma-nickname-only", False)
2019-05-21 15:48:10 +00:00
if server.irc_lower(target) == sender.name:
return False, "You cannot change your own karma"
setting = "karma-%s" % target
setting_target = server
if nickname_only:
user = server.get_user(target, create=False)
if user == None:
return False, "No such user"
setting = "karma"
setting_target = user
karma = setting_target.get_setting(setting, 0)
karma += 1 if positive else -1
if not karma == 0:
setting_target.set_setting(setting, karma)
else:
setting_target.del_setting(setting)
karma_str = self._karma_str(karma)
self._set_throttle(sender, positive)
return True, "%s now has %s karma" % (target, karma_str)
else:
return False, "Try again in a couple of seconds"
@utils.hook("command.regex")
@utils.kwarg("command", "karma")
@utils.kwarg("pattern", REGEX_KARMA)
def channel_message(self, event):
pattern = event["target"].get_setting("karma-pattern", False)
if not pattern:
return
positive = event["match"].group(3) == "++" # if match group 3 is ++, add karma
# There are two possible match groups, an arbitrary length text inside (), or a single word followed by ++/--
# group 1 is the former, group 2 is the latter
2019-09-27 13:32:09 +00:00
target = event["match"].group(1) or event["match"].group(2)
target = target.strip().rstrip("".join(WORD_STOP)) # Strips "," " " or ":" from target
2019-09-27 13:32:09 +00:00
# if we have a target...
if target:
success, message = self._karma(event["server"], event["user"],
target, positive)
event["stdout" if success else "stderr"].write(message)
@utils.hook("received.command.addpoint")
@utils.hook("received.command.rmpoint")
@utils.kwarg("min_args", 1)
@utils.kwarg("usage", "<target>")
def changepoint(self, event):
positive = event["command"] == "addpoint"
success, message = self._karma(event["server"], event["user"],
event["args"].strip(), positive)
event["stdout" if success else "stderr"].write(message)
@utils.hook("received.command.karma")
2016-03-29 11:56:58 +00:00
def karma(self, event):
"""
:help: Get your or someone else's karma
:usage: [target]
"""
2016-03-29 11:56:58 +00:00
if event["args"]:
target = event["args"]
else:
target = event["user"].nickname
2018-10-02 14:14:42 +00:00
target = target.strip()
if event["server"].get_setting("karma-nickname-only", False):
karma = event["server"].get_user(target).get_setting("karma", 0)
else:
karma = event["server"].get_setting("karma-%s" % target, 0)
karma_str = self._karma_str(karma)
event["stdout"].write("%s has %s karma" % (target, karma_str))
2018-08-18 17:28:27 +00:00
@utils.hook("received.command.resetkarma", min_args=1)
2018-08-18 17:28:27 +00:00
def reset_karma(self, event):
"""
:help: Reset a specified karma to 0
:usage: <target>
2019-09-12 20:57:40 +00:00
:permission: resetkarma
"""
2018-08-18 17:28:27 +00:00
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])