donation_handlers.py 6.2 KB

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