Tlapbot/tlapbot/owncast_helpers.py

131 lines
4.0 KiB
Python
Raw Normal View History

2022-08-24 13:39:40 +02:00
from flask import current_app
from sqlite3 import Error
from re import sub
2022-10-17 18:47:30 +02:00
# # # db stuff # # #
2022-08-24 15:35:16 +02:00
def read_users_points(db, user_id):
2022-10-18 14:09:35 +02:00
"""Errors out if user doesn't exist."""
2022-08-24 15:35:16 +02:00
try:
cursor = db.execute(
"SELECT points FROM points WHERE id = ?",
(user_id,)
)
return cursor.fetchone()[0]
except Error as e:
current_app.logger.error(f"Error occured reading points: {e.args[0]}")
current_app.logger.error(f"To user: {user_id}")
2022-08-24 15:35:16 +02:00
2022-10-17 18:47:30 +02:00
def read_all_users_with_username(db, username):
2022-10-18 14:04:15 +02:00
try:
cursor = db.execute(
"SELECT name, points FROM points WHERE name = ?",
2022-10-18 14:04:15 +02:00
(username,)
)
users = cursor.fetchall()
return users
2022-10-18 14:04:15 +02:00
except Error as e:
current_app.logger.error(f"Error occured reading points by username: {e.args[0]}")
current_app.logger.error(f"To everyone with username: {username}")
2022-10-18 14:04:15 +02:00
2022-11-22 11:33:05 +01:00
2022-08-24 15:35:16 +02:00
def give_points_to_user(db, user_id, points):
try:
db.execute(
"UPDATE points SET points = points + ? WHERE id = ?",
(points, user_id,)
2022-08-24 15:35:16 +02:00
)
db.commit()
except Error as e:
current_app.logger.error(f"Error occured giving points: {e.args[0]}")
current_app.logger.error(f"To user: {user_id} amount of points: {points}")
2022-08-24 15:55:46 +02:00
2022-10-17 18:47:30 +02:00
2022-08-24 15:55:46 +02:00
def use_points(db, user_id, points):
try:
db.execute(
"UPDATE points SET points = points - ? WHERE id = ?",
(points, user_id,)
2022-08-24 15:55:46 +02:00
)
db.commit()
return True
2022-08-24 15:55:46 +02:00
except Error as e:
current_app.logger.error(f"Error occured using points: {e.args[0]}")
current_app.logger.error(f"From user: {user_id} amount of points: {points}")
return False
2022-08-24 15:35:16 +02:00
2022-10-17 18:47:30 +02:00
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,)
)
2022-10-17 18:47:30 +02:00
if cursor.fetchone() is None:
2022-08-24 13:39:40 +02:00
return False
return True
except Error as e:
current_app.logger.error(f"Error occured checking if user exists: {e.args[0]}")
current_app.logger.error(f"To user: {user_id}")
2022-08-24 13:39:40 +02:00
2022-10-17 18:47:30 +02:00
def add_user_to_database(db, user_id, display_name):
2022-10-17 18:47:30 +02:00
""" Adds a new user to the database. Does nothing if user is already in."""
2022-08-24 13:39:40 +02:00
try:
cursor = db.execute(
"SELECT points, name FROM points WHERE id = ?",
2022-08-24 13:39:40 +02:00
(user_id,)
)
user = cursor.fetchone()
if user is None:
2022-08-24 13:39:40 +02:00
cursor.execute(
"INSERT INTO points(id, name, points) VALUES(?, ?, 10)",
(user_id, display_name)
2022-08-24 13:39:40 +02:00
)
2022-11-22 11:33:05 +01:00
if user is not None and user[1] is None:
cursor.execute(
"""UPDATE points
SET name = ?
WHERE id = ?""",
(display_name, user_id)
)
2022-08-24 13:39:40 +02:00
db.commit()
except Error as e:
current_app.logger.error(f"Error occured adding user to db: {e.args[0]}")
current_app.logger.error(f"To user id: {user_id}, with display name: {display_name}")
2022-10-17 18:47:30 +02:00
def change_display_name(db, user_id, new_name):
try:
cursor = db.execute(
"UPDATE points SET name = ? WHERE id = ?",
(new_name, user_id)
)
db.commit()
except Error as e:
current_app.logger.error(f"Error occured changing display name: {e.args[0]}")
current_app.logger.error(f"To user id: {user_id}, with display name: {new_name}")
2022-08-24 13:39:40 +02:00
def remove_duplicate_usernames(db, user_id, username):
try:
cursor = db.execute(
"""UPDATE points
SET name = NULL
WHERE name = ? AND NOT id = ?""",
2022-11-22 11:33:05 +01:00
(username, user_id)
)
db.commit()
except Error as e:
current_app.logger.error(f"Error occured removing duplicate usernames: {e.args[0]}")
2023-01-12 13:20:28 +01:00
# # # misc. stuff # # #
# This is now unused since rawBody attribute of the webhook now returns cleaned-up emotes.
2023-01-12 13:20:28 +01:00
def remove_emoji(message):
return sub(
r'<img class="emoji" alt="(:.*?:)" title=":.*?:" src="/img/emoji/.*?">',
r'\1',
message
)