2022-10-12 15:31:34 +02:00
|
|
|
from flask import current_app
|
2022-10-10 15:14:57 +02:00
|
|
|
from tlapbot.db import get_db
|
2023-03-20 17:16:17 +01:00
|
|
|
from tlapbot.owncast_requests import send_chat
|
2022-10-12 15:35:36 +02:00
|
|
|
from tlapbot.owncast_helpers import (use_points, add_to_redeem_queue,
|
2023-03-20 17:16:17 +01:00
|
|
|
add_to_counter, read_users_points, remove_emoji,
|
2023-03-20 14:28:50 +01:00
|
|
|
add_to_milestone, check_apply_milestone_completion, milestone_complete)
|
2022-10-17 18:37:49 +02:00
|
|
|
|
2022-10-10 15:14:57 +02:00
|
|
|
|
2022-10-13 13:29:38 +02:00
|
|
|
def handle_redeem(message, user_id):
|
2022-10-12 15:31:34 +02:00
|
|
|
split_message = message[1:].split(maxsplit=1)
|
|
|
|
redeem = split_message[0]
|
|
|
|
if len(split_message) == 1:
|
|
|
|
note = None
|
|
|
|
else:
|
|
|
|
note = split_message[1]
|
|
|
|
|
2022-11-30 17:45:52 +01:00
|
|
|
if redeem not in current_app.config['REDEEMS']:
|
2022-10-17 18:37:49 +02:00
|
|
|
send_chat("Can't redeem, redeem not found.")
|
2022-11-30 17:45:52 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
db = get_db()
|
|
|
|
price = current_app.config['REDEEMS'][redeem]["price"]
|
|
|
|
redeem_type = current_app.config['REDEEMS'][redeem]["type"]
|
|
|
|
points = read_users_points(db, user_id)
|
|
|
|
|
|
|
|
if not points or points < price:
|
|
|
|
send_chat(f"Can't redeem {redeem}, you don't have enough points.")
|
|
|
|
return
|
|
|
|
|
|
|
|
if redeem_type == "counter":
|
2023-01-05 11:27:10 +01:00
|
|
|
if add_to_counter(db, redeem) and use_points(db, user_id, price):
|
|
|
|
send_chat(f"{redeem} redeemed for {price} points.")
|
|
|
|
else:
|
|
|
|
send_chat(f"Redeeming {redeem} failed.")
|
2022-11-30 17:45:52 +01:00
|
|
|
elif redeem_type == "list":
|
2023-01-09 15:17:45 +01:00
|
|
|
if (add_to_redeem_queue(db, user_id, redeem) and
|
|
|
|
use_points(db, user_id, price)):
|
2023-01-05 11:27:10 +01:00
|
|
|
send_chat(f"{redeem} redeemed for {price} points.")
|
|
|
|
else:
|
|
|
|
send_chat(f"Redeeming {redeem} failed.")
|
2022-11-30 17:45:52 +01:00
|
|
|
elif redeem_type == "note":
|
|
|
|
if not note:
|
|
|
|
send_chat(f"Cannot redeem {redeem}, no note included.")
|
|
|
|
return
|
2023-01-09 15:17:45 +01:00
|
|
|
if (add_to_redeem_queue(db, user_id, redeem, remove_emoji(note)) and
|
|
|
|
use_points(db, user_id, price)):
|
2023-01-05 11:27:10 +01:00
|
|
|
send_chat(f"{redeem} redeemed for {price} points.")
|
|
|
|
else:
|
|
|
|
send_chat(f"Redeeming {redeem} failed.")
|
2023-03-14 14:15:18 +01:00
|
|
|
elif redeem_type == "milestone":
|
2023-03-20 13:23:20 +01:00
|
|
|
if milestone_complete(db, redeem):
|
|
|
|
send_chat(f"Can't redeem {redeem}, that milestone was already completed!")
|
|
|
|
elif not note:
|
2023-03-14 14:15:18 +01:00
|
|
|
send_chat(f"Cannot redeem {redeem}, no amount of points specified.")
|
|
|
|
elif not note.isdigit():
|
|
|
|
send_chat(f"Cannot redeem {redeem}, amount of points is not an integer.")
|
2023-03-14 16:26:04 +01:00
|
|
|
elif int(note) > points:
|
2023-03-15 14:21:15 +01:00
|
|
|
send_chat(f"Can't redeem {redeem}, you don't have enough points.")
|
2023-03-15 13:13:14 +01:00
|
|
|
elif add_to_milestone(db, user_id, redeem, int(note)):
|
2023-03-15 14:11:34 +01:00
|
|
|
send_chat(f"Succesfully donated to {redeem} milestone!")
|
2023-03-20 13:23:20 +01:00
|
|
|
if check_apply_milestone_completion(db, redeem):
|
|
|
|
send_chat(f"Milestone goal {redeem} complete!")
|
2022-11-30 17:45:52 +01:00
|
|
|
else:
|
2023-01-05 11:27:10 +01:00
|
|
|
send_chat(f"{redeem} not redeemed, type of redeem not found.")
|