From 062e1c113edc3461f9d08ae93a0f0b28c171ff50 Mon Sep 17 00:00:00 2001 From: Lili Date: Mon, 10 Oct 2022 15:14:57 +0200 Subject: [PATCH 01/33] prepare skeletons for configurable redeems move redeem handling code to redeem handler --- setup.py | 2 +- tlapbot/__init__.py | 2 +- tlapbot/owncast_webhooks.py | 21 ++++++++------------- tlapbot/redeems_handler.py | 5 +++++ 4 files changed, 15 insertions(+), 15 deletions(-) create mode 100644 tlapbot/redeems_handler.py diff --git a/setup.py b/setup.py index 9fe8b0c..2c1c63f 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import find_packages, setup setup( name='tlapbot', - version='0.5.3', + version='0.6.0a1', packages=find_packages(), include_package_data=True, install_requires=[ diff --git a/tlapbot/__init__.py b/tlapbot/__init__.py index bc19be3..f225bdc 100644 --- a/tlapbot/__init__.py +++ b/tlapbot/__init__.py @@ -20,7 +20,7 @@ def create_app(test_config=None): ) app.config.from_object('tlapbot.default_config') app.config.from_pyfile('config.py') - + app.config.from_pyfile('redeems.py') # prepare webhooks and redeem dashboard blueprints from . import owncast_webhooks diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index b53ae26..ad3cb87 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -1,7 +1,9 @@ -from flask import Flask,request,json,Blueprint +from flask import Flask, request, json, Blueprint from sqlite3 import Error from tlapbot.db import get_db -from tlapbot.owncast_helpers import * +from tlapbot.owncast_helpers import (add_user_to_database, change_display_name, + user_exists, send_chat, read_users_points) +from tlapbot.redeems_handler import handle_redeem bp = Blueprint('owncast_webhooks', __name__) @@ -28,25 +30,18 @@ def owncast_webhook(): !points to see your points. !drink to redeem a pitíčko for 60 points. That's it for now.""" + # TODO: also make this customizable send_chat(message) elif "!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) + message = f"{display_name}'s points: {points}" send_chat(message) - elif "!drink" in data["eventData"]["body"]: - points = read_users_points(db, user_id) - if points is not None and points >= 60: - if use_points(db, user_id, 60): - add_to_redeem_queue(db, user_id, "drink") - send_chat("Pitíčko redeemed for 60 points.") - else: - send_chat("Pitíčko not redeemed because of an error.") - else: - send_chat("Can't redeem pitíčko, you don't have enough points.") elif "!name_update" in data["eventData"]["body"]: # Forces name update in case bot didn't catch the NAME_CHANGE # event. Theoretically only needed when bot was off. change_display_name(db, user_id, display_name) + elif "!" in data["eventData"]["body"]: + handle_redeem(data["eventData"]["body"]) return data \ No newline at end of file diff --git a/tlapbot/redeems_handler.py b/tlapbot/redeems_handler.py new file mode 100644 index 0000000..a950e58 --- /dev/null +++ b/tlapbot/redeems_handler.py @@ -0,0 +1,5 @@ +from tlapbot.db import get_db +from tlapbot.owncast_helpers import use_points, add_to_redeem_queue + +def handle_redeem() + pass \ No newline at end of file From 1faee351692fb688364387d48997ef8cc8e84432 Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 12 Oct 2022 15:30:12 +0200 Subject: [PATCH 02/33] indent fixes + new helpers + updated helpers --- tlapbot/owncast_helpers.py | 47 ++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 8826571..236dcda 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -44,8 +44,8 @@ def read_users_points(db, user_id): def give_points_to_user(db, user_id, points): try: db.execute( - "UPDATE points SET points = points + ? WHERE id = ?", - (points, user_id,) + "UPDATE points SET points = points + ? WHERE id = ?", + (points, user_id,) ) db.commit() except Error as e: @@ -55,8 +55,8 @@ def give_points_to_user(db, user_id, points): def use_points(db, user_id, points): try: db.execute( - "UPDATE points SET points = points - ? WHERE id = ?", - (points, user_id,) + "UPDATE points SET points = points - ? WHERE id = ?", + (points, user_id,) ) db.commit() return True @@ -98,32 +98,45 @@ def add_user_to_database(db, user_id, display_name): def change_display_name(db, user_id, new_name): try: cursor = db.execute( - "UPDATE points SET name = ? WHERE id = ?", - (new_name, user_id) - ) + "UPDATE points SET name = ? WHERE id = ?", + (new_name, user_id) + ) db.commit() except Error as e: print("Error occured changing display name:", e.args[0]) print("To user:", user_id, new_name) - -def add_to_redeem_queue(db, user_id, redeem_name): +def add_to_counter(db, counter_name): try: cursor = db.execute( - "INSERT INTO redeem_queue(redeem, redeemer_id) VALUES(?, ?)", - (redeem_name, user_id) + "UPDATE counters SET count = count + 1 WHERE name = ?", + (counter_name,) + ) + db.commit() + except Error as e: + print("Error occured adding to counter:", e.args[0]) + print("To counter:", counter_name) + +def add_to_redeem_queue(db, user_id, redeem_name, note=None): + try: + cursor = db.execute( + "INSERT INTO redeem_queue(redeem, redeemer_id, note) VALUES(?, ?, ?)", + (redeem_name, user_id, note) ) db.commit() except Error as e: print("Error occured adding to redeem queue:", e.args[0]) - print("To user:", user_id, " with redeem:", redeem_name) + print("To user:", user_id, " with redeem:", redeem_name, "with note:", note) def clear_redeem_queue(db): try: cursor = db.execute( - "DELETE FROM redeem_queue" - ) + "DELETE FROM redeem_queue" + ) + cursor.execute( + """UPDATE counters SET counter_count = 0""" + ) db.commit() except Error as e: print("Error occured deleting redeem queue:", e.args[0]) @@ -131,7 +144,7 @@ def clear_redeem_queue(db): def pretty_redeem_queue(db): try: cursor = db.execute( - """SELECT redeem_queue.created, redeem_queue.redeem, points.name + """SELECT redeem_queue.created, redeem_queue.redeem, redeem_queue.note, points.name FROM redeem_queue INNER JOIN points on redeem_queue.redeemer_id = points.id""" @@ -143,8 +156,8 @@ def pretty_redeem_queue(db): def whole_redeem_queue(db): try: cursor = db.execute( - "SELECT * from redeem_queue" - ) + "SELECT * from redeem_queue" + ) return cursor.fetchall() except Error as e: print("Error occured selecting redeem queue:", e.args[0]) From 099525f9ffd52ea6403616708a222b06a666c062 Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 12 Oct 2022 15:31:34 +0200 Subject: [PATCH 03/33] base customizable redeems code skeletons UNTESTED but i think i got it all --- tlapbot/db.py | 12 ++++++++++++ tlapbot/owncast_webhooks.py | 4 ++-- tlapbot/redeems_handler.py | 34 ++++++++++++++++++++++++++++++++-- tlapbot/schema.sql | 11 +++++++++-- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/tlapbot/db.py b/tlapbot/db.py index 74dea73..e591486 100644 --- a/tlapbot/db.py +++ b/tlapbot/db.py @@ -22,11 +22,23 @@ def close_db(e=None): if db is not None: db.close() +def insert_counters(db): + for redeem, redeem_info in current_app.config['REDEEMS'].items(): + if redeem_info["type"] == "counter": + try: + cursor = db.execute( + "INSERT INTO counters(name, count) VALUES(?, 0)", + (redeem,) + ) + db.commit() + def init_db(): db = get_db() with current_app.open_resource('schema.sql') as f: db.executescript(f.read().decode('utf8')) + + insert_counters(db) @click.command('init-db') diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index ad3cb87..4100742 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -42,6 +42,6 @@ def owncast_webhook(): # Forces name update in case bot didn't catch the NAME_CHANGE # event. Theoretically only needed when bot was off. change_display_name(db, user_id, display_name) - elif "!" in data["eventData"]["body"]: - handle_redeem(data["eventData"]["body"]) + elif data["eventData"]["body"].startswith("!"): # TODO: make prefix configurable + handle_redeem(data["eventData"]["body"], user_id) return data \ No newline at end of file diff --git a/tlapbot/redeems_handler.py b/tlapbot/redeems_handler.py index a950e58..d3c63e0 100644 --- a/tlapbot/redeems_handler.py +++ b/tlapbot/redeems_handler.py @@ -1,5 +1,35 @@ +from flask import current_app from tlapbot.db import get_db from tlapbot.owncast_helpers import use_points, add_to_redeem_queue -def handle_redeem() - pass \ No newline at end of file +def handle_redeem(message, user_id) + split_message = message[1:].split(maxsplit=1) + redeem = split_message[0] + if len(split_message) == 1: + note = None + else: + note = split_message[1] + + if redeem in current_app.config['REDEEMS']: + price = current_app.config['REDEEMS'][redeem]["price"] + redeem_type = current_app.config['REDEEMS'][redeem]["type"] + points = read_users_points(db, user_id) + if points is not None and points >= price: + if use_points(db, user_id, redeem): + if redeem_type == "counter": + add_to_counter(db, redeem) + elif redeem_type == "list": + add_to_redeem_queue(db, user_id, redeem) + elif redeem_type == "note": + if note is not None: + add_to_redeem_queue(db, user_id, redeem, note) + else: + send_chat(f"Cannot redeem {redeem}, no note included.") + send_chat(f"{redeem} redeemed for 60 points.") + else: + send_chat(f"{redeem} not redeemed because of an error.") + else: + send_chat(f"Can't redeem {redeem}, you don't have enough points.") + else: + send_chat("Can't redeem, redeem not found.") + return False \ No newline at end of file diff --git a/tlapbot/schema.sql b/tlapbot/schema.sql index 0770ea7..b1cc10f 100644 --- a/tlapbot/schema.sql +++ b/tlapbot/schema.sql @@ -7,10 +7,17 @@ CREATE TABLE points ( points INTEGER ); +CREATE TABLE counters { + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL + count INTEGER NOT NULL +} + CREATE TABLE redeem_queue ( id INTEGER PRIMARY KEY AUTOINCREMENT, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - redeem TEXT, - redeemer_id TEXT, + redeem TEXT NOT NULL, + redeemer_id TEXT NOT NULL, + note TEXT, FOREIGN KEY (redeemer_id) REFERENCES points (id) ); \ No newline at end of file From ede1cca8da3a4c134f2f940dea14ba7e38f809c7 Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 12 Oct 2022 15:35:36 +0200 Subject: [PATCH 04/33] code skeleton fixes --- tlapbot/redeems_handler.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tlapbot/redeems_handler.py b/tlapbot/redeems_handler.py index d3c63e0..db2f233 100644 --- a/tlapbot/redeems_handler.py +++ b/tlapbot/redeems_handler.py @@ -1,6 +1,7 @@ from flask import current_app from tlapbot.db import get_db -from tlapbot.owncast_helpers import use_points, add_to_redeem_queue +from tlapbot.owncast_helpers import (use_points, add_to_redeem_queue, + add_to_counter, read_users_points, send_chat) def handle_redeem(message, user_id) split_message = message[1:].split(maxsplit=1) @@ -11,11 +12,12 @@ def handle_redeem(message, user_id) note = split_message[1] if redeem in current_app.config['REDEEMS']: + db = get_db() price = current_app.config['REDEEMS'][redeem]["price"] redeem_type = current_app.config['REDEEMS'][redeem]["type"] points = read_users_points(db, user_id) if points is not None and points >= price: - if use_points(db, user_id, redeem): + if use_points(db, user_id, price): if redeem_type == "counter": add_to_counter(db, redeem) elif redeem_type == "list": @@ -25,11 +27,10 @@ def handle_redeem(message, user_id) add_to_redeem_queue(db, user_id, redeem, note) else: send_chat(f"Cannot redeem {redeem}, no note included.") - send_chat(f"{redeem} redeemed for 60 points.") + send_chat(f"{redeem} redeemed for {price} points.") else: send_chat(f"{redeem} not redeemed because of an error.") else: send_chat(f"Can't redeem {redeem}, you don't have enough points.") else: - send_chat("Can't redeem, redeem not found.") - return False \ No newline at end of file + send_chat("Can't redeem, redeem not found.") \ No newline at end of file From 4a3927a1967eb62a88cf3332fc27bb5ff0dea6d8 Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 12 Oct 2022 15:48:03 +0200 Subject: [PATCH 05/33] schema fix --- tlapbot/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tlapbot/schema.sql b/tlapbot/schema.sql index b1cc10f..d108c2c 100644 --- a/tlapbot/schema.sql +++ b/tlapbot/schema.sql @@ -11,7 +11,7 @@ CREATE TABLE counters { id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL count INTEGER NOT NULL -} +}; CREATE TABLE redeem_queue ( id INTEGER PRIMARY KEY AUTOINCREMENT, From 5384076f64fb2ee8240faddca9cb776e2c9963ac Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 12 Oct 2022 15:48:18 +0200 Subject: [PATCH 06/33] another schema fix --- tlapbot/schema.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tlapbot/schema.sql b/tlapbot/schema.sql index d108c2c..99fca09 100644 --- a/tlapbot/schema.sql +++ b/tlapbot/schema.sql @@ -7,11 +7,11 @@ CREATE TABLE points ( points INTEGER ); -CREATE TABLE counters { +CREATE TABLE counters ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL count INTEGER NOT NULL -}; +); CREATE TABLE redeem_queue ( id INTEGER PRIMARY KEY AUTOINCREMENT, From 3c5a09b2dea14427017206396267dd62fa379513 Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 12 Oct 2022 15:48:57 +0200 Subject: [PATCH 07/33] update dashboard + dashboard helpers also untested --- tlapbot/owncast_helpers.py | 9 +++++++++ tlapbot/owncast_redeem_dashboard.py | 11 ++++------- tlapbot/templates/dashboard.html | 15 +++++++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 236dcda..2f32438 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -141,6 +141,15 @@ def clear_redeem_queue(db): except Error as e: print("Error occured deleting redeem queue:", e.args[0]) +def all_counters(db): + try: + cursor = db.execute( + """SELECT counters.name, counters.count FROM counters""" + ) + return cursor.fetchall() + except Error as e: + print("Error occured selecting all counters:", e.args[0]) + def pretty_redeem_queue(db): try: cursor = db.execute( diff --git a/tlapbot/owncast_redeem_dashboard.py b/tlapbot/owncast_redeem_dashboard.py index 27c7c64..e0fd390 100644 --- a/tlapbot/owncast_redeem_dashboard.py +++ b/tlapbot/owncast_redeem_dashboard.py @@ -7,14 +7,11 @@ bp = Blueprint('redeem_dashboard', __name__) @bp.route('/dashboard',methods=['GET']) def dashboard(): - queue = pretty_redeem_queue(get_db()) - number_of_drinks = 0 + db = get_db() + queue = pretty_redeem_queue(db) + counters = all_counters(db) utc_timezone = timezone.utc - if queue is not None: - for row in queue: - if row[1] == "drink": - number_of_drinks += 1 return render_template('dashboard.html', queue=queue, - number_of_drinks=number_of_drinks, + counters=counters, utc_timezone=utc_timezone) \ No newline at end of file diff --git a/tlapbot/templates/dashboard.html b/tlapbot/templates/dashboard.html index f4865e2..5469bd2 100644 --- a/tlapbot/templates/dashboard.html +++ b/tlapbot/templates/dashboard.html @@ -5,12 +5,21 @@ Redeems Dashboard + {% if counters %} + + + + + - - + {% for counter in counters %} + +
Counters
Number of drinks: {{ number_of_drinks }} {{ counter[0] }} {{ counter[1] }}
+ {% endif %} + {% if queue %} @@ -18,6 +27,7 @@ + {% for row in queue %} @@ -25,6 +35,7 @@ + {% endfor %}
time redeem redeemernote
{{ row[0].replace(tzinfo=utc_timezone).astimezone().strftime("%H:%M") }} {{ row[1] }} {{ row[2] }}{{ row[3] }}
From dda415f87ac5aea815e8dddc2b5f8e13dfa061b9 Mon Sep 17 00:00:00 2001 From: Lili Date: Thu, 13 Oct 2022 13:29:38 +0200 Subject: [PATCH 08/33] init-db typo fixes --- tlapbot/db.py | 8 +++++--- tlapbot/redeems_handler.py | 2 +- tlapbot/schema.sql | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tlapbot/db.py b/tlapbot/db.py index e591486..9d897d6 100644 --- a/tlapbot/db.py +++ b/tlapbot/db.py @@ -27,10 +27,12 @@ def insert_counters(db): if redeem_info["type"] == "counter": try: cursor = db.execute( - "INSERT INTO counters(name, count) VALUES(?, 0)", - (redeem,) - ) + "INSERT INTO counters(name, count) VALUES(?, 0)", + (redeem,) + ) db.commit() + except Error as e: + print("Failed inserting counters to db:", e.args[0]) def init_db(): db = get_db() diff --git a/tlapbot/redeems_handler.py b/tlapbot/redeems_handler.py index db2f233..d1be064 100644 --- a/tlapbot/redeems_handler.py +++ b/tlapbot/redeems_handler.py @@ -3,7 +3,7 @@ from tlapbot.db import get_db from tlapbot.owncast_helpers import (use_points, add_to_redeem_queue, add_to_counter, read_users_points, send_chat) -def handle_redeem(message, user_id) +def handle_redeem(message, user_id): split_message = message[1:].split(maxsplit=1) redeem = split_message[0] if len(split_message) == 1: diff --git a/tlapbot/schema.sql b/tlapbot/schema.sql index 99fca09..549540c 100644 --- a/tlapbot/schema.sql +++ b/tlapbot/schema.sql @@ -9,7 +9,7 @@ CREATE TABLE points ( CREATE TABLE counters ( id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL + name TEXT NOT NULL, count INTEGER NOT NULL ); From 796febb91f745d9e9d8a9cdf7f20f46fae348fc4 Mon Sep 17 00:00:00 2001 From: Lili Date: Thu, 13 Oct 2022 13:53:42 +0200 Subject: [PATCH 09/33] dashboard fixes --- tlapbot/owncast_redeem_dashboard.py | 2 +- tlapbot/templates/dashboard.html | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tlapbot/owncast_redeem_dashboard.py b/tlapbot/owncast_redeem_dashboard.py index e0fd390..e38f81b 100644 --- a/tlapbot/owncast_redeem_dashboard.py +++ b/tlapbot/owncast_redeem_dashboard.py @@ -1,6 +1,6 @@ from flask import render_template, Blueprint from tlapbot.db import get_db -from tlapbot.owncast_helpers import pretty_redeem_queue +from tlapbot.owncast_helpers import pretty_redeem_queue, all_counters from datetime import datetime, timezone bp = Blueprint('redeem_dashboard', __name__) diff --git a/tlapbot/templates/dashboard.html b/tlapbot/templates/dashboard.html index 5469bd2..daa1a84 100644 --- a/tlapbot/templates/dashboard.html +++ b/tlapbot/templates/dashboard.html @@ -12,11 +12,12 @@ Counters + {% for counter in counters %} - {% for counter in counters %} {{ counter[0] }} - {{ counter[1] }} + {{ counter[1] }} + {% endfor %} {% endif %} From 5c6b070a7a2b0f4c50f4fbb215578533ee370290 Mon Sep 17 00:00:00 2001 From: Lili Date: Thu, 13 Oct 2022 14:09:12 +0200 Subject: [PATCH 10/33] redeems dashboard improvement --- tlapbot/redeems_handler.py | 4 +++- tlapbot/templates/dashboard.html | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tlapbot/redeems_handler.py b/tlapbot/redeems_handler.py index d1be064..e4f3f6e 100644 --- a/tlapbot/redeems_handler.py +++ b/tlapbot/redeems_handler.py @@ -20,14 +20,16 @@ def handle_redeem(message, user_id): if use_points(db, user_id, price): if redeem_type == "counter": add_to_counter(db, redeem) + send_chat(f"{redeem} redeemed for {price} points.") elif redeem_type == "list": add_to_redeem_queue(db, user_id, redeem) + send_chat(f"{redeem} redeemed for {price} points.") elif redeem_type == "note": if note is not None: add_to_redeem_queue(db, user_id, redeem, note) + send_chat(f"{redeem} redeemed for {price} points.") else: send_chat(f"Cannot redeem {redeem}, no note included.") - send_chat(f"{redeem} redeemed for {price} points.") else: send_chat(f"{redeem} not redeemed because of an error.") else: diff --git a/tlapbot/templates/dashboard.html b/tlapbot/templates/dashboard.html index daa1a84..7b13cf2 100644 --- a/tlapbot/templates/dashboard.html +++ b/tlapbot/templates/dashboard.html @@ -35,8 +35,10 @@ {{ row[0].replace(tzinfo=utc_timezone).astimezone().strftime("%H:%M") }} {{ row[1] }} - {{ row[2] }} {{ row[3] }} + {% if row[2] %} + {{ row[2] }} + {% endif %} {% endfor %} From f916d46d557d5fe94a8952b86510efa71cb2fec2 Mon Sep 17 00:00:00 2001 From: Lili Date: Thu, 13 Oct 2022 14:23:31 +0200 Subject: [PATCH 11/33] change schema to drop counters and NOT drop points --- tlapbot/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tlapbot/schema.sql b/tlapbot/schema.sql index 549540c..bbbfdcd 100644 --- a/tlapbot/schema.sql +++ b/tlapbot/schema.sql @@ -1,4 +1,4 @@ -DROP TABLE IF EXISTS points; +DROP TABLE IF EXISTS counters; DROP TABLE IF EXISTS redeem_queue; CREATE TABLE points ( From f391a7bd12d07dd1b7a0fc8d9d7838930d51c787 Mon Sep 17 00:00:00 2001 From: Lili Date: Thu, 13 Oct 2022 14:24:36 +0200 Subject: [PATCH 12/33] create points table if not exists --- tlapbot/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tlapbot/schema.sql b/tlapbot/schema.sql index bbbfdcd..681cc70 100644 --- a/tlapbot/schema.sql +++ b/tlapbot/schema.sql @@ -1,7 +1,7 @@ DROP TABLE IF EXISTS counters; DROP TABLE IF EXISTS redeem_queue; -CREATE TABLE points ( +CREATE TABLE IF NOT EXISTS points ( id TEXT PRIMARY KEY, name TEXT, points INTEGER From 06114022da3681ef25154f07de902d5847fd2292 Mon Sep 17 00:00:00 2001 From: Lili Date: Mon, 17 Oct 2022 18:37:49 +0200 Subject: [PATCH 13/33] code style fixes --- tlapbot/redeems_handler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tlapbot/redeems_handler.py b/tlapbot/redeems_handler.py index e4f3f6e..6f7f697 100644 --- a/tlapbot/redeems_handler.py +++ b/tlapbot/redeems_handler.py @@ -1,7 +1,8 @@ from flask import current_app from tlapbot.db import get_db from tlapbot.owncast_helpers import (use_points, add_to_redeem_queue, - add_to_counter, read_users_points, send_chat) + add_to_counter, read_users_points, send_chat) + def handle_redeem(message, user_id): split_message = message[1:].split(maxsplit=1) @@ -35,4 +36,4 @@ def handle_redeem(message, user_id): else: send_chat(f"Can't redeem {redeem}, you don't have enough points.") else: - send_chat("Can't redeem, redeem not found.") \ No newline at end of file + send_chat("Can't redeem, redeem not found.") From d8230da0130ddb6698ce229b1baebb4e2efde86f Mon Sep 17 00:00:00 2001 From: Lili Date: Mon, 17 Oct 2022 18:47:30 +0200 Subject: [PATCH 14/33] more style fixes everywhere else --- tlapbot/__init__.py | 8 ++++---- tlapbot/db.py | 7 +++++-- tlapbot/owncast_helpers.py | 20 +++++++++++++++++--- tlapbot/owncast_redeem_dashboard.py | 5 +++-- tlapbot/owncast_webhooks.py | 9 +++++---- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/tlapbot/__init__.py b/tlapbot/__init__.py index f225bdc..c3fcdfd 100644 --- a/tlapbot/__init__.py +++ b/tlapbot/__init__.py @@ -4,6 +4,7 @@ from apscheduler.schedulers.background import BackgroundScheduler from tlapbot.db import get_db from tlapbot.owncast_helpers import is_stream_live, give_points_to_chat + def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) @@ -27,7 +28,7 @@ def create_app(test_config=None): from . import owncast_redeem_dashboard app.register_blueprint(owncast_webhooks.bp) app.register_blueprint(owncast_redeem_dashboard.bp) - + # add db initialization CLI command from . import db db.init_app(app) @@ -44,12 +45,11 @@ def create_app(test_config=None): # start scheduler that will give points to users points_giver = BackgroundScheduler() - points_giver.add_job(proxy_job, 'interval', seconds=app.config['POINTS_CYCLE_TIME']) # change to 10 minutes out of testing + points_giver.add_job(proxy_job, 'interval', seconds=app.config['POINTS_CYCLE_TIME']) points_giver.start() return app - if __name__ == '__main__': - create_app() \ No newline at end of file + create_app() diff --git a/tlapbot/db.py b/tlapbot/db.py index 9d897d6..8bb5b68 100644 --- a/tlapbot/db.py +++ b/tlapbot/db.py @@ -22,6 +22,7 @@ def close_db(e=None): if db is not None: db.close() + def insert_counters(db): for redeem, redeem_info in current_app.config['REDEEMS'].items(): if redeem_info["type"] == "counter": @@ -34,12 +35,13 @@ def insert_counters(db): except Error as e: print("Failed inserting counters to db:", e.args[0]) + def init_db(): db = get_db() with current_app.open_resource('schema.sql') as f: db.executescript(f.read().decode('utf8')) - + insert_counters(db) @@ -50,6 +52,7 @@ def init_db_command(): init_db() click.echo('Initialized the database.') + def init_app(app): app.teardown_appcontext(close_db) - app.cli.add_command(init_db_command) \ No newline at end of file + app.cli.add_command(init_db_command) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 2f32438..44898df 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -5,6 +5,7 @@ import click from flask.cli import with_appcontext from tlapbot.db import get_db + # # # requests stuff # # # def is_stream_live(): url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/status' @@ -12,6 +13,7 @@ def is_stream_live(): print(r.json()["online"]) return r.json()["online"] + def give_points_to_chat(db): url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/clients' headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} @@ -22,6 +24,7 @@ def give_points_to_chat(db): user_id, current_app.config['POINTS_AMOUNT_GIVEN']) + def send_chat(message): url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send' headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} @@ -41,6 +44,7 @@ def read_users_points(db, user_id): print("Error occured reading points:", e.args[0]) print("To user:", user_id) + def give_points_to_user(db, user_id, points): try: db.execute( @@ -52,6 +56,7 @@ def give_points_to_user(db, user_id, points): print("Error occured giving points:", e.args[0]) print("To user:", user_id, " amount of points:", points) + def use_points(db, user_id, points): try: db.execute( @@ -65,27 +70,29 @@ def use_points(db, user_id, points): print("From user:", user_id, " amount of points:", points) return False + def user_exists(db, user_id): try: cursor = db.execute( "SELECT points FROM points WHERE id = ?", (user_id,) ) - if cursor.fetchone() == None: + if cursor.fetchone() is None: return False return True except Error as e: print("Error occured checking if user exists:", e.args[0]) 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, 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 = ?", (user_id,) ) - if cursor.fetchone() == None: + if cursor.fetchone() is None: cursor.execute( "INSERT INTO points(id, name, points) VALUES(?, ?, 10)", (user_id, display_name) @@ -95,6 +102,7 @@ def add_user_to_database(db, user_id, display_name): print("Error occured adding user to db:", e.args[0]) print("To user:", user_id, display_name) + def change_display_name(db, user_id, new_name): try: cursor = db.execute( @@ -118,6 +126,7 @@ def add_to_counter(db, counter_name): print("Error occured adding to counter:", e.args[0]) print("To counter:", counter_name) + def add_to_redeem_queue(db, user_id, redeem_name, note=None): try: cursor = db.execute( @@ -129,6 +138,7 @@ def add_to_redeem_queue(db, user_id, redeem_name, note=None): print("Error occured adding to redeem queue:", e.args[0]) print("To user:", user_id, " with redeem:", redeem_name, "with note:", note) + def clear_redeem_queue(db): try: cursor = db.execute( @@ -141,6 +151,7 @@ def clear_redeem_queue(db): except Error as e: print("Error occured deleting redeem queue:", e.args[0]) + def all_counters(db): try: cursor = db.execute( @@ -150,6 +161,7 @@ def all_counters(db): except Error as e: print("Error occured selecting all counters:", e.args[0]) + def pretty_redeem_queue(db): try: cursor = db.execute( @@ -162,6 +174,7 @@ def pretty_redeem_queue(db): except Error as e: print("Error occured selecting pretty redeem queue:", e.args[0]) + def whole_redeem_queue(db): try: cursor = db.execute( @@ -171,6 +184,7 @@ def whole_redeem_queue(db): except Error as e: print("Error occured selecting redeem queue:", e.args[0]) + @click.command('clear-queue') @with_appcontext def clear_queue_command(): diff --git a/tlapbot/owncast_redeem_dashboard.py b/tlapbot/owncast_redeem_dashboard.py index e38f81b..4203cff 100644 --- a/tlapbot/owncast_redeem_dashboard.py +++ b/tlapbot/owncast_redeem_dashboard.py @@ -5,7 +5,8 @@ from datetime import datetime, timezone bp = Blueprint('redeem_dashboard', __name__) -@bp.route('/dashboard',methods=['GET']) + +@bp.route('/dashboard', methods=['GET']) def dashboard(): db = get_db() queue = pretty_redeem_queue(db) @@ -14,4 +15,4 @@ def dashboard(): return render_template('dashboard.html', queue=queue, counters=counters, - utc_timezone=utc_timezone) \ No newline at end of file + utc_timezone=utc_timezone) diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index 4100742..ed9cee1 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -2,12 +2,13 @@ from flask import Flask, request, json, Blueprint 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) from tlapbot.redeems_handler import handle_redeem bp = Blueprint('owncast_webhooks', __name__) -@bp.route('/owncastWebhook',methods=['POST']) + +@bp.route('/owncastWebhook', methods=['POST']) def owncast_webhook(): data = request.json db = get_db() @@ -42,6 +43,6 @@ def owncast_webhook(): # Forces name update in case bot didn't catch the NAME_CHANGE # event. Theoretically only needed when bot was off. change_display_name(db, user_id, display_name) - elif data["eventData"]["body"].startswith("!"): # TODO: make prefix configurable + elif data["eventData"]["body"].startswith("!"): # TODO: make prefix configurable handle_redeem(data["eventData"]["body"], user_id) - return data \ No newline at end of file + return data From ecb794b8faf9101a71245c767b67287031b60555 Mon Sep 17 00:00:00 2001 From: Lili Date: Mon, 17 Oct 2022 18:55:53 +0200 Subject: [PATCH 15/33] bump version for tuesday stream --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 2c1c63f..881dbb3 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import find_packages, setup setup( name='tlapbot', - version='0.6.0a1', + version='0.6.0a2', packages=find_packages(), include_package_data=True, install_requires=[ From c12bcd68c245e00fa9facd1c2fce987959581fd2 Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 18 Oct 2022 14:04:15 +0200 Subject: [PATCH 16/33] show user's points on dashboard --- tlapbot/owncast_helpers.py | 11 +++++++++++ tlapbot/owncast_redeem_dashboard.py | 12 ++++++++++-- tlapbot/templates/dashboard.html | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 44898df..d432555 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -45,6 +45,17 @@ def read_users_points(db, user_id): print("To user:", user_id) +def read_users_points_from_username(db, username): + try: + cursor = db.execute( + "SELECT points FROM points WHERE name = ?", + (username,) + ) + return cursor.fetchone()[0] + except Error as e: + print("Error occured reading points from username:", e.args[0]) + print("To user:", username) + def give_points_to_user(db, user_id, points): try: db.execute( diff --git a/tlapbot/owncast_redeem_dashboard.py b/tlapbot/owncast_redeem_dashboard.py index 4203cff..db71a79 100644 --- a/tlapbot/owncast_redeem_dashboard.py +++ b/tlapbot/owncast_redeem_dashboard.py @@ -1,6 +1,7 @@ -from flask import render_template, Blueprint +from flask import render_template, Blueprint, request from tlapbot.db import get_db -from tlapbot.owncast_helpers import pretty_redeem_queue, all_counters +from tlapbot.owncast_helpers import (pretty_redeem_queue, all_counters, + read_users_points_from_username) from datetime import datetime, timezone bp = Blueprint('redeem_dashboard', __name__) @@ -11,8 +12,15 @@ def dashboard(): db = get_db() queue = pretty_redeem_queue(db) counters = all_counters(db) + username = request.args.get("username") + if username is not None: + user_points = read_users_points_from_username(db, username) + else: + user_points = None utc_timezone = timezone.utc return render_template('dashboard.html', queue=queue, counters=counters, + username=username, + user_points=user_points, utc_timezone=utc_timezone) diff --git a/tlapbot/templates/dashboard.html b/tlapbot/templates/dashboard.html index 7b13cf2..b6814b1 100644 --- a/tlapbot/templates/dashboard.html +++ b/tlapbot/templates/dashboard.html @@ -5,6 +5,20 @@ Redeems Dashboard + {% if (username and user_points ) %} + + + + + + + + + + +
Your points balance:
{{ username }} {{ user_points }}
+ {% endif %} + {% if counters %} From 6a4c8b759a058039d75975e420d77de44f345e95 Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 18 Oct 2022 14:09:35 +0200 Subject: [PATCH 17/33] fix None type error --- tlapbot/owncast_helpers.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index d432555..1bb4108 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -34,6 +34,7 @@ def send_chat(message): # # # db stuff # # # def read_users_points(db, user_id): + """Errors out if user doesn't exist.""" try: cursor = db.execute( "SELECT points FROM points WHERE id = ?", @@ -46,12 +47,17 @@ def read_users_points(db, user_id): def read_users_points_from_username(db, username): + """Returns None if user doesn't exist, their points otherwise.""" try: cursor = db.execute( "SELECT points FROM points WHERE name = ?", (username,) ) - return cursor.fetchone()[0] + points = cursor.fetchone() + if points is None: + return None + else: + return cursor.fetchone()[0] except Error as e: print("Error occured reading points from username:", e.args[0]) print("To user:", username) From f9ff27c0550f0e1f7fcc5d0d260312cd91f79cc7 Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 18 Oct 2022 14:10:52 +0200 Subject: [PATCH 18/33] bugfix --- tlapbot/owncast_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 1bb4108..dc2ee1f 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -57,7 +57,7 @@ def read_users_points_from_username(db, username): if points is None: return None else: - return cursor.fetchone()[0] + return points[0] except Error as e: print("Error occured reading points from username:", e.args[0]) print("To user:", username) From bf49aa16cdc9b76d7205d5994c9afe17bb5c736d Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 18 Oct 2022 14:40:16 +0200 Subject: [PATCH 19/33] make help send all redeems (the help function needs a rewrite) --- tlapbot/owncast_webhooks.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index ed9cee1..da7badd 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -1,4 +1,4 @@ -from flask import Flask, request, json, Blueprint +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, @@ -28,9 +28,12 @@ def owncast_webhook(): print(f'{data["eventData"]["body"]}') if "!help" in data["eventData"]["body"]: message = """Tlapbot commands: + !help to see this help message. !points to see your points. - !drink to redeem a pitíčko for 60 points. - That's it for now.""" + !name_update to force name update if tlapbot didn't catch it. + Tlapbot redeems:\n""" + for redeem, redeem_info in current_app.config['REDEEMS'].items(): + message += (f"!{redeem} for {redeem_info['price']} points.\n") # TODO: also make this customizable send_chat(message) elif "!points" in data["eventData"]["body"]: From a62c718aa194924e90e4f9e92cdf1c70afcafc86 Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 18 Oct 2022 14:44:46 +0200 Subject: [PATCH 20/33] add a default redeems file --- tlapbot/__init__.py | 3 ++- tlapbot/default_redeems.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tlapbot/default_redeems.py diff --git a/tlapbot/__init__.py b/tlapbot/__init__.py index c3fcdfd..c5a9368 100644 --- a/tlapbot/__init__.py +++ b/tlapbot/__init__.py @@ -15,11 +15,12 @@ def create_app(test_config=None): pass # Prepare config: set db to instance folder, then load default, then - # overwrite it with config.py + # overwrite it with config.py and redeems.py app.config.from_mapping( DATABASE=os.path.join(app.instance_path, "tlapbot.sqlite") ) app.config.from_object('tlapbot.default_config') + app.config.from_object('tlapbot.default_redeems') app.config.from_pyfile('config.py') app.config.from_pyfile('redeems.py') diff --git a/tlapbot/default_redeems.py b/tlapbot/default_redeems.py new file mode 100644 index 0000000..192bcc0 --- /dev/null +++ b/tlapbot/default_redeems.py @@ -0,0 +1,6 @@ +REDEEMS={ + "hydrate": {"price": 60, "type": "list"}, + "lurk": {"price:": 1, "type": "counter"}, + "react": {"price:": 200, "type": "note"}, + "request": {"price:": 100, "type": "note"} +} \ No newline at end of file From 5561621be290f4365e41a129185d15ff4d9cd84f Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 18 Oct 2022 15:24:55 +0200 Subject: [PATCH 21/33] bump version number for today's stream (for real this time) --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 881dbb3..bc65c62 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import find_packages, setup setup( name='tlapbot', - version='0.6.0a2', + version='0.6.0a3', packages=find_packages(), include_package_data=True, install_requires=[ From f0077c56dc853ca3b25ab9cff4b4540c2a605ab2 Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 18 Oct 2022 15:39:04 +0200 Subject: [PATCH 22/33] fix default redeems --- tlapbot/default_redeems.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tlapbot/default_redeems.py b/tlapbot/default_redeems.py index 192bcc0..6956e1c 100644 --- a/tlapbot/default_redeems.py +++ b/tlapbot/default_redeems.py @@ -1,6 +1,6 @@ REDEEMS={ "hydrate": {"price": 60, "type": "list"}, - "lurk": {"price:": 1, "type": "counter"}, - "react": {"price:": 200, "type": "note"}, - "request": {"price:": 100, "type": "note"} + "lurk": {"price": 1, "type": "counter"}, + "react": {"price": 200, "type": "note"}, + "request": {"price": 100, "type": "note"} } \ No newline at end of file From 2cf2050dac2b9582e01b30a28d41f2056bf23d67 Mon Sep 17 00:00:00 2001 From: Lili Date: Thu, 20 Oct 2022 16:10:28 +0200 Subject: [PATCH 23/33] dashboard wrong points workaround dashboard now shows the points of every user with same username --- tlapbot/owncast_helpers.py | 12 ++++-------- tlapbot/owncast_redeem_dashboard.py | 8 ++++---- tlapbot/templates/dashboard.html | 10 ++++++---- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index dc2ee1f..f7c57a8 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -46,18 +46,14 @@ def read_users_points(db, user_id): print("To user:", user_id) -def read_users_points_from_username(db, username): - """Returns None if user doesn't exist, their points otherwise.""" +def read_all_users_with_username(db, username): try: cursor = db.execute( - "SELECT points FROM points WHERE name = ?", + "SELECT name, points FROM points WHERE name = ?", (username,) ) - points = cursor.fetchone() - if points is None: - return None - else: - return points[0] + users = cursor.fetchall() + return users except Error as e: print("Error occured reading points from username:", e.args[0]) print("To user:", username) diff --git a/tlapbot/owncast_redeem_dashboard.py b/tlapbot/owncast_redeem_dashboard.py index db71a79..263f301 100644 --- a/tlapbot/owncast_redeem_dashboard.py +++ b/tlapbot/owncast_redeem_dashboard.py @@ -1,7 +1,7 @@ from flask import render_template, Blueprint, request from tlapbot.db import get_db from tlapbot.owncast_helpers import (pretty_redeem_queue, all_counters, - read_users_points_from_username) + read_all_users_with_username) from datetime import datetime, timezone bp = Blueprint('redeem_dashboard', __name__) @@ -14,13 +14,13 @@ def dashboard(): counters = all_counters(db) username = request.args.get("username") if username is not None: - user_points = read_users_points_from_username(db, username) + users = read_all_users_with_username(db, username) else: - user_points = None + users = [] utc_timezone = timezone.utc return render_template('dashboard.html', queue=queue, counters=counters, username=username, - user_points=user_points, + users=users, utc_timezone=utc_timezone) diff --git a/tlapbot/templates/dashboard.html b/tlapbot/templates/dashboard.html index b6814b1..bebbc1d 100644 --- a/tlapbot/templates/dashboard.html +++ b/tlapbot/templates/dashboard.html @@ -5,17 +5,19 @@ Redeems Dashboard - {% if (username and user_points ) %} + {% if (username and users ) %}
- + + {% for user in users %} - - + + + {% endfor %}
Your points balance:Points balance:
{{ username }} {{ user_points }} {{ user[0] }} {{ user[1] }}
{% endif %} From 15dfd3ccae3cad8d71bfda4947c6f1cc9af9dbd2 Mon Sep 17 00:00:00 2001 From: Lili Date: Thu, 20 Oct 2022 16:24:49 +0200 Subject: [PATCH 24/33] bump version for stream --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index bc65c62..e84f5f5 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import find_packages, setup setup( name='tlapbot', - version='0.6.0a3', + version='0.6.0a4', packages=find_packages(), include_package_data=True, install_requires=[ From a667f746f37d5a95834c4a5c24d459757ddb225c Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 25 Oct 2022 18:31:36 +0200 Subject: [PATCH 25/33] fix taking away points when no note with note rdm --- tlapbot/redeems_handler.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tlapbot/redeems_handler.py b/tlapbot/redeems_handler.py index 6f7f697..2fa717c 100644 --- a/tlapbot/redeems_handler.py +++ b/tlapbot/redeems_handler.py @@ -18,19 +18,21 @@ def handle_redeem(message, user_id): redeem_type = current_app.config['REDEEMS'][redeem]["type"] points = read_users_points(db, user_id) if points is not None and points >= price: - if use_points(db, user_id, price): - if redeem_type == "counter": - add_to_counter(db, redeem) + if redeem_type == "counter": + add_to_counter(db, redeem) + use_points(db, user_id, price) + send_chat(f"{redeem} redeemed for {price} points.") + elif redeem_type == "list": + add_to_redeem_queue(db, user_id, redeem) + use_points(db, user_id, price) + send_chat(f"{redeem} redeemed for {price} points.") + elif redeem_type == "note": + if note is not None: + add_to_redeem_queue(db, user_id, redeem, note) + use_points(db, user_id, price) send_chat(f"{redeem} redeemed for {price} points.") - elif redeem_type == "list": - add_to_redeem_queue(db, user_id, redeem) - send_chat(f"{redeem} redeemed for {price} points.") - elif redeem_type == "note": - if note is not None: - add_to_redeem_queue(db, user_id, redeem, note) - send_chat(f"{redeem} redeemed for {price} points.") - else: - send_chat(f"Cannot redeem {redeem}, no note included.") + else: + send_chat(f"Cannot redeem {redeem}, no note included.") else: send_chat(f"{redeem} not redeemed because of an error.") else: From 932ac9ff71ae27ef74c92be2ea83f9f2cac830dd Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 25 Oct 2022 18:34:48 +0200 Subject: [PATCH 26/33] fix counters not resetting hopefully --- tlapbot/owncast_helpers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index f7c57a8..7e42cae 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -158,7 +158,7 @@ def clear_redeem_queue(db): "DELETE FROM redeem_queue" ) cursor.execute( - """UPDATE counters SET counter_count = 0""" + """UPDATE counters SET count = 0""" ) db.commit() except Error as e: @@ -203,4 +203,5 @@ def whole_redeem_queue(db): def clear_queue_command(): """Remove all redeems from the redeem queue.""" clear_redeem_queue(get_db()) + clear_counters(get_db()) click.echo('Cleared redeem queue.') From 75bc9f161d303da0ada5dbe54f8dd2cf56237cdb Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 25 Oct 2022 18:37:59 +0200 Subject: [PATCH 27/33] fully fixed redeem clear --- tlapbot/owncast_helpers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 7e42cae..93636d7 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -203,5 +203,4 @@ def whole_redeem_queue(db): def clear_queue_command(): """Remove all redeems from the redeem queue.""" clear_redeem_queue(get_db()) - clear_counters(get_db()) click.echo('Cleared redeem queue.') From 95acd70b85a9b77367a4305cd02b55910a75c866 Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 25 Oct 2022 18:58:04 +0200 Subject: [PATCH 28/33] prepare remove duplicate usernames function (NOT TESTED) --- tlapbot/owncast_helpers.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 93636d7..0e17bce 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -198,6 +198,19 @@ def whole_redeem_queue(db): print("Error occured selecting redeem queue:", e.args[0]) +def remove_duplicate_usernames(db, user_id, username): + try: + cursor = db.execute( + """UPDATE points + SET username = NULL + WHERE username = ? AND WHERE NOT id = ?""", + (username, user_id) + ) + db.commit() + except Error as e: + print("Error occured removing duplicate usernames:", e.args[0]) + + @click.command('clear-queue') @with_appcontext def clear_queue_command(): From 6ec8a31100b63996dd2996436bf10882885ef9e8 Mon Sep 17 00:00:00 2001 From: Lili Date: Tue, 25 Oct 2022 19:00:43 +0200 Subject: [PATCH 29/33] add duplicate name removal to webhook catcher --- tlapbot/owncast_webhooks.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index da7badd..5fb4bd0 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -17,10 +17,14 @@ def owncast_webhook(): display_name = data["eventData"]["user"]["displayName"] # CONSIDER: join points for joining stream add_user_to_database(db, user_id, display_name) + if data["eventData"]["user"]["authenticated"]: + remove_duplicate_usernames(db, user_id, display_name) elif data["type"] == "NAME_CHANGE": user_id = data["eventData"]["user"]["id"] new_name = data["eventData"]["newName"] change_display_name(db, user_id, new_name) + if data["eventData"]["user"]["authenticated"]: + remove_duplicate_usernames(db, user_id, display_name) elif data["type"] == "CHAT": user_id = data["eventData"]["user"]["id"] display_name = data["eventData"]["user"]["displayName"] @@ -44,8 +48,11 @@ def owncast_webhook(): send_chat(message) elif "!name_update" in data["eventData"]["body"]: # Forces name update in case bot didn't catch the NAME_CHANGE - # event. Theoretically only needed when bot was off. + # event. Also removes saved usernames from users with same name + # if user is authenticated. change_display_name(db, user_id, display_name) + if data["eventData"]["user"]["authenticated"]: + remove_duplicate_usernames(db, user_id, display_name) elif data["eventData"]["body"].startswith("!"): # TODO: make prefix configurable handle_redeem(data["eventData"]["body"], user_id) return data From 41d9fc46579c54d801e848a45b6437a8692f54bb Mon Sep 17 00:00:00 2001 From: Lili Date: Thu, 3 Nov 2022 15:58:33 +0100 Subject: [PATCH 30/33] fix duplicate name purging, add name addition to account creation --- tlapbot/owncast_helpers.py | 16 ++++++++++++---- tlapbot/owncast_webhooks.py | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) 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__) From 62ddd198349c455827e71aa7cac2c0f8b34b1706 Mon Sep 17 00:00:00 2001 From: Lili Date: Thu, 3 Nov 2022 16:14:31 +0100 Subject: [PATCH 31/33] bump version number for live instance --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e84f5f5..04bbe5b 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import find_packages, setup setup( name='tlapbot', - version='0.6.0a4', + version='0.6.0a5', packages=find_packages(), include_package_data=True, install_requires=[ From 1c295dde291b4391b3a21b2d71cc7e93305fc305 Mon Sep 17 00:00:00 2001 From: Lili Date: Mon, 7 Nov 2022 12:56:11 +0100 Subject: [PATCH 32/33] fix duplicate removal on name change for auth user --- tlapbot/owncast_webhooks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index 45d1bc2..64f8cd2 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -24,7 +24,7 @@ def owncast_webhook(): new_name = data["eventData"]["newName"] change_display_name(db, user_id, new_name) if data["eventData"]["user"]["authenticated"]: - remove_duplicate_usernames(db, user_id, display_name) + remove_duplicate_usernames(db, user_id, new_name) elif data["type"] == "CHAT": user_id = data["eventData"]["user"]["id"] display_name = data["eventData"]["user"]["displayName"] From 60e61728b817789a984f1deabf4ab07b21789097 Mon Sep 17 00:00:00 2001 From: Lili Date: Mon, 7 Nov 2022 12:57:43 +0100 Subject: [PATCH 33/33] make version number not be an alpha --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 04bbe5b..5b34277 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import find_packages, setup setup( name='tlapbot', - version='0.6.0a5', + version='0.6.0', packages=find_packages(), include_package_data=True, install_requires=[