diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 0e17bce..f766627 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -102,14 +102,22 @@ def add_user_to_database(db, user_id, display_name): """ Adds a new user to the database. Does nothing if user is already in.""" try: cursor = db.execute( - "SELECT points FROM points WHERE id = ?", + "SELECT points, name FROM points WHERE id = ?", (user_id,) ) - if cursor.fetchone() is None: + user = cursor.fetchone() + if user is None: cursor.execute( "INSERT INTO points(id, name, points) VALUES(?, ?, 10)", (user_id, display_name) ) + if user is not None and user[1] == None: + cursor.execute( + """UPDATE points + SET name = ? + WHERE id = ?""", + (display_name, user_id) + ) db.commit() except Error as e: print("Error occured adding user to db:", e.args[0]) @@ -202,8 +210,8 @@ def remove_duplicate_usernames(db, user_id, username): try: cursor = db.execute( """UPDATE points - SET username = NULL - WHERE username = ? AND WHERE NOT id = ?""", + SET name = NULL + WHERE name = ? AND NOT id = ?""", (username, user_id) ) db.commit() diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index 5fb4bd0..45d1bc2 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -2,7 +2,7 @@ from flask import Flask, request, json, Blueprint, current_app from sqlite3 import Error from tlapbot.db import get_db from tlapbot.owncast_helpers import (add_user_to_database, change_display_name, - user_exists, send_chat, read_users_points) + user_exists, send_chat, read_users_points, remove_duplicate_usernames) from tlapbot.redeems_handler import handle_redeem bp = Blueprint('owncast_webhooks', __name__)