bot_messages.py 8.7 KB

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