From 0bca3d65a8d7345deef23dd71c84fd18de222bb0 Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 6 Sep 2022 18:20:44 +0200 Subject: [PATCH] add base support for display name changes --- tlapbot/__init__.py | 1 + tlapbot/owncast_helpers.py | 21 +++++++++++---- tlapbot/owncast_webhooks.py | 52 +++++++++++++++++++++---------------- tlapbot/schema.sql | 10 +++++++ 4 files changed, 57 insertions(+), 27 deletions(-) diff --git a/tlapbot/__init__.py b/tlapbot/__init__.py index dc184a6..47535ba 100644 --- a/tlapbot/__init__.py +++ b/tlapbot/__init__.py @@ -22,6 +22,7 @@ def create_app(test_config=None): def proxy_job(): with app.app_context(): + #TODO: make this not give points when stream is offline lol give_points_to_chat(get_db()) points_giver = BackgroundScheduler() diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 3ebd90c..b37656c 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -21,7 +21,7 @@ def give_points_to_user(db, user_id, points): ) db.commit() except Error as e: - print("Error occured giving DEBUG points:", e.args[0]) + print("Error occured giving points:", e.args[0]) print("To user:", user_id, " amount of points:", points) def use_points(db, user_id, points): @@ -58,7 +58,7 @@ def user_exists(db, user_id): print("To user:", user_id) """ Adds a new user to the database. Does nothing if user is already in.""" -def add_user_to_database(db, user_id): +def add_user_to_database(db, user_id, display_name): try: cursor = db.execute( "SELECT points FROM points WHERE id = ?", @@ -66,13 +66,24 @@ def add_user_to_database(db, user_id): ) if cursor.fetchone() == None: cursor.execute( - "INSERT INTO points(id, points) VALUES(?, 10)", - (user_id,) + "INSERT INTO points(id, name, points) VALUES(?, ?, 10)", + (user_id, display_name) ) db.commit() except Error as e: print("Error occured adding user to db:", e.args[0]) - print("To user:", user_id) + print("To user:", user_id, display_name) + +def change_display_name(db, user_id, new_name): + try: + cursor = db.execute( + "UPDATE points SET name = ? WHERE id = ?", + (user_id, new_name,) + ) + db.commit() + except Error as e: + print("Error occured changing display name:", e.args[0]) + print("To user:", user_id, new_name) def send_chat(message): url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send' diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index a783dfb..1570b2d 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -11,27 +11,35 @@ def owncast_webhook(): db = get_db() if data["type"] == "USER_JOINED": user_id = data["eventData"]["user"]["id"] - # CONSIDER: join points for joining stream - add_user_to_database(db, user_id) - elif data["type"] == "CHAT": display_name = data["eventData"]["user"]["displayName"] - print("New chat message:") - print(f'from {display_name}:') - print(f'{data["eventData"]["body"]}') - user_id = data["eventData"]["user"]["id"] - if "!points" in data["eventData"]["body"]: - if not user_exists(db, user_id): - add_user_to_database(db, user_id) - points = read_users_points(db, user_id) - message = "{}'s points: {}".format(display_name, points) - print(message) - send_chat(message) - elif "!drink" in data["eventData"]["body"]: - points = read_users_points(db, user_id) - if points is not None: - if points > 60: - use_points(db, user_id, 60) - send_chat("Enjoy your DRINK........... sips") - # else: # DEBUG: give points for message - # give_points_to_chat(db) + # CONSIDER: join points for joining stream + add_user_to_database(db, user_id, display_name) + elif data["type"] == "NAME_CHANGE": + user_id = data["eventData"]["user"]["id"] + new_name = data["eventData"]["newName"] + old_names = data["eventData"]["user"]["previousNames"] + print("Changing name:") + print(f'from {old_names} to {new_name}') + change_display_name(db, user_id, new_name) + elif data["type"] == "CHAT": + user_id = data["eventData"]["user"]["id"] + if data["eventData"]["visible"]: + display_name = data["eventData"]["user"]["displayName"] + print("New chat message:") + print(f'from {display_name}:') + print(f'{data["eventData"]["body"]}') + if "!points" in data["eventData"]["body"]: + if not user_exists(db, user_id): + add_user_to_database(db, user_id, display_name) + points = read_users_points(db, user_id) + message = "{}'s points: {}".format(display_name, points) + send_chat(message) + elif "!drink" in data["eventData"]["body"]: + points = read_users_points(db, user_id) + if points is not None: + if points > 60: + use_points(db, user_id, 60) + send_chat("Enjoy your DRINK........... sips") + # else: # DEBUG: give points for message + # give_points_to_chat(db) return data \ No newline at end of file diff --git a/tlapbot/schema.sql b/tlapbot/schema.sql index b168c78..0770ea7 100644 --- a/tlapbot/schema.sql +++ b/tlapbot/schema.sql @@ -1,6 +1,16 @@ DROP TABLE IF EXISTS points; +DROP TABLE IF EXISTS redeem_queue; CREATE TABLE points ( id TEXT PRIMARY KEY, + name TEXT, points INTEGER +); + +CREATE TABLE redeem_queue ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + redeem TEXT, + redeemer_id TEXT, + FOREIGN KEY (redeemer_id) REFERENCES points (id) ); \ No newline at end of file