moving code out into helper functions

This commit is contained in:
Lili (Tlapka) 2022-08-24 15:35:16 +02:00
parent 63b4c73cda
commit c5f31946d3
2 changed files with 44 additions and 28 deletions

View File

@ -2,8 +2,38 @@ from flask import current_app
import requests
from sqlite3 import Error
def read_users_points(db, user_id):
try:
cursor = db.execute(
"SELECT points FROM points WHERE id = ?",
(user_id,)
)
return cursor.fetchone()[0]
def user_exists(user_id, db):
except Error as e:
print("Error occured reading points:", e.args[0])
print("To user:", user_id)
def give_points_to_user(db, user_id, points):
try:
db.execute(
"UPDATE points SET points = points + ? WHERE id = ?",
(points, user_id,)
)
db.commit()
except Error as e:
print("Error occured giving DEBUG points:", e.args[0])
print("To user:", user_id)
def give_points_to_chat(db):
points_given = 10
url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/clients'
headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']}
r = requests.post(url, headers=headers)
for user_object in r.json():
give_points_to_user(db, user_object["user"]["id"], points_given)
def user_exists(db, user_id):
try:
cursor = db.execute(
"SELECT points FROM points WHERE id = ?",
@ -16,8 +46,8 @@ def user_exists(user_id, db):
print("Error occured checking if user exists:", e.args[0])
print("To user:", user_id)
# only adds user if they aren't already in.
def add_user_to_database(user_id, db):
""" Adds a new user to the database. Does nothing if user is already in."""
def add_user_to_database(db, user_id):
try:
cursor = db.execute(
"SELECT points FROM points WHERE id = ?",
@ -33,7 +63,7 @@ def add_user_to_database(user_id, db):
print("Error occured adding user to db:", e.args[0])
print("To user:", user_id)
def send_chat(message): # TODO: url to constant?
def send_chat(message):
url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send'
headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']}
r = requests.post(url, headers=headers, json={"body": message})

View File

@ -1,7 +1,7 @@
from flask import Flask,request,json,Blueprint
from sqlite3 import Error
from tlapbot.db import get_db
from tlapbot.owncast_helpers import user_exists, add_user_to_database, send_chat
from tlapbot.owncast_helpers import user_exists, add_user_to_database, send_chat, give_points_to_user, read_users_points
bp = Blueprint('owncast_webhooks', __name__)
@ -11,7 +11,8 @@ def owncast_webhook():
db = get_db()
if data["type"] == "USER_JOINED":
user_id = data["eventData"]["user"]["id"]
add_user_to_database(user_id, db)
# CONSIDER: join points for joining stream
add_user_to_database(db, user_id)
elif data["type"] == "CHAT":
display_name = data["eventData"]["user"]["displayName"]
print("New chat message:")
@ -19,27 +20,12 @@ def owncast_webhook():
print(f'{data["eventData"]["body"]}')
user_id = data["eventData"]["user"]["id"]
if "!points" in data["eventData"]["body"]:
if not user_exists(user_id, db):
add_user_to_database(user_id, db)
try:
cursor = db.execute(
"SELECT points FROM points WHERE id = ?",
(user_id,)
)
message = "{}'s points: {}".format(display_name, cursor.fetchone()[0])
if not user_exists(db, user_id):
add_user_to_database(db, user_id)
points = read_users_points(db, user_id)
message = "{}'s points: {}".format(display_name, points)
print(message)
send_chat(message)
except Error as e:
print("Error occured reading points:", e.args[0])
print("To user:", user_id)
else: # DEBUG: give points for message
try:
db.execute(
"UPDATE points SET points = points + 10 WHERE id = ?",
(user_id,)
)
db.commit()
except Error as e:
print("Error occured giving DEBUG points:", e.args[0])
print("To user:", user_id)
give_points_to_user(db, user_id, 10)
return data