2022-11-24 16:06:01 +01:00
|
|
|
from flask import Flask, request, json, Blueprint, current_app
|
2022-08-17 16:33:40 +02:00
|
|
|
from tlapbot.db import get_db
|
2023-03-20 17:16:17 +01:00
|
|
|
from tlapbot.owncast_requests import send_chat
|
2022-10-10 15:14:57 +02:00
|
|
|
from tlapbot.owncast_helpers import (add_user_to_database, change_display_name,
|
2023-03-20 17:16:17 +01:00
|
|
|
read_users_points, remove_duplicate_usernames)
|
2022-11-07 17:30:40 +01:00
|
|
|
from tlapbot.help_message import send_help
|
2022-10-10 15:14:57 +02:00
|
|
|
from tlapbot.redeems_handler import handle_redeem
|
2022-08-17 16:33:40 +02:00
|
|
|
|
2022-11-07 17:30:40 +01:00
|
|
|
|
2022-08-24 13:45:13 +02:00
|
|
|
bp = Blueprint('owncast_webhooks', __name__)
|
2022-08-17 16:33:40 +02:00
|
|
|
|
2022-10-17 18:47:30 +02:00
|
|
|
|
|
|
|
@bp.route('/owncastWebhook', methods=['POST'])
|
2022-08-24 13:45:13 +02:00
|
|
|
def owncast_webhook():
|
2022-08-17 16:33:40 +02:00
|
|
|
data = request.json
|
|
|
|
db = get_db()
|
2022-12-05 11:36:25 +01:00
|
|
|
|
|
|
|
# Make sure user is in db before doing anything else.
|
|
|
|
if data["type"] in ["CHAT", "NAME_CHANGED", "USER_JOINED"]:
|
2022-08-17 16:33:40 +02:00
|
|
|
user_id = data["eventData"]["user"]["id"]
|
2022-09-06 18:20:44 +02:00
|
|
|
display_name = data["eventData"]["user"]["displayName"]
|
|
|
|
add_user_to_database(db, user_id, display_name)
|
2022-12-05 11:36:25 +01:00
|
|
|
|
|
|
|
if data["type"] == "USER_JOINED":
|
2022-10-25 19:00:43 +02:00
|
|
|
if data["eventData"]["user"]["authenticated"]:
|
|
|
|
remove_duplicate_usernames(db, user_id, display_name)
|
2022-09-06 18:20:44 +02:00
|
|
|
elif data["type"] == "NAME_CHANGE":
|
|
|
|
user_id = data["eventData"]["user"]["id"]
|
|
|
|
new_name = data["eventData"]["newName"]
|
|
|
|
change_display_name(db, user_id, new_name)
|
2022-10-25 19:00:43 +02:00
|
|
|
if data["eventData"]["user"]["authenticated"]:
|
2022-11-07 12:56:11 +01:00
|
|
|
remove_duplicate_usernames(db, user_id, new_name)
|
2022-08-17 16:33:40 +02:00
|
|
|
elif data["type"] == "CHAT":
|
2023-03-13 15:22:40 +01:00
|
|
|
if not current_app.config['PASSIVE']:
|
|
|
|
prefix = current_app.config['PREFIX']
|
|
|
|
user_id = data["eventData"]["user"]["id"]
|
|
|
|
display_name = data["eventData"]["user"]["displayName"]
|
|
|
|
current_app.logger.debug(f'New chat message from {display_name}:')
|
2024-02-04 18:09:36 +01:00
|
|
|
current_app.logger.debug(f'{data["eventData"]["rawBody"]}')
|
2023-03-13 15:22:40 +01:00
|
|
|
current_app.logger.debug(f'{data["eventData"]["body"]}')
|
2024-02-04 18:09:36 +01:00
|
|
|
if data["eventData"]["rawBody"].startswith(f"{prefix}help"):
|
2023-03-13 15:22:40 +01:00
|
|
|
send_help()
|
2024-02-04 18:09:36 +01:00
|
|
|
elif data["eventData"]["rawBody"].startswith(f"{prefix}points"):
|
2023-03-13 15:22:40 +01:00
|
|
|
points = read_users_points(db, user_id)
|
|
|
|
if points is None:
|
|
|
|
send_chat("Error reading points.")
|
|
|
|
else:
|
|
|
|
send_chat(f"{display_name}'s points: {points}")
|
2024-02-04 18:09:36 +01:00
|
|
|
elif data["eventData"]["rawBody"].startswith(f"{prefix}name_update"):
|
2023-03-13 15:22:40 +01:00
|
|
|
# Forces name update in case bot didn't catch the NAME_CHANGE
|
|
|
|
# event. Also removes saved usernames from users with same name
|
|
|
|
# if user is authenticated.
|
|
|
|
change_display_name(db, user_id, display_name)
|
|
|
|
if data["eventData"]["user"]["authenticated"]:
|
|
|
|
remove_duplicate_usernames(db, user_id, display_name)
|
2024-02-04 18:09:36 +01:00
|
|
|
elif data["eventData"]["rawBody"].startswith(prefix):
|
|
|
|
handle_redeem(data["eventData"]["rawBody"], user_id)
|
2022-10-17 18:47:30 +02:00
|
|
|
return data
|