From f70e46a19899052139e9d5a5c2373aafc425e39e Mon Sep 17 00:00:00 2001 From: Noah Pederson Date: Thu, 26 Sep 2019 05:50:10 -0500 Subject: [PATCH 1/2] Improves sentence processing for karma Changes the matching regex to grab arbitrary text between () for giving (or taking away) karma. Still supports single words followed by ++/--, including usernames, and strips "," or ":" characters from the end if a single-word match. ex. (some sentence to give karma to)++ -> "[Karma] some sentence to give karma to now has 1 karma" ex. ngp:++ -> "[Karma] ngp now has 200 karma" --- modules/karma.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/modules/karma.py b/modules/karma.py index c9dfac88..528613cf 100644 --- a/modules/karma.py +++ b/modules/karma.py @@ -8,7 +8,7 @@ from src import EventManager, ModuleManager, utils WORD_STOP = [",", ":"] KARMA_DELAY_SECONDS = 3 -REGEX_KARMA = re.compile(r"^(?:(\S+:) )?(.*)(\+{2}|\-{2})$") +REGEX_KARMA = re.compile(r'(?:\((.+?)\)|(\S+))(\+\+|--)(\s+|$)') @utils.export("channelset", utils.BoolSetting("karma-pattern", "Enable/disable parsing ++/-- karma format")) @@ -78,20 +78,20 @@ class Module(ModuleManager.BaseModule): @utils.kwarg("pattern", REGEX_KARMA) def channel_message(self, event): pattern = event["target"].get_setting("karma-pattern", False) - if pattern: - positive = event["match"].group(3)[0] == "+" + if not pattern: + return + positive = event["match"].group(3) == "++" # if match group 3 is ++, add karma - target = event["match"].group(2).strip().rstrip("".join(WORD_STOP)) - if event["match"].group(1): - if not target: - target = event["match"].group(1)[:-1] - elif not event["server"].has_user(event["match"].group(1)[:-1]): - target = "%s %s" % (event["match"].group(1), target) - - if target: - success, message = self._karma(event["server"], event["user"], - target, positive) - event["stdout" if success else "stderr"].write(message) + # 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 + target = event["match"].group(1) if event["match"].group(1) else event["match"].group(2) + target = target.strip().rstrip("".join(WORD_STOP)) # Strips "," " " or ":" from target + + # 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") From c3f95130a38017a4a3e4fa568be8209026b1286a Mon Sep 17 00:00:00 2001 From: Noah Pederson Date: Fri, 27 Sep 2019 07:32:09 -0600 Subject: [PATCH 2/2] Some minor cleanup --- modules/karma.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/karma.py b/modules/karma.py index 528613cf..bcb07db6 100644 --- a/modules/karma.py +++ b/modules/karma.py @@ -8,7 +8,7 @@ from src import EventManager, ModuleManager, utils WORD_STOP = [",", ":"] KARMA_DELAY_SECONDS = 3 -REGEX_KARMA = re.compile(r'(?:\((.+?)\)|(\S+))(\+\+|--)(\s+|$)') +REGEX_KARMA = re.compile(r'(?:\(([^)]+)\)|(\S+))(\+\+|--)(\s+|$)') @utils.export("channelset", utils.BoolSetting("karma-pattern", "Enable/disable parsing ++/-- karma format")) @@ -84,9 +84,9 @@ class Module(ModuleManager.BaseModule): # 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 - target = event["match"].group(1) if event["match"].group(1) else event["match"].group(2) + target = event["match"].group(1) or event["match"].group(2) target = target.strip().rstrip("".join(WORD_STOP)) # Strips "," " " or ":" from target - + # if we have a target... if target: success, message = self._karma(event["server"], event["user"],