partybot/partybot-matrix.py

157 lines
4.7 KiB
Python
Raw Normal View History

2017-09-14 05:56:45 +00:00
import logging
import yaml
import time
import re
import redis
import datetime
2017-09-28 05:09:37 +00:00
import random
2017-09-14 05:56:45 +00:00
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
2017-09-28 05:09:37 +00:00
def botsnack_cb(room, event):
replies = [
"Nom nom nom.",
"Ooooh",
"Yummi!",
"Delightful.",
"That makes me happy",
"How kind!",
"Sweet.",
"*burp*",
2018-04-16 23:50:39 +00:00
]
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*",
2017-09-28 05:09:37 +00:00
"Ah.. Hiccup!"
]
room.send_notice(random.choice(replies))
2018-04-16 23:50:39 +00:00
def metal_cb(room, event):
logging.debug(event)
logging.debug("\m/")
room.send_notice("\m/")
2017-09-14 05:56:45 +00:00
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'])
2017-09-14 05:56:45 +00:00
handle = tokens.group(1)
sign = tokens.group(2)
value = tokens.group(3)
2017-09-14 05:56:45 +00:00
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)
2017-09-15 21:23:02 +00:00
if alias.lower() == handle.lower() or handle.lower() == 'partybot':
2017-09-14 05:56:45 +00:00
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)
2017-09-14 05:56:45 +00:00
k = int(rs.get("partybot:k_" + room_id + "@" + handle.lower()))
2017-09-14 05:56:45 +00:00
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 = {}
2017-09-15 20:16:23 +00:00
logging.basicConfig(level=logging.DEBUG,filename='partybot.log')
# logging.basicConfig(level=logging.DEBUG)
2017-09-14 05:56:45 +00:00
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))
2017-09-14 05:56:45 +00:00
bot.add_handler(MCommandHandler("karma", show_karma_cb))
2017-09-28 05:09:37 +00:00
bot.add_handler(MCommandHandler("botsnack", botsnack_cb))
2018-08-02 23:50:33 +00:00
bot.add_handler(MRegexHandler("\s!botsnack", botsnack_cb))
2018-04-16 23:50:39 +00:00
bot.add_handler(MCommandHandler("botdrink", botdrink_cb))
2018-08-02 23:50:33 +00:00
bot.add_handler(MRegexHandler("\s!botdrink", botdrink_cb))
2017-09-14 05:56:45 +00:00
bot.start_polling()
2017-09-14 05:56:45 +00:00
while True:
time.sleep(1)
item = rs.rpop('MNDP')
if item:
2017-09-15 20:16:23 +00:00
recv_event_q('MNDP', item.decode())
2017-09-14 05:56:45 +00:00
item = rs.rpop('JUKEBOX')
if item:
2017-09-15 20:16:23 +00:00
recv_event_q('JUKEBOX', item.decode())