2022-08-24 13:39:40 +02:00
|
|
|
from flask import current_app
|
|
|
|
import requests
|
|
|
|
from sqlite3 import Error
|
|
|
|
|
2022-08-24 13:45:13 +02:00
|
|
|
|
|
|
|
def user_exists(user_id, db):
|
2022-08-24 13:39:40 +02:00
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
|
|
|
"SELECT points FROM points WHERE id = ?",
|
|
|
|
(user_id,)
|
|
|
|
)
|
|
|
|
if cursor.fetchone() == None:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
except Error as e:
|
|
|
|
print("Error occured checking if user exists:", e.args[0])
|
|
|
|
print("To user:", user_id)
|
|
|
|
|
|
|
|
# only adds user if they aren't already in.
|
2022-08-24 13:45:13 +02:00
|
|
|
def add_user_to_database(user_id, db):
|
2022-08-24 13:39:40 +02:00
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
|
|
|
"SELECT points FROM points WHERE id = ?",
|
|
|
|
(user_id,)
|
|
|
|
)
|
|
|
|
if cursor.fetchone() == None:
|
|
|
|
cursor.execute(
|
|
|
|
"INSERT INTO points(id, points) VALUES(?, 10)",
|
|
|
|
(user_id,)
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
except Error as e:
|
|
|
|
print("Error occured adding user to db:", e.args[0])
|
|
|
|
print("To user:", user_id)
|
|
|
|
|
2022-08-24 13:45:13 +02:00
|
|
|
def send_chat(message): # TODO: url to constant?
|
2022-08-24 13:39:40 +02:00
|
|
|
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})
|
|
|
|
return r.json()
|