bot_messages.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, 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. 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. wmr = was_milestone_reached(db, reward)
  67. if wmr:
  68. send_chat(f'{wmr} milestone reached!')
  69. if was_goal_reached(db, reward):
  70. send_chat(f'\"{prefix}{reward}\" target reached! 🎉')
  71. else:
  72. send_chat(f'Couldn\'t contribute to the \"{prefix}{reward}\" goal for {username}, for some highly technical reason.')
  73. return
  74. price = current_app.config['REWARDS'][reward]['price'] # Do they have enough points?
  75. if not points or points < price:
  76. send_chat(f'{username}, you don\'t have enough points for \"{prefix}{reward}\".')
  77. return
  78. if reward_type == 'vote': # If it's a vote, do the thing
  79. if check_vote(db, reward, user_id): # See if viewer already voted
  80. send_chat(f'{username}, you already voted for \"{prefix}{reward}\".')
  81. else:
  82. if add_to_vote(db, reward, user_id) and spend_points(db, user_id, price):
  83. send_chat(f'{username} voted for \"{prefix}{reward}\" for {porps(price)}.')
  84. else:
  85. send_chat(f'Couldn\'t vote for \"{prefix}{reward}\" for {username}, for some highly technical reason.')
  86. elif reward_type == 'redeem': # If it's a redeem, do the thing
  87. if is_cool(reward)[0]: # Is there an active cool down?
  88. if (add_to_queue(db, user_id, reward) and
  89. spend_points(db, user_id, price)):
  90. send_chat(f'{username} redeemed \"{prefix}{reward}\" for {porps(price)}.')
  91. else:
  92. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}, for some highly technical reason.')
  93. else:
  94. hot_time = is_cool(reward)[1]
  95. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}.<br>That reward has {mas(hot_time)} left to cool down.')
  96. elif reward_type == 'special': # If it's a special, do the thing
  97. if is_cool(reward)[0]: # Is there an active cool down?
  98. script_cmd = current_app.config['REWARDS'][reward]['cmd']
  99. script_ran = run_script(reward, script_cmd)
  100. used_points = spend_points(db, user_id, price)
  101. if (script_ran and used_points):
  102. send_chat(f'{username} redeemed \'{prefix}{reward}\' for {porps(price)}.')
  103. else:
  104. if refund_points(db, user_id, price):
  105. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}, for some highly technical reason.')
  106. else:
  107. hot_time = is_cool(reward)[1]
  108. send_chat(f'Couldn\'t redeem \"{prefix}{reward}\"for {username}.<br>That reward has {mas(hot_time)} left to cool down.')
  109. else: # If we can't find the reward, say so
  110. send_chat(f'\"{prefix}{reward}\", {username}? No such reward.')
  111. def help_message():
  112. prefix = current_app.config['PREFIX']
  113. kofi_settings = current_app.config['KOFI_SETTINGS']
  114. kofi_integration = current_app.config['KOFI_INTEGRATION']
  115. message = f'You get {current_app.config["POINTS_AWARD"]} points for every {current_app.config["POINTS_INTERVAL"]} seconds you\'re in chat.<br> \
  116. You can see your points, the rewards queue, and other helpful information by clicking on the \"Points Rewards\" button.<br><br> \
  117. Chat commands:<br> \
  118. {prefix}help to see this help message.<br> \
  119. {prefix}points to see your points.<br> \
  120. {prefix}rewards to see a list of currently active rewards.<br>'
  121. if kofi_integration and kofi_settings['tips']:
  122. message = f'{message}<br>\
  123. Kofi is enabled! Once you\'ve authenticated with Owncast, you\'ll recieve\
  124. {kofi_settings["tip_points"]} points for every dollar you tip on Kofi.'
  125. send_chat(message)
  126. def save_announce(announce_dict): # Write rewards to announce.py
  127. announce_file = os.path.join(current_app.instance_path, 'announce.py')
  128. new_announce = f"ANNOUNCEMENTS = {announce_dict['ANNOUNCEMENTS']} # List of announcements\n\n\
  129. ANNOUNCE_ENABLE = {announce_dict['ANNOUNCE_ENABLE']} # Enable announcements\n\
  130. ANNOUNCE_INTERVAL = {announce_dict['ANNOUNCE_INTERVAL']} # How long, in minutes, between points announcements"
  131. try:
  132. with open(announce_file, 'w') as f:
  133. f.write(new_announce)
  134. except Exception as saerror:
  135. current_app.logger.error(f'Couldn\'t save announce.py: {saerror.args[0]}')
  136. return False
  137. current_app.config.from_pyfile('announce.py', silent=True)
  138. return True