diff --git a/partybot-matrix.py b/partybot-matrix.py index 98888e6..4a0e764 100644 --- a/partybot-matrix.py +++ b/partybot-matrix.py @@ -3,6 +3,7 @@ import yaml import time import re import redis +import datetime from matrix_bot_api.matrix_bot_api import MatrixBotAPI from matrix_bot_api.mregex_handler import MRegexHandler @@ -39,17 +40,36 @@ def karma_cb(room, event): alias = re.search('@(pnut_)?([^:]+)', mxid).group(2) if alias.lower() == handle.lower() or handle.lower() == 'partybot': - if sign == '++': - rs.incr("partybot:k_" + event['room_id'] + "@" + handle) - elif sign == '--': - rs.decr("partybot:k_" + event['room_id'] + "@" + handle) + if sender.lower() not in kpool: + kpool[sender.lower()] = {} + kpool[sender.lower()]['last'] = datetime.datetime.now() + kpool[sender.lower()]['count'] = 1 + _set_karma(room, event['room_id'], handle, sign) - _show_karma(room, event['room_id'], handle) + else: + delta = datetime.datetime.now() - kpool[sender.lower()]['last'] + print('seconds: ' + str(delta.seconds)) + if kpool[sender.lower()]['count'] < 5: + kpool[sender.lower()]['count'] += 1 + _set_karma(room, event['room_id'], handle, sign) + else: + if delta.seconds < 60: + reply = "@" + sender + ": Whoa, back off a little human." + room.send_notice(reply) + else: + kpool[sender.lower()]['count'] = 1 + kpool[sender.lower()]['last'] = datetime.datetime.now() + _set_karma(room, event['room_id'], handle, sign) - break + break -def _show_karma(room, room_id, handle): +def _set_karma(room, room_id, handle, sign): + + if sign == '++': + rs.incr("partybot:k_" + room_id + "@" + handle) + elif sign == '--': + rs.decr("partybot:k_" + room_id + "@" + handle) k = int(rs.get("partybot:k_" + room_id + "@" + handle)) reply = handle + " has " + str(k) + " karma in this channel." @@ -67,7 +87,10 @@ def recv_event_q(queue, post): if __name__ == "__main__": + kpool = {} + logging.basicConfig(level=logging.DEBUG,filename='partybot.log') + # logging.basicConfig(level=logging.DEBUG) with open("partybot-config.yaml", "rb") as config_file: config = yaml.load(config_file) @@ -79,6 +102,7 @@ if __name__ == "__main__": bot.add_handler(MCommandHandler("karma", show_karma_cb)) bot.start_polling() + while True: time.sleep(1) item = rs.rpop('MNDP')