Quellcode durchsuchen

Got alert browser sources working for milestones and goals

deadtom vor 1 Woche
Ursprung
Commit
9a780f6db4

+ 0 - 6
ownchatbot/__init__.py

@@ -8,12 +8,6 @@ from apscheduler.schedulers.background import BackgroundScheduler
 
 current_index = 0
 
-followers = []
-donations = []
-subscribers = []
-rgoal = []
-rmilestone = []
-
 
 def create_app(test_config=None):
     app = Flask(__name__, instance_relative_config=True)

+ 8 - 6
ownchatbot/bot_messages.py

@@ -1,10 +1,9 @@
 from flask import current_app
 from ownchatbot.db import get_db, is_cool
 from ownchatbot.owncast_com import send_chat, send_private_chat
-from ownchatbot.reward_handlers import run_script, add_to_queue, add_to_vote, add_to_goal, was_goal_reached, goal_reached, is_reward_active, check_vote, all_active_goals, goal_left, was_milestone_reached
+from ownchatbot.reward_handlers import run_script, add_to_queue, add_to_vote, add_to_goal, was_goal_reached, goal_reached, is_reward_active, check_vote, all_active_goals, goal_left, was_milestone_reached, save_alerts
 from ownchatbot.user_handlers import spend_points, get_users_points, refund_points, get_all_users_with_user_id
 import os
-from ownchatbot import followers, donations, subscribers, rgoal, rmilestone
 
 
 def porps(points):  # Pluralize points based on the number of points
@@ -35,11 +34,10 @@ def mas(time_diff):  # Convert time difference decimal number to minutes and sec
 
 def do_reward(message, user_id):  # Parse the chat command
     db = get_db()
-    global rmilestone
-    global rgoal
     for user in get_all_users_with_user_id(db, user_id):
         username = user[1]
     prefix = current_app.config['PREFIX']
+    alerts_dict = current_app.config['ALERTS']
     split_message = message[1:].split(maxsplit=1)
     reward = split_message[0]
     if len(split_message) == 1:  # If it's a goal contribution, split the command and the contribution
@@ -77,10 +75,14 @@ def do_reward(message, user_id):  # Parse the chat command
             send_chat(f'{username} contributed {porps(contribution)} to the \"{prefix}{reward}\" goal.')
             wmr = was_milestone_reached(db, reward)
             if wmr:
-                rmilestone = [username, wmr]
+                alerts_dict['m_name'] = username
+                alerts_dict['m_reward'] = wmr
+                save_alerts(alerts_dict)
                 send_chat(f'{wmr} milestone reached!')
             if was_goal_reached(db, reward):
-                rgoal = [username, reward]
+                alerts_dict['g_name'] = username
+                alerts_dict['g_reward'] = reward
+                save_alerts(alerts_dict)
                 send_chat(f'\"{prefix}{reward}\" target reached! 🎉')
         else:
             send_private_chat(user_id, f'Couldn\'t contribute to the \"{prefix}{reward}\" goal for {username}, for some highly technical reason.')

+ 1 - 1
ownchatbot/defaults/alerts.py

@@ -1,2 +1,2 @@
-ALERTS = {}  # Will populate as the streamer configures alerts
+ALERTS = {'m_name': '', 'm_reward': '', 'g_name': '', 'g_reward': ''}
 

+ 0 - 83
ownchatbot/templates/ocbalert.html

@@ -1,83 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-    <meta charset="UTF-8">
-    <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <title>OwnchatBot Alert</title>
-    <style>
-        body {
-            background-color: transparent;
-            position: relative; /* Ensure relative positioning for absolute children */
-        }
-
-        #alertVid {
-            display: none;
-            max-height: 100px;
-            position: absolute; /* Allow positioning with top, left, etc. */
-            z-index: 1; /* Ensure it's below textBox */
-        }
-
-        #alertImg {
-            display: none;
-            max-height: 100px;
-            position: absolute; /* Allow positioning */
-            z-index: 1; /* Ensure it's below textBox */
-        }
-
-        #textBox {
-            display: none;
-            position: absolute;
-            top: 20%; /* Adjust as needed */
-            left: 50%; /* Center horizontally */
-            transform: translateX(-50%); /* Center */
-            color: black;
-            z-index: 2; /* Ensure it appears above alertImg and alertVid */
-            font-size: 20px; /* Adjust as needed */
-        }
-    </style>
-</head>
-<body>
-    {% if not alert_name %}
-        <div id="alertVid"></div>
-        <div id="alertImg"></div>
-    {% elif "webm" in alert_name %}
-        <video id="alertVid" autoplay>
-            <source src="{{ url_for('web_panels.alerts', alert_name=alert_name) }}" type="video/webm">
-        </video>
-        <div id="alertImg"></div>
-    {% else %}
-        <div id="alertVid"></div>
-        <img id="alertImg" src="{{ url_for('web_panels.alerts', alert_name=alert_name) }}">
-    {% endif %}
-
-    <div id="textBox"></div>
-
-    <script>
-        setInterval(function() {
-            fetch('/checkAlerts')
-                .then(response => response.json())
-                .then(data => {
-                    if (data) {
-                        const alertVid = document.getElementById('alertVid');
-                        const alertImg = document.getElementById('alertImg');
-                        const textBox = document.getElementById('textBox');
-
-                        textBox.innerHTML = `
-
-                        `;
-                        textBox.style.display = 'block';
-
-                        alertVid.style.display = 'block';
-                        alertImg.style.display = 'block';
-
-                        setTimeout(() => {
-                            alertVid.style.display = 'none';
-                            alertImg.style.display = 'none';
-                            textBox.style.display = 'none';
-                        }, 10000); // Visible for 10 seconds
-                    }
-                });
-        }, 3000); // Check for new subscribers every 3 seconds
-    </script>
-</body>
-</html>

