1
0

donation_handlers.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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'Private donation of ${amount} received from {name} 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. is_public = sub_info[0]
  46. name = sub_info[1]
  47. email = sub_info[2]
  48. amount = sub_info[3]
  49. amount = int(float(amount)) # Convert from str to int
  50. message = sub_info[4]
  51. first_sub = sub_info[5]
  52. tier_name = sub_info[6]
  53. points = sub_points
  54. ids = get_id_by_email(db, email)
  55. if not ids: # If no id found with that email address
  56. if add_email_to_points(db, email, points): # Create empty account with email and points
  57. name = 'Someone'
  58. current_app.logger.info(f'No user with email \"{email}\" found in database, created empty account.')
  59. else: # Grant points to the corresponding id
  60. for id in ids:
  61. if award_chat_points(db, id[0], points): # Grant points
  62. for user in get_all_users_with_user_id(db, id[0]):
  63. name = user[1] # Assign name from points table
  64. current_app.logger.info(f'Awarded user id {id[0]} {porps(points)} for their subscription.')
  65. if is_public:
  66. if not name: # If no name in points table
  67. name = 'Someone'
  68. if first_sub:
  69. message = f'{name} got {porps(points)} for their one month membership on Kofi!'
  70. current_app.logger.info(f'Public subscription received from {name}')
  71. else:
  72. message = f'{name} got {porps(points)} for renewing their membership on Kofi!'
  73. current_app.logger.info(f'Public subscription renewal received from {name}')
  74. send_chat(message) # Send message publicly if a public membership
  75. else:
  76. if not ids:
  77. current_app.logger.info(f'No account to associate with.')
  78. # return True
  79. if not name: # If no name in points table
  80. name = sub_info[1] # Assign name from Kofi response
  81. if first_sub:
  82. message = f'Thanks so much for your subscribing to my Kofi! You\'ve been awarded {porps(points)}!'
  83. current_app.logger.info(f'Private subscription received from {name}')
  84. else:
  85. message = f'Thanks so much for renewing your membership to my Kofi! You\'ve been awarded {porps(points)}!'
  86. current_app.logger.info(f'Private subscription renewal received from {name}')
  87. send_private_chat(id[0], message)
  88. return True
  89. except Exception as aderror:
  90. current_app.logger.error(f'General Exception: {aderror}')
  91. def save_kofi_settings(ksettings_info): # Write settings to kofi.py
  92. settings_file = os.path.join(current_app.instance_path, 'kofi.py')
  93. try:
  94. with open(settings_file, 'w') as f:
  95. f.write(f'KOFI_SETTINGS = {ksettings_info}')
  96. f.close
  97. current_app.config.from_pyfile('kofi.py', silent=True) # Reread kofi.py into the app
  98. except Exception as sks_error:
  99. current_app.logger.error(f'Couldn\'t save kofi.py: {sks_error.args[0]}')
  100. return False
  101. return True
  102. def save_gb_settings(gbsettings_info): # Write settings to givebutter.py
  103. settings_file = os.path.join(current_app.instance_path, 'givebutter.py')
  104. try:
  105. with open(settings_file, 'w') as f:
  106. f.write(f'GB_SETTINGS = {gbsettings_info}')
  107. f.close
  108. current_app.config.from_pyfile('givebutter.py', silent=True) # Reread givebutter.py into the app
  109. except Exception as sgbs_error:
  110. current_app.logger.error(f'Couldn\'t save givebutter.py: {sgbs_error.args[0]}')
  111. return False
  112. return True
  113. def kofi_pngs(): # Create a list of all pngs in the kofi img dir
  114. png_dir = 'static/img/kofi'
  115. png_dir = os.path.join(current_app.root_path, png_dir)
  116. png_files = []
  117. for file in os.listdir(png_dir):
  118. if file.lower().endswith('.png'):
  119. png_files.append(file)
  120. return png_files