intial partybot rewrite
This commit is contained in:
parent
bc4e9959c5
commit
4951335093
2 changed files with 88 additions and 1 deletions
|
@ -1,3 +1,3 @@
|
|||
# partybot
|
||||
|
||||
Because bots like to party to. A bot for #MNDP
|
||||
Because bots like to party. A bot for #MNDP
|
87
partybot-matrix.py
Normal file
87
partybot-matrix.py
Normal file
|
@ -0,0 +1,87 @@
|
|||
import logging
|
||||
import yaml
|
||||
import time
|
||||
import re
|
||||
import redis
|
||||
|
||||
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 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]+)(\-\-|\+\+)", event['content']['body'])
|
||||
handle = tokens.group(1)
|
||||
sign = tokens.group(2)
|
||||
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('@([\w]+_)?([^:]+)', mxid).group(2)
|
||||
if alias.lower() == handle.lower():
|
||||
|
||||
if sign == '++':
|
||||
rs.incr("partybot:k_" + event['room_id'] + "@" + handle)
|
||||
elif sign == '--':
|
||||
rs.decr("partybot:k_" + event['room_id'] + "@" + handle)
|
||||
|
||||
_show_karma(room, event['room_id'], handle)
|
||||
|
||||
|
||||
def _show_karma(room, room_id, handle):
|
||||
|
||||
k = int(rs.get("partybot:k_" + room_id + "@" + handle))
|
||||
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__":
|
||||
|
||||
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]+)(\-\-|\+\+)", karma_cb))
|
||||
bot.add_handler(MCommandHandler("karma", show_karma_cb))
|
||||
|
||||
bot.start_polling()
|
||||
while True:
|
||||
time.sleep(1)
|
||||
item = rs.rpop('MNDP')
|
||||
if item:
|
||||
bot.recv_event_q('MNDP', item.decode())
|
||||
item = rs.rpop('JUKEBOX')
|
||||
if item:
|
||||
bot.recv_event_q('JUKEBOX', item.decode())
|
Loading…
Reference in a new issue