+ 6 - 3
ownchatbot/web_panels.py

@@ -537,7 +537,7 @@ def reset(reward_name, reward_type):
         reset_goal(reward_name)
     if reward_type == "vote":
         reset_vote(reward_name)
-    return redirect(url_for('web_panels.mgmt'))
+    return redirect(url_for('web_panels.mgmt', activeTab='managerewards'))
 
 
 @ocb.route('/mgmt/rereadvotes', methods=['GET', 'POST'])
@@ -680,8 +680,11 @@ def ocb_alert(alert_type):
     elif alert_type == 'SUBSCRIBER_ALERT':
         return render_template('subscriber.html',
                                alert_name=alert_name)
-    else:
-        return render_template('ocbalert.html',
+    elif alert_type == 'MILESTONE_ALERT':
+        return render_template('rmilestone.html',
+                               alert_name=alert_name)
+    elif alert_type == 'GOAL_ALERT':
+        return render_template('rgoal.html',
                                alert_name=alert_name)
 
 

+ 29 - 28
ownchatbot/webhooks.py

@@ -3,11 +3,10 @@ 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, award_chat_points, user_in_points, get_all_users_with_user_id
 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.reward_handlers import all_active_goals, all_active_votes, all_active_rewards, save_alerts
 from ownchatbot.kofi_handlers import accept_donation, accept_sub
 import json
 import random
-from ownchatbot import followers, donations, subscribers, rgoal, rmilestone
 
 
 ocb = Blueprint('webhooks', __name__)
@@ -165,60 +164,62 @@ def kofi_hook():
 
 @ocb.route('/checkFollows')  # Polled by follower.html template to check for new followers
 def check_follows():
-    global followers
     if followers:
         current_app.logger.debug(f'\n\n{format(followers[0])}\n\n')
-        last_follower = followers.pop(0)
+        last_follower = followers.clear()
         return jsonify(last_follower)
     else:
-        current_app.logger.info(f'No new followers')
+        current_app.logger.debug(f'No new followers')
     return jsonify(None)
 
 
-@ocb.route('/checkGoals')  # Polled by follower.html template to check for new followers
+@ocb.route('/checkGoals')  # Polled by ocbalert.html template to check for new followers
 def check_goals():
-    global rgoals
-    if rgoals:
-        current_app.logger.debug(f'\n\n{format(rgoals[0])}\n\n')
-        last_goal = rgoals.pop(0)
-        return jsonify(last_goal)
+    alerts_dict = current_app.config['ALERTS']
+    rgoals = {'name': alerts_dict['g_name'], 'reward': alerts_dict['g_reward']}
+    if rgoals['name']:
+        current_app.logger.debug(f'\n\n{format(rgoals)}\n\n')
+        alerts_dict['g_name'] = ''
+        alerts_dict['g_reward'] = ''
+        save_alerts(alerts_dict)
+        return jsonify(rgoals)
     else:
-        current_app.logger.info(f'No new goals reached')
-    return jsonify(None)
+        current_app.logger.debug(f'No new goals reached')
     return jsonify(None)
 
 
-@ocb.route('/checkMilestones')  # Polled by follower.html template to check for new followers
+@ocb.route('/checkMilestones')  # Polled by ocbalert.html template to check for new followers
 def check_milestones():
-    global rmilestones
-    if rmilestones:
-        current_app.logger.debug(f'\n\n{format(rmilestones[0])}\n\n')
-        last_milestone = rmilestones.pop(0)
-        return jsonify(last_milestone)
+    alerts_dict = current_app.config['ALERTS']
+    rmilestones = {'name': alerts_dict['m_name'], 'reward': alerts_dict['m_reward']}
+    if rmilestones['name']:
+        current_app.logger.info(f'\n\n{format(rmilestones)}\n\n')
+        alerts_dict['m_name'] = ''
+        alerts_dict['m_reward'] = ''
+        save_alerts(alerts_dict)
+        return jsonify(rmilestones)
     else:
-        current_app.logger.info(f'No new milestones passed')
+        current_app.logger.debug(f'No new milestones passed')
     return jsonify(None)
 
 
 @ocb.route('/checkDonations')  # Polled by donation.html template to check for new kofi donations
 def check_donations():
-    global donations
     if donations:
-        current_app.logger.info(f'\n\n{format(donations[0])}\n\n')
-        last_donation = donations.pop(0)
+        current_app.logger.info(f'\n\n{format(donations)}\n\n')
+        last_donation = donations.clear()
         return jsonify(last_donation)
     else:
-        current_app.logger.info(f'No new donations')
+        current_app.logger.debug(f'No new donations')
     return jsonify(None)
 
 
 @ocb.route('/checkSubscribers')  # Polled by subscriber.html template to check for new kofi subscribers
 def check_subscribers():
-    global subscribers
     if subscribers:
-        current_app.logger.info(f'\n\n{format(subscribers[0])}\n\n')
-        last_subscriber = subscribers.pop(0)
+        current_app.logger.info(f'\n\n{format(subscribers)}\n\n')
+        last_subscriber = subscribers.clear()
         return jsonify(last_subscriber)
     else:
-        current_app.logger.info(f'No new subscribers')
+        current_app.logger.debug(f'No new subscribers')
     return jsonify(None)