From 60926072079fa54e11006f3b373ccd694f228050 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sun, 17 Feb 2019 10:08:01 -0800 Subject: [PATCH] grant karma based on mndp requests #12 --- models.py | 6 ++++++ partybot-pnut.py | 56 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/models.py b/models.py index 982de96..17df8e4 100644 --- a/models.py +++ b/models.py @@ -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) diff --git a/partybot-pnut.py b/partybot-pnut.py index 808e75f..3e65fda 100644 --- a/partybot-pnut.py +++ b/partybot-pnut.py @@ -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):