bot_messages.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from flask import current_app
  2. from ownchatbot.db import get_db, is_cool
  3. from ownchatbot.owncast_com import send_chat
  4. 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
  5. from ownchatbot.user_handlers import spend_points, get_users_points, refund_points, get_all_users_with_user_id
  6. def porps(points): # Pluralize points based on the number of points
  7. if points == 1:
  8. num = 'point'
  9. else:
  10. num = 'points'
  11. return f'{points} {num}'
  12. def mas(time_diff): # Convert time difference decimal number to minutes and seconds
  13. minutes = int(time_diff)
  14. seconds = (time_diff - minutes) * 60
  15. seconds = round(seconds)
  16. if minutes > 1:
  17. mnum = 'minutes'
  18. else:
  19. mnum = 'minute'
  20. if seconds > 1:
  21. snum = 'seconds'
  22. else:
  23. snum = 'second'
  24. if minutes == 0:
  25. return f'{seconds} {snum}'
  26. else:
  27. return f'{minutes} {mnum} and {seconds} {snum}'
  28. def do_reward(message, user_id): # Parse the chat command
  29. db = get_db()
  30. for user in get_all_users_with_user_id(db, user_id):
  31. username = user[1]
  32. prefix = current_app.config['PREFIX']
  33. split_message = message[1:].split(maxsplit=1)
  34. reward = split_message[0]
  35. if len(split_message) == 1: # If it's a goal contribution, split the command and the contribution
  36. contribution = None
  37. else:
  38. contribution = split_message[1]
  39. if reward not in current_app.config['REWARDS']: # Check if it's a command or a reward
  40. send_chat(f'{username}, \"{prefix}{reward}\" is not a chat command or a reward. Check your spelling?')
  41. return
  42. if not is_reward_active(reward): # If reward isn't active, say so
  43. send_chat(f'Sorry, {username}. \"{prefix}{reward}\" is not currently an active reward.')
  44. return
  45. reward_type = current_app.config['REWARDS'][reward]['type']
  46. points = get_users_points(db, user_id)
  47. if reward_type == 'goal': # If it's a goal contribution, do the thing
  48. if int(contribution) > goal_left(db, reward): # If they're contributing more than they need to,
  49. contribution = goal_left(db, reward) # only spend what is needed to reach the goal.
  50. if goal_reached(db, reward):
  51. send_chat(f'{username}, we already completed this goal.')
  52. elif not contribution:
  53. send_chat(f'{username}, you didn\'t tell me how many points you want to contribute.')
  54. elif int(contribution) > points:
  55. send_chat(f'{username}, you don\'t have enough points.')
  56. elif int(contribution) < 0:
  57. send_chat(f'{username}, you can\'t contribute negative points.')
  58. elif int(contribution) == 0:
  59. send_chat(f'{username}, you can\'t contribute zero points.')
  60. elif add_to_goal(db, user_id, reward, int(contribution)):
  61. send_chat(f'{username} contributed {porps(contribution)} to the \"{prefix}{reward}\" goal.')
  62. if was_goal_reached(db, reward):
  63. send_chat(f'\"{prefix}{reward}\" target reached! 🎉')
  64. else:
  65. send_chat(f'Couldn\'t contribute to the \"{prefix}{reward}\" goal for {username}, for some highly technical reason.')
  66. return
  67. price = current_app.config['REWARDS'][reward]['price'] # Do they have enough points?
  68. if not points or points < price:
  69. send_chat(f'{username}, you don\'t have enough points for \"{prefix}{reward}\".')
  70. return
  71. if reward_type == 'vote': # If it's a vote, do the thing
  72. if check_vote(db, reward, user_id): # See if viewer already voted
  73. send_chat(f'{username}, you already voted for \"{prefix}{reward}\".')
  74. else:
  75. if add_to_vote(db, reward, user_id) and spend_points(db, user_id, price):
  76. send_chat(f'{username} voted for \"{prefix}{reward}\" for {porps(price)}.')
  77. else:
  78. send_chat(f'Couldn\'t vote for \"{prefix}{reward}\" for {username}, for some highly technical reason.')
  79. elif reward_type == 'redeem': # If it's a redeem, do the thing
  80. if is_cool(reward)[0]: # Is there an active cool down?
  81. if (add_to_queue(db, user_id, reward) and
  82. spend_points(db, user_id, price)):
  83. send_chat(f'{username} redeemed \"{prefix}{reward}\" for {porps(price)}.')
  84. else:
  85. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}, for some highly technical reason.')
  86. else:
  87. hot_time = is_cool(reward)[1]
  88. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}.<br>That reward has {mas(hot_time)} left to cool down.')
  89. elif reward_type == 'special': # If it's a special, do the thing
  90. if is_cool(reward)[0]: # Is there an active cool down?
  91. script_cmd = current_app.config['REWARDS'][reward]['cmd']
  92. script_ran = run_script(reward, script_cmd)
  93. used_points = spend_points(db, user_id, price)
  94. if (script_ran and used_points):
  95. send_chat(f'{username} redeemed \'{prefix}{reward}\' for {porps(price)}.')
  96. else:
  97. refund_points(db, user_id, price)
  98. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}, for some highly technical reason.')
  99. else:
  100. hot_time = is_cool(reward)[1]
  101. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}.<br>That reward has {mas(hot_time)} left to cool down.')
  102. else: # If we can't find the reward, say so
  103. send_chat(f'\"{prefix}{reward}\", {username}? No such reward.')
  104. def help_message():
  105. prefix = current_app.config['PREFIX']
  106. message = f'You get {current_app.config["POINTS_AWARD"]} points for every {current_app.config["POINTS_INTERVAL"]} seconds you\'re in chat.<br> \
  107. You can see your points, the rewards queue, and other helpful information by clicking on the \"Points Rewards\" button.<br><br> \
  108. Chat commands:<br> \
  109. {prefix}help to see this help message.<br> \
  110. {prefix}points to see your points.<br> \
  111. {prefix}rewards to see a list of currently active rewards.<br>'
  112. send_chat(message)