2022-08-24 13:39:40 +02:00
|
|
|
from flask import Flask,request,json,Blueprint
|
2022-08-17 16:33:40 +02:00
|
|
|
from sqlite3 import Error
|
|
|
|
from tlapbot.db import get_db
|
2022-08-24 15:55:55 +02:00
|
|
|
from tlapbot.owncast_helpers import *
|
2022-08-17 16:33:40 +02:00
|
|
|
|
2022-08-24 13:45:13 +02:00
|
|
|
bp = Blueprint('owncast_webhooks', __name__)
|
2022-08-17 16:33:40 +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()
|
|
|
|
if data["type"] == "USER_JOINED":
|
|
|
|
user_id = data["eventData"]["user"]["id"]
|
2022-09-06 18:20:44 +02:00
|
|
|
display_name = data["eventData"]["user"]["displayName"]
|
2022-08-24 15:35:16 +02:00
|
|
|
# CONSIDER: join points for joining stream
|
2022-09-06 18:20:44 +02:00
|
|
|
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"]
|
|
|
|
change_display_name(db, user_id, new_name)
|
2022-08-17 16:33:40 +02:00
|
|
|
elif data["type"] == "CHAT":
|
2022-09-06 18:20:44 +02:00
|
|
|
if data["eventData"]["visible"]:
|
2022-09-09 12:41:13 +02:00
|
|
|
user_id = data["eventData"]["user"]["id"]
|
2022-09-06 18:20:44 +02:00
|
|
|
display_name = data["eventData"]["user"]["displayName"]
|
|
|
|
print("New chat message:")
|
|
|
|
print(f'from {display_name}:')
|
|
|
|
print(f'{data["eventData"]["body"]}')
|
2022-09-09 12:35:20 +02:00
|
|
|
if "!help" in data["eventData"]["body"]:
|
|
|
|
message = """Tlapbot commands:
|
|
|
|
!points to see your points.
|
|
|
|
!drink to redeem a pitíčko for 60 points.
|
|
|
|
That's it for now."""
|
|
|
|
send_chat(message)
|
|
|
|
elif "!points" in data["eventData"]["body"]:
|
2022-09-06 18:20:44 +02:00
|
|
|
if not user_exists(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)
|
|
|
|
send_chat(message)
|
|
|
|
elif "!drink" in data["eventData"]["body"]:
|
|
|
|
points = read_users_points(db, user_id)
|
2022-09-24 21:20:44 +02:00
|
|
|
if points is not None and points >= 60:
|
2022-09-07 16:44:59 +02:00
|
|
|
if use_points(db, user_id, 60):
|
|
|
|
add_to_redeem_queue(db, user_id, "drink")
|
2022-09-09 12:35:20 +02:00
|
|
|
send_chat("Pitíčko redeemed for 60 points.")
|
|
|
|
else:
|
|
|
|
send_chat("Pitíčko not redeemed because of an error.")
|
|
|
|
else:
|
|
|
|
send_chat("Can't redeem pitíčko, you don't have enough points.")
|
2022-09-09 12:41:13 +02:00
|
|
|
elif "!name_update" in data["eventData"]["body"]:
|
|
|
|
# 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)
|
2022-09-09 12:35:20 +02:00
|
|
|
elif "!clear" in data["eventData"]["body"]:
|
|
|
|
clear_redeem_queue(db)
|
2022-09-07 16:44:59 +02:00
|
|
|
elif "!queue" in data["eventData"]["body"]:
|
2022-09-09 12:35:20 +02:00
|
|
|
queue = pretty_redeem_queue(db)
|
|
|
|
print("timestamp | redeem | redeemer")
|
2022-09-07 16:44:59 +02:00
|
|
|
for row in queue:
|
2022-09-09 12:35:20 +02:00
|
|
|
print(row[0], " ", row[1], " ", row[2])
|
2022-09-06 18:20:44 +02:00
|
|
|
# else: # DEBUG: give points for message
|
|
|
|
# give_points_to_chat(db)
|
2022-08-17 17:44:25 +02:00
|
|
|
return data
|