2022-08-24 13:39:40 +02:00
|
|
|
from flask import current_app
|
|
|
|
import requests
|
|
|
|
from sqlite3 import Error
|
2022-09-26 11:10:48 +02:00
|
|
|
import click
|
|
|
|
from flask.cli import with_appcontext
|
2022-09-26 16:51:22 +02:00
|
|
|
from tlapbot.db import get_db
|
2022-09-14 14:05:10 +02:00
|
|
|
|
|
|
|
# # # requests stuff # # #
|
|
|
|
def is_stream_live():
|
|
|
|
url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/status'
|
|
|
|
r = requests.post(url)
|
|
|
|
print(r.json()["online"])
|
|
|
|
return r.json()["online"]
|
|
|
|
|
|
|
|
def give_points_to_chat(db):
|
|
|
|
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)
|
2022-09-30 12:01:33 +02:00
|
|
|
unique_users = list(set(map(lambda user_object: user_object["user"]["id"], r.json())))
|
|
|
|
for user_id in unique_users:
|
2022-09-14 14:05:10 +02:00
|
|
|
give_points_to_user(db,
|
2022-09-30 12:01:33 +02:00
|
|
|
user_id,
|
2022-09-14 14:05:10 +02:00
|
|
|
current_app.config['POINTS_AMOUNT_GIVEN'])
|
|
|
|
|
|
|
|
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})
|
|
|
|
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
# # # db stuff # # #
|
2022-08-24 15:35:16 +02:00
|
|
|
def read_users_points(db, user_id):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
|
|
|
"SELECT points FROM points WHERE id = ?",
|
|
|
|
(user_id,)
|
|
|
|
)
|
|
|
|
return cursor.fetchone()[0]
|
|
|
|
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(
|
2022-10-12 15:30:12 +02:00
|
|
|
"UPDATE points SET points = points + ? WHERE id = ?",
|
|
|
|
(points, user_id,)
|
2022-08-24 15:35:16 +02:00
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
except Error as e:
|
2022-09-06 18:20:44 +02:00
|
|
|
print("Error occured giving points:", e.args[0])
|
2022-08-24 15:55:46 +02:00
|
|
|
print("To user:", user_id, " amount of points:", points)
|
|
|
|
|
|
|
|
def use_points(db, user_id, points):
|
|
|
|
try:
|
|
|
|
db.execute(
|
2022-10-12 15:30:12 +02:00
|
|
|
"UPDATE points SET points = points - ? WHERE id = ?",
|
|
|
|
(points, user_id,)
|
2022-08-24 15:55:46 +02:00
|
|
|
)
|
|
|
|
db.commit()
|
2022-09-07 16:44:59 +02:00
|
|
|
return True
|
2022-08-24 15:55:46 +02:00
|
|
|
except Error as e:
|
|
|
|
print("Error occured using points:", e.args[0])
|
|
|
|
print("From user:", user_id, " amount of points:", points)
|
2022-09-07 16:44:59 +02:00
|
|
|
return False
|
2022-08-24 15:35:16 +02:00
|
|
|
|
|
|
|
def user_exists(db, user_id):
|
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)
|
|
|
|
|
2022-08-24 15:35:16 +02:00
|
|
|
""" Adds a new user to the database. Does nothing if user is already in."""
|
2022-09-06 18:20:44 +02:00
|
|
|
def add_user_to_database(db, user_id, display_name):
|
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(
|
2022-09-06 18:20:44 +02:00
|
|
|
"INSERT INTO points(id, name, points) VALUES(?, ?, 10)",
|
|
|
|
(user_id, display_name)
|
2022-08-24 13:39:40 +02:00
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
except Error as e:
|
|
|
|
print("Error occured adding user to db:", e.args[0])
|
2022-09-06 18:20:44 +02:00
|
|
|
print("To user:", user_id, display_name)
|
|
|
|
|
|
|
|
def change_display_name(db, user_id, new_name):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2022-10-12 15:30:12 +02:00
|
|
|
"UPDATE points SET name = ? WHERE id = ?",
|
|
|
|
(new_name, user_id)
|
|
|
|
)
|
2022-09-06 18:20:44 +02:00
|
|
|
db.commit()
|
|
|
|
except Error as e:
|
|
|
|
print("Error occured changing display name:", e.args[0])
|
|
|
|
print("To user:", user_id, new_name)
|
2022-08-24 13:39:40 +02:00
|
|
|
|
2022-09-14 14:05:10 +02:00
|
|
|
|
2022-10-12 15:30:12 +02:00
|
|
|
def add_to_counter(db, counter_name):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
|
|
|
"UPDATE counters SET count = count + 1 WHERE name = ?",
|
|
|
|
(counter_name,)
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
except Error as e:
|
|
|
|
print("Error occured adding to counter:", e.args[0])
|
|
|
|
print("To counter:", counter_name)
|
2022-09-07 16:44:59 +02:00
|
|
|
|
2022-10-12 15:30:12 +02:00
|
|
|
def add_to_redeem_queue(db, user_id, redeem_name, note=None):
|
2022-09-07 16:44:59 +02:00
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2022-10-12 15:30:12 +02:00
|
|
|
"INSERT INTO redeem_queue(redeem, redeemer_id, note) VALUES(?, ?, ?)",
|
|
|
|
(redeem_name, user_id, note)
|
2022-09-07 16:44:59 +02:00
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
except Error as e:
|
|
|
|
print("Error occured adding to redeem queue:", e.args[0])
|
2022-10-12 15:30:12 +02:00
|
|
|
print("To user:", user_id, " with redeem:", redeem_name, "with note:", note)
|
2022-09-07 16:44:59 +02:00
|
|
|
|
2022-09-09 12:34:31 +02:00
|
|
|
def clear_redeem_queue(db):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2022-10-12 15:30:12 +02:00
|
|
|
"DELETE FROM redeem_queue"
|
|
|
|
)
|
|
|
|
cursor.execute(
|
|
|
|
"""UPDATE counters SET counter_count = 0"""
|
|
|
|
)
|
2022-09-09 12:34:31 +02:00
|
|
|
db.commit()
|
|
|
|
except Error as e:
|
|
|
|
print("Error occured deleting redeem queue:", e.args[0])
|
|
|
|
|
2022-10-12 15:48:57 +02:00
|
|
|
def all_counters(db):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
|
|
|
"""SELECT counters.name, counters.count FROM counters"""
|
|
|
|
)
|
|
|
|
return cursor.fetchall()
|
|
|
|
except Error as e:
|
|
|
|
print("Error occured selecting all counters:", e.args[0])
|
|
|
|
|
2022-09-09 12:34:31 +02:00
|
|
|
def pretty_redeem_queue(db):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2022-10-12 15:30:12 +02:00
|
|
|
"""SELECT redeem_queue.created, redeem_queue.redeem, redeem_queue.note, points.name
|
2022-09-09 12:34:31 +02:00
|
|
|
FROM redeem_queue
|
|
|
|
INNER JOIN points
|
|
|
|
on redeem_queue.redeemer_id = points.id"""
|
|
|
|
)
|
|
|
|
return cursor.fetchall()
|
|
|
|
except Error as e:
|
|
|
|
print("Error occured selecting pretty redeem queue:", e.args[0])
|
|
|
|
|
2022-09-07 16:44:59 +02:00
|
|
|
def whole_redeem_queue(db):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2022-10-12 15:30:12 +02:00
|
|
|
"SELECT * from redeem_queue"
|
|
|
|
)
|
2022-09-07 16:44:59 +02:00
|
|
|
return cursor.fetchall()
|
|
|
|
except Error as e:
|
2022-09-26 11:10:48 +02:00
|
|
|
print("Error occured selecting redeem queue:", e.args[0])
|
|
|
|
|
|
|
|
@click.command('clear-queue')
|
|
|
|
@with_appcontext
|
|
|
|
def clear_queue_command():
|
|
|
|
"""Remove all redeems from the redeem queue."""
|
|
|
|
clear_redeem_queue(get_db())
|
|
|
|
click.echo('Cleared redeem queue.')
|