From b0ad45e93590bd84fa6eb0f1569d8cd082be2049 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sun, 19 Nov 2023 16:18:49 -0800 Subject: [PATCH] Handle 404 response when a user with karma no longer exists When a user which has karma deletes there account on pnut.io a 404 is generated on user lookup. This will handle the error gracefully and allow the !karma command to complete which resolves issue #2. A more complete for this should be to delete the karma entry for the now invalid user, to be done in another commit. --- partybot/pnutbot.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/partybot/pnutbot.py b/partybot/pnutbot.py index 0476c8d..e109db4 100644 --- a/partybot/pnutbot.py +++ b/partybot/pnutbot.py @@ -103,7 +103,7 @@ def optout(msg): db_session.add(entry) db_session.commit() - reply = "@" + msg.user.username + reply = "@" + msg.user.username reply += " you have been removed from the karma table" send(msg.channel_id, reply) @@ -154,8 +154,12 @@ 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" + try: + user, meta = pnutpy.api.get_user(entry.userid) + reply += user.username + ": " + str(entry.karma) + "\n" + + except pnutpy.errors.PnutMissing: + continue send(room, reply) def chimpnut(msg): @@ -163,7 +167,7 @@ def chimpnut(msg): 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" @@ -182,16 +186,16 @@ def on_command(msg): 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) @@ -215,7 +219,7 @@ def on_vote(msg, matcher): 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" @@ -267,7 +271,7 @@ def on_mndp(msg): if "NowPlaying" not in tags: return - + for m in mentions: addkarma = False try: @@ -307,7 +311,7 @@ def on_message(ws, message): 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": @@ -367,7 +371,7 @@ def on_open(ws): step += 1 time.sleep(5) logger.debug("*** terminate ***") - + t = threading.Thread(target=run) t.start() @@ -420,7 +424,7 @@ def main(): ws_url += "?access_token=" + config['ACCESS_TOKEN'] # setup the websocket connection - ws = websocket.WebSocketApp(ws_url, on_message=on_message, + ws = websocket.WebSocketApp(ws_url, on_message=on_message, on_error=on_error, on_close=on_close) ws.on_open = on_open r = True