added more limits around use of karma. issue #2
This commit is contained in:
parent
e364098010
commit
c4a60419da
1 changed files with 31 additions and 7 deletions
|
@ -3,6 +3,7 @@ import yaml
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
import redis
|
import redis
|
||||||
|
import datetime
|
||||||
|
|
||||||
from matrix_bot_api.matrix_bot_api import MatrixBotAPI
|
from matrix_bot_api.matrix_bot_api import MatrixBotAPI
|
||||||
from matrix_bot_api.mregex_handler import MRegexHandler
|
from matrix_bot_api.mregex_handler import MRegexHandler
|
||||||
|
@ -39,17 +40,36 @@ def karma_cb(room, event):
|
||||||
alias = re.search('@(pnut_)?([^:]+)', mxid).group(2)
|
alias = re.search('@(pnut_)?([^:]+)', mxid).group(2)
|
||||||
if alias.lower() == handle.lower() or handle.lower() == 'partybot':
|
if alias.lower() == handle.lower() or handle.lower() == 'partybot':
|
||||||
|
|
||||||
if sign == '++':
|
if sender.lower() not in kpool:
|
||||||
rs.incr("partybot:k_" + event['room_id'] + "@" + handle)
|
kpool[sender.lower()] = {}
|
||||||
elif sign == '--':
|
kpool[sender.lower()]['last'] = datetime.datetime.now()
|
||||||
rs.decr("partybot:k_" + event['room_id'] + "@" + handle)
|
kpool[sender.lower()]['count'] = 1
|
||||||
|
_set_karma(room, event['room_id'], handle, sign)
|
||||||
|
|
||||||
_show_karma(room, event['room_id'], handle)
|
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)
|
||||||
|
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)
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
def _show_karma(room, room_id, handle):
|
def _set_karma(room, room_id, handle, sign):
|
||||||
|
|
||||||
|
if sign == '++':
|
||||||
|
rs.incr("partybot:k_" + room_id + "@" + handle)
|
||||||
|
elif sign == '--':
|
||||||
|
rs.decr("partybot:k_" + room_id + "@" + handle)
|
||||||
|
|
||||||
k = int(rs.get("partybot:k_" + room_id + "@" + handle))
|
k = int(rs.get("partybot:k_" + room_id + "@" + handle))
|
||||||
reply = handle + " has " + str(k) + " karma in this channel."
|
reply = handle + " has " + str(k) + " karma in this channel."
|
||||||
|
@ -67,7 +87,10 @@ def recv_event_q(queue, post):
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
kpool = {}
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG,filename='partybot.log')
|
logging.basicConfig(level=logging.DEBUG,filename='partybot.log')
|
||||||
|
# logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
with open("partybot-config.yaml", "rb") as config_file:
|
with open("partybot-config.yaml", "rb") as config_file:
|
||||||
config = yaml.load(config_file)
|
config = yaml.load(config_file)
|
||||||
|
@ -79,6 +102,7 @@ if __name__ == "__main__":
|
||||||
bot.add_handler(MCommandHandler("karma", show_karma_cb))
|
bot.add_handler(MCommandHandler("karma", show_karma_cb))
|
||||||
|
|
||||||
bot.start_polling()
|
bot.start_polling()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
item = rs.rpop('MNDP')
|
item = rs.rpop('MNDP')
|
||||||
|
|
Loading…
Reference in a new issue