2022-08-24 13:39:40 +02:00
|
|
|
from flask import current_app
|
|
|
|
from sqlite3 import Error
|
2023-01-09 15:08:33 +01:00
|
|
|
from re import sub
|
2022-10-17 18:47:30 +02:00
|
|
|
|
2022-09-14 14:05:10 +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:
|
2023-01-05 14:26:18 +01:00
|
|
|
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
|
|
|
|
2022-10-20 16:10:28 +02:00
|
|
|
def read_all_users_with_username(db, username):
|
2022-10-18 14:04:15 +02:00
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2022-10-20 16:10:28 +02:00
|
|
|
"SELECT name, points FROM points WHERE name = ?",
|
2022-10-18 14:04:15 +02:00
|
|
|
(username,)
|
|
|
|
)
|
2022-10-20 16:10:28 +02:00
|
|
|
users = cursor.fetchall()
|
|
|
|
return users
|
2022-10-18 14:04:15 +02:00
|
|
|
except Error as e:
|
2023-01-05 14:26:18 +01:00
|
|
|
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(
|
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:
|
2023-01-05 14:26:18 +01:00
|
|
|
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(
|
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:
|
2023-01-05 14:26:18 +01:00
|
|
|
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}")
|
2022-09-07 16:44:59 +02:00
|
|
|
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:
|
2023-01-05 14:26:18 +01:00
|
|
|
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
|
|
|
|
2022-09-06 18:20:44 +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(
|
2022-11-03 15:58:33 +01:00
|
|
|
"SELECT points, name FROM points WHERE id = ?",
|
2022-08-24 13:39:40 +02:00
|
|
|
(user_id,)
|
|
|
|
)
|
2022-11-03 15:58:33 +01:00
|
|
|
user = cursor.fetchone()
|
|
|
|
if user is None:
|
2022-08-24 13:39:40 +02:00
|
|
|
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
|
|
|
)
|
2022-11-22 11:33:05 +01:00
|
|
|
if user is not None and user[1] is None:
|
2022-11-03 15:58:33 +01:00
|
|
|
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:
|
2023-01-05 14:26:18 +01:00
|
|
|
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-09-06 18:20:44 +02:00
|
|
|
|
2022-10-17 18:47:30 +02:00
|
|
|
|
2022-09-06 18:20:44 +02:00
|
|
|
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:
|
2023-01-05 14:26:18 +01:00
|
|
|
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
|
|
|
|
2022-09-14 14:05:10 +02:00
|
|
|
|
2023-03-14 14:15:18 +01:00
|
|
|
def counter_exists(db, counter_name):
|
2022-10-12 15:30:12 +02:00
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2023-01-04 13:53:46 +01:00
|
|
|
"SELECT count FROM counters WHERE name = ?",
|
2022-10-12 15:30:12 +02:00
|
|
|
(counter_name,)
|
|
|
|
)
|
2023-01-04 13:22:47 +01:00
|
|
|
counter = cursor.fetchone()
|
|
|
|
if counter is None:
|
|
|
|
current_app.logger.warning("Counter not found in database.")
|
|
|
|
current_app.logger.warning("Maybe you forgot to run the refresh-counters CLI command "
|
|
|
|
"after you added a new counter to the config?")
|
|
|
|
return False
|
|
|
|
return True
|
2022-10-12 15:30:12 +02:00
|
|
|
except Error as e:
|
2023-01-05 14:26:18 +01:00
|
|
|
current_app.logger.error(f"Error occured checking if counter exists: {e.args[0]}")
|
|
|
|
current_app.logger.error(f"For counter: {counter_name}")
|
2023-01-04 13:22:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
def add_to_counter(db, counter_name):
|
2023-03-14 14:15:18 +01:00
|
|
|
if counter_exists(db, counter_name):
|
2023-01-04 13:22:47 +01:00
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
|
|
|
"UPDATE counters SET count = count + 1 WHERE name = ?",
|
|
|
|
(counter_name,)
|
|
|
|
)
|
|
|
|
db.commit()
|
2023-01-05 11:27:10 +01:00
|
|
|
return True
|
2023-01-04 13:22:47 +01:00
|
|
|
except Error as e:
|
2023-01-05 14:26:18 +01:00
|
|
|
current_app.logger.error(f"Error occured adding to counter: {e.args[0]}")
|
|
|
|
current_app.logger.error(f"To counter: {counter_name}")
|
2023-01-05 11:27:10 +01:00
|
|
|
return False
|
2022-09-07 16:44:59 +02:00
|
|
|
|
2022-10-17 18:47:30 +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()
|
2023-01-05 11:27:10 +01:00
|
|
|
return True
|
2022-09-07 16:44:59 +02:00
|
|
|
except Error as e:
|
2023-01-05 14:26:18 +01:00
|
|
|
current_app.logger.error(f"Error occured adding to redeem queue: {e.args[0]}")
|
|
|
|
current_app.logger.error(f"To user: {user_id} with redeem: {redeem_name} with note: {note}")
|
2023-03-14 14:15:18 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-03-14 16:26:04 +01:00
|
|
|
def start_milestone(db, redeem_name):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2023-03-15 13:13:14 +01:00
|
|
|
"SELECT progress, goal FROM milestones WHERE name = ?",
|
2023-03-14 16:26:04 +01:00
|
|
|
(redeem_name,)
|
|
|
|
)
|
|
|
|
milestone = cursor.fetchone()
|
|
|
|
current_app.logger.error(f"Milestone: {milestone}")
|
|
|
|
if milestone is None:
|
|
|
|
cursor = db.execute(
|
2023-03-15 13:13:14 +01:00
|
|
|
"INSERT INTO milestones(name, progress, goal) VALUES(?, ?, ?)",
|
2023-03-14 16:26:04 +01:00
|
|
|
(redeem_name, 0, current_app.config['REDEEMS'][redeem_name]['goal'])
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
except Error as e:
|
|
|
|
current_app.logger.error(f"Error occured adding a milestone: {e.args[0]}")
|
|
|
|
|
|
|
|
|
2023-03-15 13:13:14 +01:00
|
|
|
def add_to_milestone(db, user_id, redeem_name, points_donated):
|
2023-03-14 14:15:18 +01:00
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2023-03-15 13:13:14 +01:00
|
|
|
"SELECT progress, goal FROM milestones WHERE name = ?",
|
2023-03-14 14:15:18 +01:00
|
|
|
(redeem_name,)
|
|
|
|
)
|
|
|
|
row = cursor.fetchone()
|
2023-03-15 13:13:14 +01:00
|
|
|
if row is None:
|
|
|
|
current_app.logger.warning("Milestone not found in database.")
|
|
|
|
current_app.logger.warning("Maybe you forgot to run the refresh-milestones CLI command "
|
|
|
|
"after you added a new milestone to the config?")
|
|
|
|
return False
|
2023-03-20 13:23:20 +01:00
|
|
|
progress, goal = row
|
2023-03-15 13:13:14 +01:00
|
|
|
if progress + points_donated > goal:
|
|
|
|
points_donated = goal - progress
|
|
|
|
if use_points(db, user_id, points_donated):
|
|
|
|
cursor = db.execute(
|
|
|
|
"UPDATE milestones SET progress = ? WHERE name = ?",
|
|
|
|
(progress + points_donated, redeem_name)
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
return True
|
2023-03-14 16:26:04 +01:00
|
|
|
except Error as e:
|
2023-03-14 14:15:18 +01:00
|
|
|
current_app.logger.error(f"Error occured updating milestone: {e.args[0]}")
|
|
|
|
return False
|
2022-09-07 16:44:59 +02:00
|
|
|
|
2022-10-17 18:47:30 +02:00
|
|
|
|
2023-03-20 13:23:20 +01:00
|
|
|
def milestone_complete(db, redeem_name):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
|
|
|
"SELECT complete FROM milestones WHERE name = ?",
|
|
|
|
(redeem_name,)
|
|
|
|
)
|
|
|
|
row = cursor.fetchone()
|
|
|
|
if row is None:
|
|
|
|
current_app.logger.warning("Milestone not found in database.")
|
|
|
|
current_app.logger.warning("Maybe you forgot to run the refresh-milestones CLI command "
|
|
|
|
"after you added a new milestone to the config?")
|
|
|
|
return row[0]
|
|
|
|
except Error as e:
|
|
|
|
current_app.logger.error(f"Error occured checking if milestone is complete: {e.args[0]}")
|
|
|
|
|
|
|
|
|
|
|
|
def check_apply_milestone_completion(db, redeem_name):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
|
|
|
"SELECT progress, goal FROM milestones WHERE name = ?",
|
|
|
|
(redeem_name,)
|
|
|
|
)
|
|
|
|
row = cursor.fetchone()
|
|
|
|
if row is None:
|
|
|
|
current_app.logger.warning("Milestone not found in database.")
|
|
|
|
current_app.logger.warning("Maybe you forgot to run the refresh-milestones CLI command "
|
|
|
|
"after you added a new milestone to the config?")
|
|
|
|
progress, goal = row
|
|
|
|
if progress == goal:
|
|
|
|
cursor = db.execute(
|
|
|
|
"UPDATE milestones SET complete = TRUE WHERE name = ?",
|
|
|
|
(redeem_name,)
|
|
|
|
)
|
2023-03-20 17:10:33 +01:00
|
|
|
db.commit()
|
2023-03-20 13:23:20 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
except Error as e:
|
|
|
|
current_app.logger.error(f"Error occured applying milestone completion: {e.args[0]}")
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-03-14 15:42:14 +01:00
|
|
|
def all_milestones(db):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
2023-03-15 13:13:14 +01:00
|
|
|
"""SELECT name, progress, goal FROM milestones"""
|
2023-03-14 15:42:14 +01:00
|
|
|
)
|
|
|
|
return cursor.fetchall()
|
|
|
|
except Error as e:
|
|
|
|
current_app.logger.error(f"Error occured selecting all milestones: {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:
|
2023-01-05 14:26:18 +01:00
|
|
|
current_app.logger.error(f"Error occured selecting all counters: {e.args[0]}")
|
2022-10-12 15:48:57 +02:00
|
|
|
|
2022-10-17 18:47:30 +02:00
|
|
|
|
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:
|
2023-01-05 14:26:18 +01:00
|
|
|
current_app.logger.error(f"Error occured selecting pretty redeem queue: {e.args[0]}")
|
2022-09-09 12:34:31 +02:00
|
|
|
|
2022-10-17 18:47:30 +02:00
|
|
|
|
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:
|
2023-01-05 14:26:18 +01:00
|
|
|
current_app.logger.error(f"Error occured selecting redeem queue: {e.args[0]}")
|
2022-09-26 11:10:48 +02:00
|
|
|
|
2022-10-17 18:47:30 +02:00
|
|
|
|
2022-10-25 18:58:04 +02:00
|
|
|
def remove_duplicate_usernames(db, user_id, username):
|
|
|
|
try:
|
|
|
|
cursor = db.execute(
|
|
|
|
"""UPDATE points
|
2022-11-03 15:58:33 +01:00
|
|
|
SET name = NULL
|
|
|
|
WHERE name = ? AND NOT id = ?""",
|
2022-11-22 11:33:05 +01:00
|
|
|
(username, user_id)
|
2022-10-25 18:58:04 +02:00
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
except Error as e:
|
2023-01-05 14:26:18 +01:00
|
|
|
current_app.logger.error(f"Error occured removing duplicate usernames: {e.args[0]}")
|
2023-01-12 13:20:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
# # # misc. stuff # # #
|
|
|
|
def remove_emoji(message):
|
|
|
|
return sub(
|
|
|
|
r'<img class="emoji" alt="(:.*?:)" title=":.*?:" src="/img/emoji/.*?">',
|
|
|
|
r'\1',
|
|
|
|
message
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-12 14:23:32 +01:00
|
|
|
def is_redeem_active(redeem, active_categories):
|
2023-01-16 13:30:15 +01:00
|
|
|
if "category" in redeem[1] and redeem[1]["category"]:
|
2023-01-13 18:51:44 +01:00
|
|
|
for category in redeem[1]["category"]:
|
|
|
|
if category in active_categories:
|
|
|
|
return True
|
2023-01-12 14:23:32 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def remove_inactive_redeems(redeems, active_categories):
|
2023-01-12 14:40:05 +01:00
|
|
|
return dict(filter(lambda redeem: is_redeem_active(redeem, active_categories),
|
|
|
|
redeems.items()))
|