浏览代码

Added newFollow route, to award points to new followers

deadtom 2 周之前
父节点
当前提交
f864251977
共有 1 个文件被更改,包括 25 次插入9 次删除
  1. 25 9
      ownchatbot/webhooks.py

+ 25 - 9
ownchatbot/webhooks.py

@@ -1,7 +1,7 @@
 from flask import Flask, request, json, Blueprint, current_app, render_template, jsonify, request, g
 from ownchatbot.db import get_db
 from ownchatbot.owncast_com import send_chat, send_private_chat
-from ownchatbot.user_handlers import add_user_to_points, change_name, get_users_points, remove_duplicates, get_email_code, set_email_code
+from ownchatbot.user_handlers import add_user_to_points, change_name, get_users_points, remove_duplicates, get_email_code, set_email_code, award_chat_points, user_in_points
 from ownchatbot.bot_messages import do_reward, help_message
 from ownchatbot.reward_handlers import all_active_goals, all_active_votes, all_active_rewards
 from ownchatbot.kofi_handlers import accept_donation, accept_sub
@@ -56,13 +56,13 @@ def kofiHook():
                                 current_app.logger.info(f'{from_name} <{email}> renewed their {tier_name} tier membership.')
                             else:
                                 current_app.logger.info(f'{from_name} <{email}> renewed their membership.')
-                        
+
                         sub_info = [is_public, from_name, email, amount, message, first_sub, tier_name]
                         sub_points = current_app.config['KOFI_SETTINGS']['sub_points']
                         accept_sub(sub_info, sub_points)
                     else:
                         current_app.logger.info(f'Kofi membership received, but subscriptions are not enabled. Doing nothing.')
-                        
+
                 return jsonify({'status': 'success'}), 200
             else:
                 current_app.logger.info(f'Token invalid. Rejecting.')
@@ -82,7 +82,7 @@ def chat_hook():
         if add_user_to_points(db, user_id, display_name, authed):
             current_app.logger.debug(f'Added/updated {user_id} database.')
         current_app.logger.debug(f'{display_name}/{user_id}: {data["eventData"]}')  # Log all chat messages
-            
+
     if data['type'] == 'USER_JOINED':  # Do username house cleaning when a viewer joins
         if data['eventData']['user']['authenticated']:
             remove_duplicates(db, user_id, display_name)
@@ -96,18 +96,18 @@ def chat_hook():
         user_id = data['eventData']['user']['id']
         display_name = data['eventData']['user']['displayName']
         current_app.logger.info(f'{display_name}/{user_id}: {data["eventData"]["rawBody"]}')  # Log all chat messages
-        
+
         lowercase_msg = data['eventData']['rawBody'].lower()  # Convert body to lower case to match reward case
         if lowercase_msg.startswith(f'{prefix}help'):  # Send the help message
             help_message(user_id)
-            
+
         elif lowercase_msg.startswith(f'{prefix}points'):  # Get the viewer's current points
             points = get_users_points(db, user_id)
             if points is None:
                 send_private_chat(user_id, f'{display_name}, couldn\'t get your points, for some highly technical reason.')
             else:
                 send_private_chat(user_id, f'{display_name}, you have {points} points.')
-            
+
         elif lowercase_msg.startswith(f'{prefix}reg_mail'):  # Generate a code to verify users account for email registration
             if current_app.config['KOFI_INTEGRATION']:
                 mail_reg_code = get_email_code(db, user_id)
@@ -119,7 +119,7 @@ def chat_hook():
                         send_private_chat(user_id, f'{display_name}, your code is {mail_reg_code}. Enter it into the form on the Stream Rewards Info page, with your email address, to enable Kofi perks!')
             else:
                 send_chat(f'{display_name}, Kofi integration is not enabled on this stream.')                
-                
+
         elif lowercase_msg.startswith(f'{prefix}rewards'):  # Send rewards list
             if current_app.config['REWARDS']:
                 rewards_msg = f'Currently active rewards:'
@@ -138,7 +138,7 @@ def chat_hook():
             else:
                 rewards_msg = 'There are currently no active rewards.'
             send_private_chat(user_id, rewards_msg)
-                    
+
         elif lowercase_msg.startswith(f'{prefix}'):  # Send to handle rewards
             do_reward(lowercase_msg, user_id)
 
@@ -158,3 +158,19 @@ def votes():
     db = get_db()
     return render_template('votes.html',
                            votes=all_active_votes(db))
+
+
+@ocb.route('/newFollow', methods=['POST'])
+def new_follow():
+    data = request.json
+    db = get_db()
+    user_id = ['eventData']['id']
+    display_name_ = ['eventData']['name']
+    follow_points = current_app.config['FOLLOW_POINTS']
+    if user_in_points(db, user_id):  # If the user is in the database
+        award_chat_points(db, user_id, follow_points)  # Award points
+        current_app.logger.info(f'Awarded {display_name}/{user_id} {follow_points} for following.')
+        return jsonify({'status': 'success'}), 200
+    else:  # If not, don't award points. Need to find out how this is handled by Owncast
+        current_app.logger.info(f'{display_name}/{user_id} not in database. Not awarding points.')
+        return jsonify({'status': 'success'}), 200