parent
91a456d7e1
commit
0600473f06
1 changed files with 12 additions and 7 deletions
|
@ -60,9 +60,10 @@ def karma_cb(room, event):
|
||||||
members = room.get_joined_members()
|
members = room.get_joined_members()
|
||||||
msender = re.search('@([\w]+_)?([^:]+)', event['sender'])
|
msender = re.search('@([\w]+_)?([^:]+)', event['sender'])
|
||||||
sender = msender.group(2)
|
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)
|
handle = tokens.group(1)
|
||||||
sign = tokens.group(2)
|
sign = tokens.group(2)
|
||||||
|
value = tokens.group(3)
|
||||||
reply = ""
|
reply = ""
|
||||||
|
|
||||||
if sender.lower() == handle.lower():
|
if sender.lower() == handle.lower():
|
||||||
|
@ -78,14 +79,14 @@ def karma_cb(room, event):
|
||||||
kpool[sender.lower()] = {}
|
kpool[sender.lower()] = {}
|
||||||
kpool[sender.lower()]['last'] = datetime.datetime.now()
|
kpool[sender.lower()]['last'] = datetime.datetime.now()
|
||||||
kpool[sender.lower()]['count'] = 1
|
kpool[sender.lower()]['count'] = 1
|
||||||
_set_karma(room, event['room_id'], handle, sign)
|
_set_karma(room, event['room_id'], handle, sign, value)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
delta = datetime.datetime.now() - kpool[sender.lower()]['last']
|
delta = datetime.datetime.now() - kpool[sender.lower()]['last']
|
||||||
print('seconds: ' + str(delta.seconds))
|
print('seconds: ' + str(delta.seconds))
|
||||||
if kpool[sender.lower()]['count'] < 5:
|
if kpool[sender.lower()]['count'] < 5:
|
||||||
kpool[sender.lower()]['count'] += 1
|
kpool[sender.lower()]['count'] += 1
|
||||||
_set_karma(room, event['room_id'], handle, sign)
|
_set_karma(room, event['room_id'], handle, sign, value)
|
||||||
else:
|
else:
|
||||||
if delta.seconds < 60:
|
if delta.seconds < 60:
|
||||||
reply = "@" + sender + ": Whoa, back off a little human."
|
reply = "@" + sender + ": Whoa, back off a little human."
|
||||||
|
@ -93,17 +94,21 @@ def karma_cb(room, event):
|
||||||
else:
|
else:
|
||||||
kpool[sender.lower()]['count'] = 1
|
kpool[sender.lower()]['count'] = 1
|
||||||
kpool[sender.lower()]['last'] = datetime.datetime.now()
|
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
|
break
|
||||||
|
|
||||||
|
|
||||||
def _set_karma(room, room_id, handle, sign):
|
def _set_karma(room, room_id, handle, sign, value=1):
|
||||||
|
|
||||||
if sign == '++':
|
if sign == '++':
|
||||||
rs.incr("partybot:k_" + room_id + "@" + handle.lower())
|
rs.incr("partybot:k_" + room_id + "@" + handle.lower())
|
||||||
elif sign == '--':
|
elif sign == '--':
|
||||||
rs.decr("partybot:k_" + room_id + "@" + handle.lower())
|
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()))
|
k = int(rs.get("partybot:k_" + room_id + "@" + handle.lower()))
|
||||||
reply = handle + " has " + str(k) + " karma in this channel."
|
reply = handle + " has " + str(k) + " karma in this channel."
|
||||||
|
@ -132,11 +137,11 @@ if __name__ == "__main__":
|
||||||
rs = redis.StrictRedis()
|
rs = redis.StrictRedis()
|
||||||
bot = MatrixBotAPI(config['username'], config['password'], config['url'])
|
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("karma", show_karma_cb))
|
||||||
bot.add_handler(MCommandHandler("botsnack", botsnack_cb))
|
bot.add_handler(MCommandHandler("botsnack", botsnack_cb))
|
||||||
bot.add_handler(MCommandHandler("botdrink", botdrink_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()
|
bot.start_polling()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue