add base support for display name changes
This commit is contained in:
		
							parent
							
								
									4a0940b8ff
								
							
						
					
					
						commit
						0bca3d65a8
					
				@ -22,6 +22,7 @@ def create_app(test_config=None):
 | 
			
		||||
 | 
			
		||||
    def proxy_job():
 | 
			
		||||
        with app.app_context():
 | 
			
		||||
            #TODO: make this not give points when stream is offline lol
 | 
			
		||||
            give_points_to_chat(get_db())
 | 
			
		||||
 | 
			
		||||
    points_giver = BackgroundScheduler()
 | 
			
		||||
 | 
			
		||||
@ -21,7 +21,7 @@ def give_points_to_user(db, user_id, points):
 | 
			
		||||
        )
 | 
			
		||||
        db.commit()
 | 
			
		||||
    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)
 | 
			
		||||
 | 
			
		||||
def use_points(db, user_id, points):
 | 
			
		||||
@ -58,7 +58,7 @@ def user_exists(db, user_id):
 | 
			
		||||
        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):
 | 
			
		||||
def add_user_to_database(db, user_id, display_name):
 | 
			
		||||
    try:
 | 
			
		||||
        cursor = db.execute(
 | 
			
		||||
            "SELECT points FROM points WHERE id = ?",
 | 
			
		||||
@ -66,13 +66,24 @@ def add_user_to_database(db, user_id):
 | 
			
		||||
        )
 | 
			
		||||
        if cursor.fetchone() == None:
 | 
			
		||||
            cursor.execute(
 | 
			
		||||
                "INSERT INTO points(id, points) VALUES(?, 10)",
 | 
			
		||||
                (user_id,)
 | 
			
		||||
                "INSERT INTO points(id, name, points) VALUES(?, ?, 10)",
 | 
			
		||||
                (user_id, display_name)
 | 
			
		||||
            )
 | 
			
		||||
        db.commit()
 | 
			
		||||
    except Error as e:
 | 
			
		||||
        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):
 | 
			
		||||
    url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send'
 | 
			
		||||
 | 
			
		||||
@ -11,20 +11,28 @@ def owncast_webhook():
 | 
			
		||||
    db = get_db()
 | 
			
		||||
    if data["type"] == "USER_JOINED":
 | 
			
		||||
        user_id = data["eventData"]["user"]["id"]
 | 
			
		||||
        display_name = data["eventData"]["user"]["displayName"]
 | 
			
		||||
        # 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":
 | 
			
		||||
        user_id = data["eventData"]["user"]["id"]
 | 
			
		||||
        if data["eventData"]["visible"]:
 | 
			
		||||
            display_name = data["eventData"]["user"]["displayName"]
 | 
			
		||||
            print("New chat message:")
 | 
			
		||||
            print(f'from {display_name}:')
 | 
			
		||||
            print(f'{data["eventData"]["body"]}')
 | 
			
		||||
        user_id = data["eventData"]["user"]["id"]  
 | 
			
		||||
            if "!points" in data["eventData"]["body"]:
 | 
			
		||||
                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)
 | 
			
		||||
                message = "{}'s points: {}".format(display_name, points)
 | 
			
		||||
            print(message)
 | 
			
		||||
                send_chat(message)
 | 
			
		||||
            elif "!drink" in data["eventData"]["body"]:
 | 
			
		||||
                points = read_users_points(db, user_id)
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,16 @@
 | 
			
		||||
DROP TABLE IF EXISTS points;
 | 
			
		||||
DROP TABLE IF EXISTS redeem_queue;
 | 
			
		||||
 | 
			
		||||
CREATE TABLE points (
 | 
			
		||||
  id TEXT PRIMARY KEY,
 | 
			
		||||
  name TEXT,
 | 
			
		||||
  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)
 | 
			
		||||
);
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user