123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- from flask import current_app
- from sqlite3 import Error
- from ownchatbot.db import get_db
- from ownchatbot.user_handlers import get_id_by_email, award_chat_points, add_email_to_points, get_all_users_with_user_id
- from ownchatbot.owncast_com import send_chat
- import json
- import os
- def accept_donation(donation_info, tip_points):
- db = get_db()
- is_public = donation_info[0]
- email = donation_info[2]
- amount = donation_info[3]
- amount = int(float(amount)) # Convert from str to int
- message = donation_info[4]
- points = amount * tip_points # Multiply by streamers tip point award
- ids = get_id_by_email(db, email)
- if not ids: # If no id found with that email address
- if add_email_to_points(db, email, points): # Create empty account with email and points
- name = 'Someone'
- current_app.logger.info(f'No user with email \"{email}\" found in database, created empty account.')
- else: # Grant points to the corresponding id
- for id in ids:
- if award_chat_points(db, id[0], points): # Grant points
- for user in get_all_users_with_user_id(db, id[0]):
- name = user[1]
- current_app.logger.info(f'Granted user id {id[0]} {points} points for their ${amount} donation.')
- if is_public:
- message = f'{name} got {points} points for tipping ${amount} on Kofi!'
- current_app.logger.info(f'Public donation of ${amount} received from {name}')
- else:
- message = None
- current_app.logger.info(f'Private donation of ${amount} received from {name}')
- if message is not None: # Only send chat message if it's a public donation
- send_chat(message)
- def save_kofi_settings(ksettings_info): # Write rewards to kofi.py
- settings_file = os.path.join(current_app.instance_path, 'kofi.py')
- try:
- with open(settings_file, 'w') as f:
- f.write(f'KOFI_SETTINGS = {ksettings_info}')
- f.close
- current_app.config.from_pyfile('kofi.py', silent=True) # Reread kofi.py into the app
- except Exception as sks_error:
- current_app.logger.error(f'Couldn\'t save kofi.py: {sks_error.args[0]}')
- return False
-
- return True
- def kofi_pngs(): # Create a list of all pngs in the kofi img dir
- png_dir = 'static/img/kofi'
- png_dir = os.path.join(current_app.root_path, png_dir)
- png_files = []
- for file in os.listdir(png_dir):
- if file.lower().endswith('.png'):
- png_files.append(file)
- return png_files
|