1
0

donation_handlers.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from flask import current_app
  2. from sqlite3 import Error
  3. from ownchatbot.db import get_db
  4. from ownchatbot.user_handlers import get_id_by_email, award_chat_points, add_email_to_points, get_all_users_with_user_id
  5. from ownchatbot.owncast_com import send_chat, send_private_chat
  6. from ownchatbot.bot_messages import porps
  7. from ownchatbot.reward_handlers import save_alerts
  8. import json
  9. import os
  10. def accept_donation(donation_info, donation_points, donation_service):
  11. try:
  12. db = get_db()
  13. alerts_dict = current_app.config['ALERTS']
  14. is_public = donation_info[0]
  15. email = donation_info[2]
  16. amount = donation_info[3]
  17. amount = int(float(amount)) # Convert from str to int
  18. message = donation_info[4]
  19. points = amount * donation_points # Multiply by streamers donation points award
  20. ids = get_id_by_email(db, email)
  21. if not ids: # If no id found with that email address
  22. if add_email_to_points(db, email, points): # Create empty account with email and points
  23. name = 'Someone'
  24. alerts_dict['gb_name'] = 'New Donation!'
  25. current_app.logger.info(f'No user with email \"{email}\" found in database, created empty account.')
  26. else: # Grant points to the corresponding id
  27. for id in ids:
  28. if award_chat_points(db, id[0], points): # Grant points
  29. for user in get_all_users_with_user_id(db, id[0]):
  30. name = user[1]
  31. alerts_dict['gb_name'] = name
  32. if not name:
  33. name = 'Someone'
  34. alerts_dict['gb_name'] = 'New Donation!'
  35. current_app.logger.info(f'Granted user id {id[0]} {porps(points)} for their ${amount} donation.')
  36. if is_public:
  37. message = f'{name} got {porps(points)} for donating ${amount} on {donation_service}!'
  38. alerts_dict['gb_name'] = name
  39. current_app.logger.info(f'Public donation of ${amount} received from {name} via {donation_service}. Granted {porps(points)}.')
  40. else:
  41. message = f'We got an anonymous donation of ${amount}!'
  42. alerts_dict['gb_name'] = 'New Donation!'
  43. current_app.logger.info(f'Anonymous donation of ${amount} received via {donation_service}')
  44. if donation_service == 'GiveButter': # If a GiveButter donation, update alerts.py, to trigger alert
  45. save_alerts(alerts_dict)
  46. send_chat(message) # Send message to chat
  47. return True
  48. except Exception as aderror:
  49. current_app.logger.error(f'A general exception occurred in accept_donation: {aderror}')
  50. def accept_kofi_sub(sub_info, sub_points):
  51. try:
  52. db = get_db()
  53. donation_service = "Ko-fi"
  54. is_public = sub_info[0]
  55. name = sub_info[1]
  56. email = sub_info[2]
  57. amount = sub_info[3]
  58. amount = int(float(amount)) # Convert from str to int
  59. message = sub_info[4]
  60. first_sub = sub_info[5]
  61. tier_name = sub_info[6]
  62. points = sub_points
  63. ids = get_id_by_email(db, email)
  64. if not ids: # If no id found with that email address
  65. if add_email_to_points(db, email, points): # Create empty account with email and points
  66. name = 'Someone'
  67. current_app.logger.info(f'No user with email \"{email}\" found in database, created empty Owncast account.')
  68. else: # Grant points to the corresponding id
  69. for id in ids:
  70. if award_chat_points(db, id[0], points): # Grant points
  71. for user in get_all_users_with_user_id(db, id[0]):
  72. name = user[1] # Assign name from points table
  73. current_app.logger.info(f'Awarded user id {id[0]} {porps(points)} for their subscription.')
  74. if is_public:
  75. if not name: # If no name in points table
  76. name = 'Someone'
  77. if first_sub: # Kofi-only field
  78. message = f'{name} got {porps(points)} for their one month membership on {donation_service}!'
  79. current_app.logger.info(f'Public subscription received from {name}')
  80. else:
  81. message = f'{name} got {porps(points)} for renewing their membership on {donation_service}!'
  82. current_app.logger.info(f'Anonymous {donation_service} subcription renewal received.')
  83. send_chat(message) # Send message publicly if a public membership
  84. else:
  85. if not ids:
  86. current_app.logger.info(f'No Owncast account to associate {donation_service} subscription with.')
  87. # return True
  88. if not name: # If no name in points table
  89. name = sub_info[1] # Assign name from Kofi response
  90. if first_sub:
  91. message = f'Thanks so much for your subscribing to my {donation_service}! You\'ve been awarded {porps(points)}!'
  92. current_app.logger.info(f'Anonymous {donation_service} subscription received.')
  93. else:
  94. message = f'Thanks so much for renewing your membership to my {donation_service}! You\'ve been awarded {porps(points)}!'
  95. current_app.logger.info(f'Anonymous {donation_service} subscription renewal received.')
  96. send_private_chat(id[0], message)
  97. return True
  98. except Exception as aderror:
  99. current_app.logger.error(f'A general exception occurred in accept_kofi_sub: {aderror}')
  100. def save_kofi_settings(ksettings_info): # Write settings to kofi.py
  101. settings_file = os.path.join(current_app.instance_path, 'kofi.py')
  102. try:
  103. with open(settings_file, 'w') as f:
  104. f.write(f'KOFI_SETTINGS = {ksettings_info}')
  105. f.close
  106. current_app.config.from_pyfile('kofi.py', silent=True) # Reread kofi.py into the app
  107. except Exception as sks_error:
  108. current_app.logger.error(f'Couldn\'t save kofi.py: {sks_error}')
  109. return False
  110. return True
  111. def save_gb_settings(gbsettings_info): # Write settings to givebutter.py
  112. settings_file = os.path.join(current_app.instance_path, 'givebutter.py')
  113. try:
  114. with open(settings_file, 'w') as f:
  115. f.write(f'GB_SETTINGS = {gbsettings_info}')
  116. f.close
  117. current_app.config.from_pyfile('givebutter.py', silent=True) # Reread givebutter.py into the app
  118. except Exception as sgbs_error:
  119. current_app.logger.error(f'Couldn\'t save givebutter.py: {sgbs_error}')
  120. return False
  121. return True