add base support for display name changes

This commit is contained in:
Lili (Tlapka) 2022-09-06 18:20:44 +02:00
parent 4a0940b8ff
commit 0bca3d65a8
4 changed files with 57 additions and 27 deletions

View File

@ -22,6 +22,7 @@ def create_app(test_config=None):
def proxy_job(): def proxy_job():
with app.app_context(): with app.app_context():
#TODO: make this not give points when stream is offline lol
give_points_to_chat(get_db()) give_points_to_chat(get_db())
points_giver = BackgroundScheduler() points_giver = BackgroundScheduler()

View File

@ -21,7 +21,7 @@ def give_points_to_user(db, user_id, points):
) )
db.commit() db.commit()
except Error as e: 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) print("To user:", user_id, " amount of points:", points)
def use_points(db, user_id, points): def use_points(db, user_id, points):
@ -58,7 +58,7 @@ def user_exists(db, user_id):
print("To user:", user_id) print("To user:", user_id)
""" Adds a new user to the database. Does nothing if user is already in.""" """ 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: try:
cursor = db.execute( cursor = db.execute(
"SELECT points FROM points WHERE id = ?", "SELECT points FROM points WHERE id = ?",
@ -66,13 +66,24 @@ def add_user_to_database(db, user_id):
) )
if cursor.fetchone() == None: if cursor.fetchone() == None:
cursor.execute( cursor.execute(
"INSERT INTO points(id, points) VALUES(?, 10)", "INSERT INTO points(id, name, points) VALUES(?, ?, 10)",
(user_id,) (user_id, display_name)
) )
db.commit() db.commit()
except Error as e: except Error as e:
print("Error occured adding user to db:", e.args[0]) 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): def send_chat(message):
url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send' url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send'

View File

@ -11,27 +11,35 @@ def owncast_webhook():
db = get_db() db = get_db()
if data["type"] == "USER_JOINED": if data["type"] == "USER_JOINED":
user_id = data["eventData"]["user"]["id"] 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"] display_name = data["eventData"]["user"]["displayName"]
print("New chat message:") # CONSIDER: join points for joining stream
print(f'from {display_name}:') add_user_to_database(db, user_id, display_name)
print(f'{data["eventData"]["body"]}') elif data["type"] == "NAME_CHANGE":
user_id = data["eventData"]["user"]["id"] user_id = data["eventData"]["user"]["id"]
if "!points" in data["eventData"]["body"]: new_name = data["eventData"]["newName"]
if not user_exists(db, user_id): old_names = data["eventData"]["user"]["previousNames"]
add_user_to_database(db, user_id) print("Changing name:")
points = read_users_points(db, user_id) print(f'from {old_names} to {new_name}')
message = "{}'s points: {}".format(display_name, points) change_display_name(db, user_id, new_name)
print(message) elif data["type"] == "CHAT":
send_chat(message) user_id = data["eventData"]["user"]["id"]
elif "!drink" in data["eventData"]["body"]: if data["eventData"]["visible"]:
points = read_users_points(db, user_id) display_name = data["eventData"]["user"]["displayName"]
if points is not None: print("New chat message:")
if points > 60: print(f'from {display_name}:')
use_points(db, user_id, 60) print(f'{data["eventData"]["body"]}')
send_chat("Enjoy your DRINK........... sips") if "!points" in data["eventData"]["body"]:
# else: # DEBUG: give points for message if not user_exists(db, user_id):
# give_points_to_chat(db) 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 return data

View File

@ -1,6 +1,16 @@
DROP TABLE IF EXISTS points; DROP TABLE IF EXISTS points;
DROP TABLE IF EXISTS redeem_queue;
CREATE TABLE points ( CREATE TABLE points (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
name TEXT,
points INTEGER 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)
); );