Implement karma-nickname-only, to only allow karma for users in the current

channel
This commit is contained in:
jesopo 2018-10-02 14:29:32 +01:00
parent a7563c9653
commit 64733eb37f

View file

@ -5,7 +5,10 @@ REGEX_KARMA = re.compile("^(.*[^-+])[-+]*(\+{2,}|\-{2,})$")
KARMA_DELAY_SECONDS = 3 KARMA_DELAY_SECONDS = 3
@Utils.export("channelset", {"setting": "karma-verbose", @Utils.export("channelset", {"setting": "karma-verbose",
"help": "Disable/Enable automatically responding to karma changes", "help": "Enable/disable automatically responding to karma changes",
"validate": Utils.bool_or_none})
@Utils.export("serverset", {"setting": "karma-nickname-only",
"help": "Enable/disable karma being for nicknames only",
"validate": Utils.bool_or_none}) "validate": Utils.bool_or_none})
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@Utils.hook("new.user") @Utils.hook("new.user")
@ -18,31 +21,46 @@ class Module(ModuleManager.BaseModule):
match = re.match(REGEX_KARMA, event["message"].strip()) match = re.match(REGEX_KARMA, event["message"].strip())
if match and not event["action"]: if match and not event["action"]:
verbose = event["channel"].get_setting("karma-verbose", False) verbose = event["channel"].get_setting("karma-verbose", False)
nickname_only = event["server"].get_setting("karma-nickname-only",
False)
if not event["user"].last_karma or (time.time()-event["user" if not event["user"].last_karma or (time.time()-event["user"
].last_karma) >= KARMA_DELAY_SECONDS: ].last_karma) >= KARMA_DELAY_SECONDS:
target = match.group(1).lower().strip() target = match.group(1).strip()
if not target == event["user"].name and target: if target == event["user"].name:
positive = match.group(2)[0] == "+" if verbose:
self.events.on("send.stderr").call(
module_name="Karma", target=event["channel"],
message="You cannot change your own karma")
return
setting = "karma-%s" % target setting = "karma-%s" % target
setting_target = event["server"]
if nickname_only:
user = event["server"].get_user(target)
setting = target
setting_target = user
if not event["channel"].has_user(user):
if verbose:
self.events.on("send.stderr").call(
module_name="Karma", target=event["channel"],
message="User not found")
return
positive = match.group(2)[0] == "+"
karma = event["server"].get_setting(setting, 0) karma = event["server"].get_setting(setting, 0)
if positive: karma += 1 if positive else -1
karma += 1
else:
karma -= 1
if karma: if karma:
event["server"].set_setting(setting, karma) event["server"].set_setting(setting, karma)
else: else:
event["server"].del_setting(setting) event["server"].del_setting(setting)
if verbose: if verbose:
self.events.on("send.stdout").call( self.events.on("send.stdout").call(
module_name="Karma", target=event["channel"], module_name="Karma", target=event["channel"],
message="%s now has %d karma" % (target, karma)) message="%s now has %d karma" % (target, karma))
event["user"].last_karma = time.time() event["user"].last_karma = time.time()
elif verbose:
if target:
self.events.on("send.stderr").call(
module_name="Karma", target=event["channel"],
message="You cannot change your own karma")
elif verbose: elif verbose:
self.events.on("send.stderr").call(module_name="Karma", self.events.on("send.stderr").call(module_name="Karma",
target=event["channel"], target=event["channel"],
@ -58,6 +76,10 @@ class Module(ModuleManager.BaseModule):
target = event["args"] target = event["args"]
else: else:
target = event["user"].nickname target = event["user"].nickname
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 = event["server"].get_setting("karma-%s" % target, 0)
event["stdout"].write("%s has %s karma" % (target, karma)) event["stdout"].write("%s has %s karma" % (target, karma))