kofi_handlers.py 5.4 KB

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