added more ways to assign karma resolves issue #6 and part of #9

This commit is contained in:
Morgan McMillian 2018-08-02 16:42:04 -07:00
parent 91a456d7e1
commit 0600473f06

View file

@ -60,9 +60,10 @@ 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?(\-\-|\+\+)", event['content']['body'])
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():
@ -78,14 +79,14 @@ def karma_cb(room, event):
kpool[sender.lower()] = {}
kpool[sender.lower()]['last'] = datetime.datetime.now()
kpool[sender.lower()]['count'] = 1
_set_karma(room, event['room_id'], handle, sign)
_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)
_set_karma(room, event['room_id'], handle, sign, value)
else:
if delta.seconds < 60:
reply = "@" + sender + ": Whoa, back off a little human."
@ -93,17 +94,21 @@ def karma_cb(room, event):
else:
kpool[sender.lower()]['count'] = 1
kpool[sender.lower()]['last'] = datetime.datetime.now()
_set_karma(room, event['room_id'], handle, sign)
_set_karma(room, event['room_id'], handle, sign, value)
break
def _set_karma(room, room_id, handle, sign):
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."
@ -132,11 +137,11 @@ if __name__ == "__main__":
rs = redis.StrictRedis()
bot = MatrixBotAPI(config['username'], config['password'], config['url'])
bot.add_handler(MRegexHandler("([\w]+)\s?(\-\-|\+\+)", karma_cb))
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(MCommandHandler("botdrink", botdrink_cb))
bot.add_handler(MRegexHandler("\s?\\m\/\s?", metal_cb))
# bot.add_handler(MRegexHandler("\s?\\m\/\s?", metal_cb)) # Regex is hard or something
bot.start_polling()