Skip to content
Snippets Groups Projects
karma.py 2.23 KiB
Newer Older
Bruce Cowan's avatar
Bruce Cowan committed
# Copyright © 2016, 2018 Bruce Cowan <bruce@bcowan.eu>
Bruce Cowan's avatar
Bruce Cowan committed
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

Bruce Cowan's avatar
Bruce Cowan committed
from collections import defaultdict
Bruce Cowan's avatar
Bruce Cowan committed
import re
Bruce Cowan's avatar
Bruce Cowan committed

from errbot import BotPlugin, botcmd


class Karma(BotPlugin):
    """Karma service"""

    def activate(self):
        super().activate()

Bruce Cowan's avatar
Bruce Cowan committed
        self.karma = self.get('karma', defaultdict(int))
Bruce Cowan's avatar
Bruce Cowan committed
        self.inc = re.compile(r'(.+)\+\+')
        self.dec = re.compile(r'(.+)--')

Bruce Cowan's avatar
Bruce Cowan committed
    def deactivate(self):
        self['karma'] = self.karma
Bruce Cowan's avatar
Bruce Cowan committed

Bruce Cowan's avatar
Bruce Cowan committed
        super().deactivate()
Bruce Cowan's avatar
Bruce Cowan committed

Bruce Cowan's avatar
Bruce Cowan committed
    def _change_karma(self, target, value, message):
        self.karma[target] += value
Bruce Cowan's avatar
Bruce Cowan committed
        text = "Karma for '{}' is now {}".format(target, self.karma[target])
        self.send(message.to, text, message, True)

    def callback_message(self, message):
        # Increment?
        match = self.inc.search(message.body)
        if match:
Bruce Cowan's avatar
Bruce Cowan committed
            self._change_karma(match.group(1), 1, message)
Bruce Cowan's avatar
Bruce Cowan committed

        # Decrement?
        match = self.dec.search(message.body)
        if match:
Bruce Cowan's avatar
Bruce Cowan committed
            self._change_karma(match.group(1), -1, message)
Bruce Cowan's avatar
Bruce Cowan committed
    @botcmd
    def karma_add(self, message, args):
        """Adds karma"""
        if not args:
            return "**Usage**: !karma add <target>"

Bruce Cowan's avatar
Bruce Cowan committed
        self._change_karma(args, 1, message)
Bruce Cowan's avatar
Bruce Cowan committed

    @botcmd
    def karma_remove(self, message, args):
        """Removes karma"""
        if not args:
            return "**Usage**: !karma remove <target>"

Bruce Cowan's avatar
Bruce Cowan committed
        self._change_karma(args, -1, message)
Bruce Cowan's avatar
Bruce Cowan committed

    @botcmd
    def karma(self, message, args):
        """Gets karma"""
        if not args:
            return "**Usage**: !karma <target>"

        return "Karma for '{}' is {}".format(args, self.karma[args])