grant karma based on mndp requests #12

This commit is contained in:
Morgan McMillian 2019-02-17 10:08:01 -08:00
parent ce99d7416c
commit 6092607207
2 changed files with 57 additions and 5 deletions

View file

@ -23,3 +23,9 @@ class Preferences(Base):
id = Column(Integer, primary_key=True)
userid = Column(Integer, unique=True)
chimpnut = Column(Boolean)
class MdnpRequests(Base):
__tablename__ = 'requests'
id = Column(Integer, primary_key=True)
userid = Column(Integer, unique=True)
requests = Column(Integer)

View file

@ -11,7 +11,7 @@ import re
from database import db_session, init_db
from sqlalchemy import and_
from models import Karma, Optout, Queue, Preferences
from models import Karma, Optout, Queue, Preferences, MdnpRequests
_shutdown = threading.Event()
_connected = threading.Event()
@ -35,7 +35,9 @@ def echo(room, text):
send(room, text)
def help(room):
reply = "You can upvote or downvote participants by mentioning them with the following symbols\n\n"
reply = "You can earn karma by making #MNDP requests!"
reply += " Your first request, and every 10 after earns you karma.\n\n"
reply += "You can upvote or downvote others by mentioning them with the following...\n\n"
reply += " to vote up: +1, ++, \U0001F44D \n"
reply += " to vote down: -1, --, \U0001F44E \n"
reply += "\n"
@ -77,11 +79,16 @@ def optout(msg):
karma = Karma.query.filter(Karma.userid == msg.user.id).one_or_none()
if karma:
db_session.delete(karma)
counter = MdnpRequests.query.filter(MdnpRequests.userid == msg.user.id).one_or_none()
if counter:
db_session.delete(counter)
entry = Optout.query.filter(Optout.userid == msg.user.id).one_or_none()
if entry is None:
entry = Optout(userid=msg.user.id)
db_session.add(entry)
db_session.commit()
reply = "@" + msg.user.username
reply += " you have been removed from the karma table"
send(msg.channel_id, reply)
@ -234,6 +241,42 @@ def on_mention(msg):
# TODO: use for even more magic
return
def on_mndp(msg):
tags = [e.text for e in msg.content.entities.tags]
mentions = [e.text for e in msg.content.entities.mentions]
if "NowPlaying" not in tags:
return
for m in mentions:
addkarma = False
try:
pnutuser, meta = pnutpy.api.get_user("@" + m)
optout = Optout.query.filter(Optout.userid == pnutuser.id).one_or_none()
if optout:
continue
prefs = Preferences.query.filter(Preferences.userid == pnutuser.id).one_or_none()
if prefs is None:
prefs = Preferences(userid=pnutuser.id, chimpnut=True)
db_session.add(prefs)
db_session.commit()
entry = MdnpRequests.query.filter(MdnpRequests.userid == pnutuser.id).one_or_none()
if entry is None:
entry = MdnpRequests(userid=pnutuser.id, requests=0)
db_session.add(entry)
addkarma = True
entry.requests = entry.requests + 1
db_session.commit()
if entry.requests % 10 == 0 or addkarma:
upvote(msg.channel_id, pnutuser, prefs)
except pnutpy.errors.PnutMissing:
continue
def on_message(ws, message):
logger.debug("on_message: " + message)
msg = json.loads(message)
@ -259,7 +302,10 @@ def on_message(ws, message):
if pmsg.user.username == config['USERNAME']:
return
if config['USERNAME'] in [e.text for e in pmsg.content.entities.mentions]:
if pmsg.user.username == config['MNDPUSER']:
on_mndp(pmsg)
elif config['USERNAME'] in [e.text for e in pmsg.content.entities.mentions]:
on_mention(pmsg)
elif pmsg.content.text.startswith('!'):
@ -271,12 +317,12 @@ def on_message(ws, message):
def on_error(ws, error):
logger.error("on_error: !!! ERROR !!!")
logger.error(error)
_shutdown.set()
# _shutdown.set()
def on_close(ws):
send(config['CHANNEL'], "...shutdown initiated...")
logger.debug("on_close: ### CLOSED ###")
_shutdown.set()
# _shutdown.set()
def on_open(ws):