From 099525f9ffd52ea6403616708a222b06a666c062 Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 12 Oct 2022 15:31:34 +0200 Subject: [PATCH] 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