import logging import yaml import time import re import redis import datetime import random from matrix_bot_api.matrix_bot_api import MatrixBotAPI from matrix_bot_api.mregex_handler import MRegexHandler from matrix_bot_api.mcommand_handler import MCommandHandler def help_cb(room, event): reply = "" reply += "`!karma` shows the karma assigned to room members\n" reply += "`!botsnack` or `!botdrink` to give partybot some refreshment\n" reply += "`@username ++` or `@username --` to assign karma to a room member\n" room.send_notice(reply) def botsnack_cb(room, event): replies = [ "Nom nom nom.", "Ooooh", "Yummi!", "Delightful.", "That makes me happy", "How kind!", "Sweet.", "*burp*", ] room.send_notice(random.choice(replies)) def botdrink_cb(room, event): replies = [ "Hold my beer.", "Ooooh", "Delightful.", "That makes me happy", "How kind!", "Sweet.", "*burp*", "Ah.. Hiccup!" ] room.send_notice(random.choice(replies)) def metal_cb(room, event): logging.debug(event) logging.debug("\m/") room.send_notice("\m/") def show_karma_cb(room, event): allkkeys = rs.keys("partybot:k_" + event['room_id'] + "@*") reply = "" for k in allkkeys: handle = k.decode().split('@')[1] karma = rs.get(k) reply += handle + " has " + str(karma.decode()) + " karma.\n" room.send_notice(reply) def karma_cb(room, event): members = room.get_joined_members() msender = re.search('@([\w]+_)?([^:]+)', event['sender']) sender = msender.group(2) tokens = re.search("([\w]+)\s?(\-\-|\+\+|\+=|\-=)\s?([1-9])?", event['content']['body']) handle = tokens.group(1) sign = tokens.group(2) value = tokens.group(3) reply = "" if sender.lower() == handle.lower(): reply = "@" + sender + ": Silly human, your karma must be decided by others!" room.send_notice(reply) return for mxid in members: alias = re.search('@(pnut_)?([^:]+)', mxid).group(2) if alias.lower() == handle.lower() or handle.lower() == 'partybot': 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, value) 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, value) 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, value) break def _set_karma(room, room_id, handle, sign, value=1): if sign == '++': rs.incr("partybot:k_" + room_id + "@" + handle.lower()) elif sign == '--': rs.decr("partybot:k_" + room_id + "@" + handle.lower()) elif sign == '+=': rs.incrby("partybot:k_" + room_id + "@" + handle.lower(), value) elif sign == '-=': rs.decr("partybot:k_" + room_id + "@" + handle.lower(), value) k = int(rs.get("partybot:k_" + room_id + "@" + handle.lower())) reply = handle + " has " + str(k) + " karma in this channel." room.send_notice(reply) def recv_event_q(queue, post): if queue == "MNDP": room = bot.client.rooms[config['queue']['MNDP']] room.send_notice(post) elif queue == "JUKEBOX": room = bot.client.rooms[config['queue']['JUKEBOX']] room.send_notice(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) rs = redis.StrictRedis() bot = MatrixBotAPI(config['username'], config['password'], config['url']) bot.add_handler(MRegexHandler("([\w]+)\s?(\-\-|\+\+|\+=|\-=)", karma_cb)) bot.add_handler(MCommandHandler("karma", show_karma_cb)) bot.add_handler(MCommandHandler("botsnack", botsnack_cb)) bot.add_handler(MRegexHandler("\s!botsnack", botsnack_cb)) bot.add_handler(MCommandHandler("botdrink", botdrink_cb)) bot.add_handler(MRegexHandler("\s!botdrink", botdrink_cb)) bot.add_handler(MCommandHandler("help", help_cb)) bot.start_polling() while True: time.sleep(1) item = rs.rpop('MNDP') if item: recv_event_q('MNDP', item.decode()) item = rs.rpop('JUKEBOX') if item: recv_event_q('JUKEBOX', item.decode())