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,20 +11,28 @@ 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"]
display_name = data["eventData"]["user"]["displayName"]
# CONSIDER: join points for joining stream # CONSIDER: join points for joining stream
add_user_to_database(db, user_id) 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": elif data["type"] == "CHAT":
user_id = data["eventData"]["user"]["id"]
if data["eventData"]["visible"]:
display_name = data["eventData"]["user"]["displayName"] display_name = data["eventData"]["user"]["displayName"]
print("New chat message:") print("New chat message:")
print(f'from {display_name}:') print(f'from {display_name}:')
print(f'{data["eventData"]["body"]}') print(f'{data["eventData"]["body"]}')
user_id = data["eventData"]["user"]["id"]
if "!points" in data["eventData"]["body"]: if "!points" in data["eventData"]["body"]:
if not user_exists(db, user_id): if not user_exists(db, user_id):
add_user_to_database(db, user_id) add_user_to_database(db, user_id, display_name)
points = read_users_points(db, user_id) points = read_users_points(db, user_id)
message = "{}'s points: {}".format(display_name, points) message = "{}'s points: {}".format(display_name, points)
print(message)
send_chat(message) send_chat(message)
elif "!drink" in data["eventData"]["body"]: elif "!drink" in data["eventData"]["body"]:
points = read_users_points(db, user_id) points = read_users_points(db, user_id)

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)
);