~thrrgilag/partybot

ref: 84e96df9458c2571ee27628ecd1ccade39a23ca5 partybot/partybot-pnut.py -rw-r--r-- 11.4 KiB
84e96df9Morgan McMillian test of migration 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import yaml
import requests
import pnutpy
import websocket
import threading
import logging
import time
import json
import random
import re
import argparse

# from database import db_session, init_db
from sqlalchemy import create_engine, and_
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from models import Base, Karma, Optout, Queue, Preferences, MdnpRequests

_startup = threading.Event()
_shutdown = threading.Event()
_connected = threading.Event()
_error = threading.Event()

def subscribe(connection_id):
    url = f"https://api.pnut.io/v0/channels/{config['CHANNEL']}/messages"
    url += "?connection_id=" + connection_id
    headers = {'Authorization': "Bearer " + config['ACCESS_TOKEN']}
    r = requests.get(url, headers=headers)
    if r.status_code == 200:
        _connected.set()
        if _startup.isSet():
            send(config['CHANNEL'], "Partybot online and ready! \o/")
            _startup.clear()
    else:
        logger.error(r)

def send(room, text):
    pnutpy.api.create_message(room, data={'text': text})

def echo(room, text):
    logger.debug(text)
    send(room, text)

def help(room):
    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"
    reply += "You can use the following commands\n\n"
    reply += "!karma -- show the current karma ratings\n"
    reply += "!optout -- remove yourself from karma ratings\n"
    reply += "!optin -- add yourself to karma ratings\n"
    reply += "!chimpnut -- toggle special ChimPnut alerts\n"
    reply += "!botsnack -- give @partybot a special treat\n"
    send(room, reply)

def botsnack(room):
    replies = [
        "Nom nom nom.",
        "Ooooh",
        "Yummi!",
        "Delightful.",
        "That makes me happy",
        "How kind!",
        "Sweet.",
        "*burp*",
    ]
    send(room, random.choice(replies))

def botdrink(room):
    replies = [
        "Hold my beer.",
        "Ooooh",
        "Delightful.",
        "That makes me happy",
        "How kind!",
        "Sweet.",
        "*burp*",
        "Ah.. Hiccup!"
    ]
    send(room, random.choice(replies))

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)

def optin(msg):
    entry = Optout.query.filter(Optout.userid == msg.user.id).one_or_none()
    if entry:
        db_session.delete(entry)
    reply = "@" + msg.user.username
    reply += " you are able to earn karma"
    send(msg.channel_id, reply)

def upvote(room, user, prefs):
    karma = Karma.query.filter(Karma.userid == user.id).one_or_none()
    if karma is None:
        karma = Karma(userid=user.id, chanid=room, karma=1)
        db_session.add(karma)
    else:
        karma.karma = karma.karma + 1
    db_session.commit()
    if prefs.chimpnut:
        prefix = "/karma "
    else:
        prefix = ""
    reply = prefix + "@" + user.username
    reply += " now has " + str(karma.karma) + " karma in this channel"
    send(room, reply)

def downvote(room, user, prefs):
    karma = Karma.query.filter(Karma.userid == user.id).one_or_none()
    if karma is None:
        karma = Karma(userid=user.id, chanid=room, karma=-1)
        db_session.add(karma)
    else:
        karma.karma = karma.karma - 1
    db_session.commit()
    if prefs.chimpnut:
        prefix = "/karma "
    else:
        prefix = ""
    reply = prefix + "@" + user.username
    reply += " now has " + str(karma.karma) + " karma in this channel"
    send(room, reply)

def karma(room):
    reply = "Karma standings\n\n"
    results = Karma.query.filter(Karma.chanid == room).order_by(Karma.karma.desc()).all()
    for entry in results:
        user, meta = pnutpy.api.get_user(entry.userid)
        reply += user.username + ": " + str(entry.karma) + "\n"
    send(room, reply)

def chimpnut(msg):
    prefs = Preferences.query.filter(Preferences.userid == msg.user.id).one_or_none()
    if prefs is None:
        prefs = Preferences(userid=msg.user.id, chimpnut=False)
        db_session.add(prefs)
    
    if prefs.chimpnut:
        prefs.chimpnut = False
        reply = "@" + msg.user.username + " ChimPnut alert is now disabled"
    else:
        prefs.chimpnut = True
        reply = "/Mac @" + msg.user.username + " ChimPnut alert is now enabled"
    db_session.commit()
    send(msg.channel_id, reply)

