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, send_private_chat from ownchatbot.bot_messages import porps import json import os def accept_donation(donation_info, donation_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 * donation_points # Multiply by streamers donation points 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]} {porps(points)} for their ${amount} donation.') if is_public: message = f'{name} got {porps(points)} for donating ${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 accept_sub(sub_info, sub_points): db = get_db() is_public = sub_info[0] name = sub_info[1] email = sub_info[2] amount = sub_info[3] amount = int(float(amount)) # Convert from str to int message = sub_info[4] first_sub = sub_info[5] tier_name = sub_info[6] points = sub_points 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] # Assign name from points table current_app.logger.info(f'Awarded user id {id[0]} {porps(points)} for their subscription.') if is_public: if not name: # If no name in points table name = 'Someone' if first_sub: message = f'{name} got {porps(points)} for their one month membership on Kofi!' current_app.logger.info(f'Public subscription received from {name}') else: message = f'{name} got {porps(points)} for renewing their membership on Kofi!' current_app.logger.info(f'Public subscription renewal received from {name}') send_chat(message) # Send message publicly if a public membership else: if not name: # If no name in points table name = sub_info[1] # Assign name from Kofi response if first_sub: message = f'Thanks so much for your subscribing to my Kofi! You\'ve been awarded {porps(points)}!' current_app.logger.info(f'Private subscription received from {name}') else: message = f'Thanks so much for renewing your membership to my Kofi! You\'ve been awarded {porps(points)}!' current_app.logger.info(f'Private subscription renewal received from {name}') send_private_chat(id[0], 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