from flask import render_template, Blueprint, current_app, redirect, request, url_for, session from datetime import timezone from ownchatbot.db import get_db, reread_goals, reread_votes, rem_vote, reset_vote, reset_goal, clear_fulfilled_rewards, clear_reward_queue, rem_cool, rem_from_queue from ownchatbot.reward_handlers import all_active_votes, all_active_goals, all_active_rewards, get_queue, fulfill_reward, save_rewards, activate_category, deactivate_category, refund_reward, reread_categories, save_config from ownchatbot.user_handlers import get_all_users, get_all_users_by_name, refund_points, adjust_points import json import emoji ocb = Blueprint('web_panels', __name__) @ocb.route('/mgmt', methods=['GET']) # The streamer's management panel def mgmt(): auth_code = request.args.get('auth') if auth_code == current_app.config['MGMT_AUTH']: session['auth_code'] = auth_code # Store auth code in session else: return "Not authorized", 403 # Handle invalid auth code db = get_db() users = get_all_users(db) utc_timezone = timezone.utc rewards = current_app.config['REWARDS'] active_rewards = [] for each_reward in all_active_rewards(): # Get the name of all active rewards active_rewards.append(each_reward) active_categories = current_app.config['ACTIVE_CAT'] inactive_categories = current_app.config['INACTIVE_CAT'] all_cats = current_app.config['ALL_CAT'] mgmt_auth = current_app.config['MGMT_AUTH'] points_interval = current_app.config['POINTS_INTERVAL'] points_award = current_app.config['POINTS_AWARD'] gunicorn_logging = current_app.config['GUNICORN'] prefix = current_app.config['PREFIX'] access_token = current_app.config['ACCESS_TOKEN'] owncast_url = current_app.config['OWNCAST_URL'] settings_info = [mgmt_auth, points_interval, points_award, gunicorn_logging, prefix, access_token, owncast_url] return render_template('mgmt.html', queue=get_queue(db), votes=all_active_votes(db), goals=all_active_goals(db), rewards=rewards, active_rewards=active_rewards, prefix=current_app.config['PREFIX'], users=users, utc_timezone=utc_timezone, active_categories=active_categories, inactive_categories=inactive_categories, settings_info=settings_info) @ocb.route('/userpanel', methods=['GET']) # The viewers panel def user_panel(): db = get_db() all_rewards = rewards = current_app.config['REWARDS'] username = request.args.get('username') points_interval = current_app.config['POINTS_INTERVAL'] points_award = current_app.config['POINTS_AWARD'] if username is not None: users = get_all_users_by_name(db, username) else: users = [] utc_timezone = timezone.utc return render_template('userpanel.html', queue=get_queue(db), votes=all_active_votes(db), goals=all_active_goals(db), rewards=all_active_rewards(), all_rewards=all_rewards, prefix=current_app.config['PREFIX'], points_interval=points_interval, points_award=points_award, username=username, users=users, utc_timezone=utc_timezone) @ocb.route('/mgmt/fulfill', methods=['GET']) def fulfilled(): db = get_db() reward_id = request.args.get('reward_id') username = request.args.get('username') fulfill_reward(db, reward_id) return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/refund', methods=['GET']) def refund(): db = get_db() reward_id = request.args.get('reward_id') reward = request.args.get('reward') rewards = current_app.config['REWARDS'] points = rewards[reward]['price'] username = request.args.get('username') user_id = request.args.get('rewarder_id') refund_points(db, user_id, points) # resets points refund_reward(db, reward_id) # marks the reward as refunded return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/adjust/', methods=['GET', 'POST']) # Streamer manually adjusts user's points def adjust(user_id): if 'auth_code' not in session: return "Not authorized", 403 db = get_db() name = request.args.get('name') points = request.args.get('points') if request.method == 'POST': user_id = request.form['user_id'] name = request.form['name'] newpoints = request.form['newpoints'] adjust_points(db, user_id, newpoints) return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) return render_template('adjust.html', name=name, user_id=user_id, points=points) @ocb.route('/mgmt/delete/', methods=['GET', 'POST']) def delete(reward_name): del_reward = current_app.config['REWARDS'] del_reward.pop(reward_name) if save_rewards(del_reward): if rem_cool(reward_name): rem_from_queue(reward_name) if reread_votes(): if reread_goals(): pass return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/edit/', methods=['GET', 'POST']) def edit(reward_name): if 'auth_code' not in session: return "Not authorized", 403 active_categories = current_app.config['ACTIVE_CAT'] all_the_rewards = current_app.config['REWARDS'] reward_data = all_the_rewards[reward_name] all_cats = current_app.config['ALL_CAT'] if request.method == 'POST': reward_data['cooldown'] = int(request.form['cooldown']) reward_data['type'] = request.form['type'] if reward_data['type'] == 'goal': reward_data['target'] = int(request.form['target']) else: reward_data['price'] = int(request.form['price']) reward_data['info'] = emoji.demojize(request.form['info']) if reward_data['type'] == 'special': reward_data['cmd'] = request.form['cmd'] reward_data['categories'] = request.form.getlist('category') reward_data['cooldown'] = int(request.form['cooldown']) all_the_rewards[reward_name] = reward_data save_rewards(all_the_rewards) if reward_data['type'] == 'goal': # Sync goals and votes in the db with rewards.py reread_goals() if reward_data['type'] == 'vote': reread_votes() return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) return render_template('edit.html', all_cats=all_cats, reward_name=reward_name, active_categories=active_categories, reward_data=reward_data) @ocb.route('/mgmt/settings', methods=['GET', 'POST']) # OwnchatBot settings panel def settings(): points_interval = int(request.form['points_interval']) points_award = int(request.form['points_award']) gunicorn_logging = 'gunicorn_logging' in request.form prefix = request.form['prefix'] access_token = request.form['access_token'] owncast_url = request.form['owncast_url'] mgmt_auth = request.form['mgmt_auth'] config_dict = { 'MGMT_AUTH': mgmt_auth, 'POINTS_INTERVAL': points_interval, 'POINTS_AWARD': points_award, 'GUNICORN': gunicorn_logging, 'PREFIX': prefix, 'ACCESS_TOKEN': access_token, 'OWNCAST_URL': owncast_url } save_config(config_dict) return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/add/', methods=['GET', 'POST']) def add(reward_type): if 'auth_code' not in session: return "Not authorized", 403 all_cats = current_app.config['ALL_CAT'] active_categories = current_app.config['ACTIVE_CAT'] all_the_rewards = current_app.config['REWARDS'] if request.method == 'POST': name = request.form['name'] name = name.lower() # Force the name to all lower case name = emoji.demojize(name) # Remove any emojis, because they cause UnicodeEncodeErrors name = name.replace(" ", "") # Remove any spaces from the name type = request.form['type'] if type != 'category': # If we're only adding a category, skip all of this cooldown = int(request.form['cooldown']) if type == 'redeem' or type == 'special' or type == 'vote': price = int(request.form['price']) if type == 'goal': target = int(request.form['target']) info = request.form['info'] info = emoji.demojize(info) # Remove any emojis, because they cause UnicodeEncodeErrors if type == 'special': cmd = request.form['cmd'] categories = request.form.getlist('category') if type == 'redeem': if categories == ['']: all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'cooldown': cooldown} else: all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'categories': categories, 'cooldown': cooldown} if type == 'goal': if categories == ['']: all_the_rewards[name] = {'target': target, 'type': type, 'info': info, 'cooldown': cooldown} else: all_the_rewards[name] = {'target': target, 'type': type, 'info': info, 'categories': categories, 'cooldown': cooldown} if type == 'vote': if categories == ['']: all_the_rewards[name] = {'price': price, 'type': type, 'info': info} else: all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'categories': categories, 'cooldown': cooldown} if type == 'special': if categories == ['']: all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'cmd': cmd, 'cooldown': cooldown} else: all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'cmd': cmd, 'categories': categories, 'cooldown': cooldown} save_rewards(all_the_rewards) if type == 'goal': # Remove old goals and votes from the database reread_goals() if type == 'vote': reread_votes() else: # If we're only adding a category inactive_categories = current_app.config['INACTIVE_CAT'] inactive_categories.append(name) # Add it to the INACTIVE_CAT variable reread_categories() # Write it to categories.py return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) return render_template('add.html', all_cats=all_cats, reward_type=reward_type, active_categories=active_categories) @ocb.route('/mgmt/activate/', methods=['GET', 'POST']) def activate(category): activate_category(category) return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/deactivate/', methods=['GET', 'POST']) def deactivate(category): deactivate_category(category) return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/delcat//', methods=['GET', 'POST']) def delcat(cat_name, cat_act): active_categories = current_app.config['ACTIVE_CAT'] inactive_categories = current_app.config['INACTIVE_CAT'] if cat_act == 'inactive': inactive_categories.remove(cat_name) else: active_categories.remove(cat_name) reread_categories() current_rewards = current_app.config['REWARDS'] for reward, details in current_rewards.items(): # Remove from rewards.py as well if cat_name in details['categories']: details['categories'].remove(cat_name) save_rewards(current_rewards) return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/reset//', methods=['GET', 'POST']) # Reset votes and goals to zero def reset(reward_name, reward_type): if reward_type == "goal": reset_goal(reward_name) if reward_type == "vote": reset_vote(reward_name) return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/rereadvotes', methods=['GET', 'POST']) def rereadv(): reread_votes() return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/clearfulfilled', methods=['GET', 'POST']) def clearfulfilled(): clear_fulfilled_rewards() return redirect(url_for('web_panels.mgmt', auth=session['auth_code'])) @ocb.route('/mgmt/clearqueue', methods=['GET', 'POST']) def clear_queue(): clear_reward_queue() return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))