def on_command(msg):
    room = msg.channel_id
    args = msg.content.text.split(' ', 1)

    if args[0] == "!help":
        help(msg.channel_id)

    elif args[0] == "!botsnack":
        botsnack(msg.channel_id)
    
    elif args[0] == "!botdrink":
        botdrink(msg.channel_id)
    
    elif args[0] == "!optout":
        optout(msg)
    
    elif args[0] == "!optin":
        optin(msg)
    
    elif args[0] == "!chimpnut":
        chimpnut(msg)

    elif args[0] == "!karma":
        karma(msg.channel_id)

def on_vote(msg, matcher):

    canidate = matcher.group(1)
    vote = matcher.group(2)

    if canidate == config['MNDPUSER']:
        return

    try:
        pnutuser, meta = pnutpy.api.get_user("@" + canidate)
        if msg.user.username == pnutuser.username:
            logger.debug(pnutuser.username)
            logger.debug(canidate)
            reply = "@" + msg.user.username
            reply += " silly human, your karma must be decided by others!"
            send(msg.channel_id, reply)
            return
    
    except pnutpy.errors.PnutMissing:
        reply = "@" + msg.user.username
        reply += " I do not know who that is"
        send(msg.channel_id, reply)
        return

    optout = Optout.query.filter(Optout.userid == pnutuser.id).one_or_none()
    if optout:
        reply = "@" + msg.user.username
        reply += " user has chosen not to receive karma"
        send(msg.channel_id, reply)
        return

    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()

    logger.debug("-- VOTING " + vote)

    upvotes = [
        "++",
        ":thumbsup:",
        ":+1:",
        "+1",
        "\U0001F44D"
    ]
    downvotes = [
        "--",
        ":thumbsdown:",
        ":-1:",
        "-1",
        "\U0001F44E"
    ]
    if vote in upvotes:
        upvote(msg.channel_id, pnutuser, prefs)

    elif vote in downvotes:
        downvote(msg.channel_id, pnutuser, prefs)

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)

    if not _connected.isSet() and 'connection_id' in msg['meta']:
        if _startup.isSet():
            send(config['CHANNEL'], "...connecting circuits...")
        logger.debug("connection_id: " + msg['meta']['connection_id'])
        subscribe(msg['meta']['connection_id'])
        return
    
    if 'data' in msg:

        if "channel_type" in msg['meta'] and msg['meta']['channel_type'] == "io.pnut.core.chat":

            pmsg = pnutpy.models.Message.from_response_data(msg['data'])

            if 'is_deleted' in msg['meta']:
                return

            vpattern = r"([\w]+)\s?(\-\-|\+\+|\U0001F44D|\U0001F44E|\:thumbsup\:|\:\+1\:|\:thumbsdown\:|\:-1\:|\+1|-1)"
            votes = re.search(vpattern, pmsg.content.text)

            if pmsg.user.username == config['USERNAME']:
                return

            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('!'):
                on_command(pmsg)

            elif votes:
                on_vote(pmsg, votes)

def on_error(ws, error):
    logger.error("on_error: !!! ERROR !!!")
    logger.error(error)
    _error.set()

def on_close(ws):
    # send(config['CHANNEL'], "...shutdown initiated...")
    logger.debug("on_close: ### CLOSED ###")
    _connected.clear()

def on_open(ws):

    def run(*args):
        while not _error.isSet():
            qmsg = Queue.query.one_or_none()
            if qmsg:
                send(config['CHANNEL'], qmsg.msg)
                db_session.delete(qmsg)
                db_session.commit()
            ws.send(".")
            time.sleep(3)
        logger.debug("*** terminate ***")
    
    t = threading.Thread(target=run)
    t.start()

if __name__ == "__main__":

    logger = logging.getLogger()
    a_parser = argparse.ArgumentParser()
    a_parser.add_argument(
        '-d', action='store_true', dest='debug',
        help="debug logging"
    )
    a_parser.add_argument(
        '-c', '--config', default="config.yaml",
        help="configuration file"
    )
    args = a_parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    with open(args.config, 'rb') as config_file:
        config = yaml.load(config_file, Loader=yaml.SafeLoader)

    engine = create_engine(config['SERVICE_DB'])
    db_session = scoped_session(sessionmaker(bind=engine))
    Base.query = db_session.query_property()
    Base.metadata.create_all(bind=engine)

    pnutpy.api.add_authorization_token(config['ACCESS_TOKEN'])

    ws_url = "wss://stream.pnut.io/v0/user"
    ws_url += "?access_token=" + config['ACCESS_TOKEN']

    # setup the websocket connection
    ws = websocket.WebSocketApp(ws_url, on_message=on_message, 
        on_error=on_error, on_close=on_close)
    ws.on_open = on_open
    r = True
    _startup.set()
    while r:
        _error.clear()
        r = ws.run_forever()