bot_messages.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. import os
  7. def porps(points): # Pluralize points based on the number of points
  8. if points == 1:
  9. num = 'point'
  10. else:
  11. num = 'points'
  12. return f'{points} {num}'
  13. def mas(time_diff): # Convert time difference decimal number to minutes and seconds
  14. minutes = int(time_diff)
  15. seconds = (time_diff - minutes) * 60
  16. seconds = round(seconds)
  17. if minutes > 1:
  18. mnum = 'minutes'
  19. else:
  20. mnum = 'minute'
  21. if seconds > 1:
  22. snum = 'seconds'
  23. else:
  24. snum = 'second'
  25. if minutes == 0:
  26. return f'{seconds} {snum}'
  27. else:
  28. return f'{minutes} {mnum} and {seconds} {snum}'
  29. def do_reward(message, user_id): # Parse the chat command
  30. db = get_db()
  31. for user in get_all_users_with_user_id(db, user_id):
  32. username = user[1]
  33. prefix = current_app.config['PREFIX']
  34. split_message = message[1:].split(maxsplit=1)
  35. reward = split_message[0]
  36. if len(split_message) == 1: # If it's a goal contribution, split the command and the contribution
  37. contribution = None
  38. else:
  39. contribution = split_message[1]
  40. if reward not in current_app.config['REWARDS']: # Check if it's a command or a reward
  41. send_chat(f'{username}, \"{prefix}{reward}\" is not a chat command or a reward. Check your spelling?')
  42. return
  43. if not is_reward_active(reward): # If reward isn't active, say so
  44. send_chat(f'Sorry, {username}. \"{prefix}{reward}\" is not currently an active reward.')
  45. return
  46. reward_type = current_app.config['REWARDS'][reward]['type']
  47. points = get_users_points(db, user_id)
  48. if reward_type == 'goal': # If it's a goal contribution, do the thing
  49. if not contribution:
  50. send_chat(f'{username}, you didn\'t tell me how many points you want to contribute.')
  51. return
  52. if goal_reached(db, reward):
  53. send_chat(f'{username}, we already completed this goal.')
  54. return
  55. if int(contribution) > goal_left(db, reward): # If they're contributing more than they need to
  56. current_app.logger.info(f'{username} contributed more than what was needed to reach the target.')
  57. contribution = goal_left(db, reward) # only spend what is needed to reach the goal.
  58. if int(contribution) > points:
  59. send_chat(f'{username}, you don\'t have that many points.')
  60. elif int(contribution) < 0:
  61. send_chat(f'{username}, you can\'t contribute negative points.')
  62. elif int(contribution) == 0:
  63. send_chat(f'{username}, you can\'t contribute zero points.')
  64. elif add_to_goal(db, user_id, reward, int(contribution)):
  65. send_chat(f'{username} contributed {porps(contribution)} to the \"{prefix}{reward}\" goal.')
  66. if was_goal_reached(db, reward):
  67. send_chat(f'\"{prefix}{reward}\" target reached! 🎉')
  68. else:
  69. send_chat(f'Couldn\'t contribute to the \"{prefix}{reward}\" goal for {username}, for some highly technical reason.')
  70. return
  71. price = current_app.config['REWARDS'][reward]['price'] # Do they have enough points?
  72. if not points or points < price:
  73. send_chat(f'{username}, you don\'t have enough points for \"{prefix}{reward}\".')
  74. return
  75. if reward_type == 'vote': # If it's a vote, do the thing
  76. if check_vote(db, reward, user_id): # See if viewer already voted
  77. send_chat(f'{username}, you already voted for \"{prefix}{reward}\".')
  78. else:
  79. if add_to_vote(db, reward, user_id) and spend_points(db, user_id, price):
  80. send_chat(f'{username} voted for \"{prefix}{reward}\" for {porps(price)}.')
  81. else:
  82. send_chat(f'Couldn\'t vote for \"{prefix}{reward}\" for {username}, for some highly technical reason.')
  83. elif reward_type == 'redeem': # If it's a redeem, do the thing
  84. if is_cool(reward)[0]: # Is there an active cool down?
  85. if (add_to_queue(db, user_id, reward) and
  86. spend_points(db, user_id, price)):
  87. send_chat(f'{username} redeemed \"{prefix}{reward}\" for {porps(price)}.')
  88. else:
  89. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}, for some highly technical reason.')
  90. else:
  91. hot_time = is_cool(reward)[1]
  92. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}.<br>That reward has {mas(hot_time)} left to cool down.')
  93. elif reward_type == 'special': # If it's a special, do the thing
  94. if is_cool(reward)[0]: # Is there an active cool down?
  95. script_cmd = current_app.config['REWARDS'][reward]['cmd']
  96. script_ran = run_script(reward, script_cmd)
  97. used_points = spend_points(db, user_id, price)
  98. if (script_ran and used_points):
  99. send_chat(f'{username} redeemed \'{prefix}{reward}\' for {porps(price)}.')
  100. else:
  101. if refund_points(db, user_id, price):
  102. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}, for some highly technical reason.')
  103. else:
  104. hot_time = is_cool(reward)[1]
  105. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}.<br>That reward has {mas(hot_time)} left to cool down.')
  106. else: # If we can't find the reward, say so
  107. send_chat(f'\"{prefix}{reward}\", {username}? No such reward.')
  108. def help_message():
  109. prefix = current_app.config['PREFIX']
  110. kofi_settings = current_app.config['KOFI_SETTINGS']
  111. kofi_integration = current_app.config['KOFI_INTEGRATION']
  112. message = f'You get {current_app.config["POINTS_AWARD"]} points for every {current_app.config["POINTS_INTERVAL"]} seconds you\'re in chat.<br> \
  113. You can see your points, the rewards queue, and other helpful information by clicking on the \"Points Rewards\" button.<br><br> \
  114. Chat commands:<br> \
  115. {prefix}help to see this help message.<br> \
  116. {prefix}points to see your points.<br> \
  117. {prefix}rewards to see a list of currently active rewards.<br>'
  118. if kofi_integration and kofi_settings['tips']:
  119. message = f'{message}<br>\
  120. Kofi is enabled! Once you\'ve authenticated with Owncast, you\'ll recieve\
  121. {kofi_settings["tip_points"]} points for every dollar you tip on Kofi.'
  122. send_chat(message)
  123. def save_announce(announce_dict): # Write rewards to announce.py
  124. announce_file = os.path.join(current_app.instance_path, 'announce.py')
  125. new_announce = f"ANNOUNCEMENTS = {announce_dict['ANNOUNCEMENTS']} # List of announcements\n\n\
  126. ANNOUNCE_ENABLE = {announce_dict['ANNOUNCE_ENABLE']} # Enable announcements\n\
  127. ANNOUNCE_INTERVAL = {announce_dict['ANNOUNCE_INTERVAL']} # How long, in minutes, between points announcements"
  128. try:
  129. with open(announce_file, 'w') as f:
  130. f.write(new_announce)
  131. except Exception as saerror:
  132. current_app.logger.error(f'Couldn\'t save announce.py: {saerror.args[0]}')
  133. return False
  134. current_app.config.from_pyfile('announce.py', silent=True)
  135. return True