base customizable redeems code skeletons
UNTESTED but i think i got it all
This commit is contained in:
parent
1faee35169
commit
099525f9ff
|
@ -22,12 +22,24 @@ 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')
|
||||
@with_appcontext
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
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
|
|
@ -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)
|
||||
);
|
Loading…
Reference in New Issue