123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- from flask import current_app
- from ownchatbot.db import get_db, is_cool
- from ownchatbot.owncast_com import send_chat
- from ownchatbot.reward_handlers import run_script, add_to_queue, add_to_vote, add_to_goal, was_goal_reached, goal_reached, is_reward_active, check_vote, all_active_goals, goal_left
- from ownchatbot.user_handlers import spend_points, get_users_points, refund_points, get_all_users_with_user_id
- def porps(points): # Pluralize points based on the number of points
- if points == 1:
- num = 'point'
- else:
- num = 'points'
- return f'{points} {num}'
- def mas(time_diff): # Convert time difference decimal number to minutes and seconds
- minutes = int(time_diff)
- seconds = (time_diff - minutes) * 60
- seconds = round(seconds)
- if minutes > 1:
- mnum = 'minutes'
- else:
- mnum = 'minute'
- if seconds > 1:
- snum = 'seconds'
- else:
- snum = 'second'
- if minutes == 0:
- return f'{seconds} {snum}'
- else:
- return f'{minutes} {mnum} and {seconds} {snum}'
- def do_reward(message, user_id): # Parse the chat command
- db = get_db()
- for user in get_all_users_with_user_id(db, user_id):
- username = user[1]
- prefix = current_app.config['PREFIX']
- split_message = message[1:].split(maxsplit=1)
- reward = split_message[0]
- if len(split_message) == 1: # If it's a goal contribution, split the command and the contribution
- contribution = None
- else:
- contribution = split_message[1]
- if reward not in current_app.config['REWARDS']: # Check if it's a command or a reward
- send_chat(f'{username}, \"{prefix}{reward}\" is not a chat command or a reward. Check your spelling?')
- return
- if not is_reward_active(reward): # If reward isn't active, say so
- send_chat(f'Sorry, {username}. \"{prefix}{reward}\" is not currently an active reward.')
- return
- reward_type = current_app.config['REWARDS'][reward]['type']
- points = get_users_points(db, user_id)
- if reward_type == 'goal': # If it's a goal contribution, do the thing
- if int(contribution) > goal_left(db, reward): # If they're contributing more than they need to,
- contribution = goal_left(db, reward) # only spend what is needed to reach the goal.
- if goal_reached(db, reward):
- send_chat(f'{username}, we already completed this goal.')
- elif not contribution:
- send_chat(f'{username}, you didn\'t tell me how many points you want to contribute.')
- elif int(contribution) > points:
- send_chat(f'{username}, you don\'t have enough points.')
- elif int(contribution) < 0:
- send_chat(f'{username}, you can\'t contribute negative points.')
- elif int(contribution) == 0:
- send_chat(f'{username}, you can\'t contribute zero points.')
- elif add_to_goal(db, user_id, reward, int(contribution)):
- send_chat(f'{username} contributed {porps(contribution)} to the \"{prefix}{reward}\" goal.')
- if was_goal_reached(db, reward):
- send_chat(f'\"{prefix}{reward}\" target reached! 🎉')
- else:
- send_chat(f'Couldn\'t contribute to the \"{prefix}{reward}\" goal for {username}, for some highly technical reason.')
- return
- price = current_app.config['REWARDS'][reward]['price'] # Do they have enough points?
- if not points or points < price:
- send_chat(f'{username}, you don\'t have enough points for \"{prefix}{reward}\".')
- return
- if reward_type == 'vote': # If it's a vote, do the thing
- if check_vote(db, reward, user_id): # See if viewer already voted
- send_chat(f'{username}, you already voted for \"{prefix}{reward}\".')
- else:
- if add_to_vote(db, reward, user_id) and spend_points(db, user_id, price):
- send_chat(f'{username} voted for \"{prefix}{reward}\" for {porps(price)}.')
- else:
- send_chat(f'Couldn\'t vote for \"{prefix}{reward}\" for {username}, for some highly technical reason.')
-
- elif reward_type == 'redeem': # If it's a redeem, do the thing
- if is_cool(reward)[0]: # Is there an active cool down?
- if (add_to_queue(db, user_id, reward) and
- spend_points(db, user_id, price)):
- send_chat(f'{username} redeemed \"{prefix}{reward}\" for {porps(price)}.')
- else:
- send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}, for some highly technical reason.')
- else:
- hot_time = is_cool(reward)[1]
- send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}.<br>That reward has {mas(hot_time)} left to cool down.')
-
- elif reward_type == 'special': # If it's a special, do the thing
- if is_cool(reward)[0]: # Is there an active cool down?
- script_cmd = current_app.config['REWARDS'][reward]['cmd']
- script_ran = run_script(reward, script_cmd)
- used_points = spend_points(db, user_id, price)
- if (script_ran and used_points):
- send_chat(f'{username} redeemed \'{prefix}{reward}\' for {porps(price)}.')
- else:
- refund_points(db, user_id, price)
- send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}, for some highly technical reason.')
- else:
- hot_time = is_cool(reward)[1]
- send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}.<br>That reward has {mas(hot_time)} left to cool down.')
-
- else: # If we can't find the reward, say so
- send_chat(f'\"{prefix}{reward}\", {username}? No such reward.')
- def help_message():
- prefix = current_app.config['PREFIX']
- message = f'You get {current_app.config["POINTS_AWARD"]} points for every {current_app.config["POINTS_INTERVAL"]} seconds you\'re in chat.<br> \
- You can see your points, the rewards queue, and other helpful information by clicking on the \"Points Rewards\" button.<br><br> \
- Chat commands:<br> \
- {prefix}help to see this help message.<br> \
- {prefix}points to see your points.<br> \
- {prefix}rewards to see a list of currently active rewards.<br>'
- send_chat(message)
|