kofi_handlers.py 4.7 KB

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