web_panels.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. from flask import render_template, Blueprint, current_app, redirect, request, url_for, session
  2. from datetime import timezone
  3. 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
  4. 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
  5. from ownchatbot.user_handlers import get_all_users, get_all_users_by_name, refund_points, adjust_points
  6. import json
  7. import emoji
  8. ocb = Blueprint('web_panels', __name__)
  9. @ocb.route('/mgmt', methods=['GET']) # The streamer's management panel
  10. def mgmt():
  11. auth_code = request.args.get('auth')
  12. if auth_code == current_app.config['MGMT_AUTH']:
  13. session['auth_code'] = auth_code # Store auth code in session
  14. else:
  15. return "Not authorized", 403 # Handle invalid auth code
  16. db = get_db()
  17. users = get_all_users(db)
  18. utc_timezone = timezone.utc
  19. rewards = current_app.config['REWARDS']
  20. active_rewards = []
  21. for each_reward in all_active_rewards(): # Get the name of all active rewards
  22. active_rewards.append(each_reward)
  23. active_categories = current_app.config['ACTIVE_CAT']
  24. inactive_categories = current_app.config['INACTIVE_CAT']
  25. all_cats = current_app.config['ALL_CAT']
  26. mgmt_auth = current_app.config['MGMT_AUTH']
  27. points_interval = current_app.config['POINTS_INTERVAL']
  28. points_award = current_app.config['POINTS_AWARD']
  29. gunicorn_logging = current_app.config['GUNICORN']
  30. prefix = current_app.config['PREFIX']
  31. access_token = current_app.config['ACCESS_TOKEN']
  32. owncast_url = current_app.config['OWNCAST_URL']
  33. settings_info = [mgmt_auth, points_interval, points_award, gunicorn_logging, prefix, access_token, owncast_url]
  34. return render_template('mgmt.html',
  35. queue=get_queue(db),
  36. votes=all_active_votes(db),
  37. goals=all_active_goals(db),
  38. rewards=rewards,
  39. active_rewards=active_rewards,
  40. prefix=current_app.config['PREFIX'],
  41. users=users,
  42. utc_timezone=utc_timezone,
  43. active_categories=active_categories,
  44. inactive_categories=inactive_categories,
  45. settings_info=settings_info)
  46. @ocb.route('/userpanel', methods=['GET']) # The viewers panel
  47. def user_panel():
  48. db = get_db()
  49. all_rewards = rewards = current_app.config['REWARDS']
  50. username = request.args.get('username')
  51. points_interval = current_app.config['POINTS_INTERVAL']
  52. points_award = current_app.config['POINTS_AWARD']
  53. if username is not None:
  54. users = get_all_users_by_name(db, username)
  55. else:
  56. users = []
  57. utc_timezone = timezone.utc
  58. return render_template('userpanel.html',
  59. queue=get_queue(db),
  60. votes=all_active_votes(db),
  61. goals=all_active_goals(db),
  62. rewards=all_active_rewards(),
  63. all_rewards=all_rewards,
  64. prefix=current_app.config['PREFIX'],
  65. points_interval=points_interval,
  66. points_award=points_award,
  67. username=username,
  68. users=users,
  69. utc_timezone=utc_timezone)
  70. @ocb.route('/mgmt/fulfill', methods=['GET'])
  71. def fulfilled():
  72. db = get_db()
  73. reward_id = request.args.get('reward_id')
  74. username = request.args.get('username')
  75. fulfill_reward(db, reward_id)
  76. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  77. @ocb.route('/mgmt/refund', methods=['GET'])
  78. def refund():
  79. db = get_db()
  80. reward_id = request.args.get('reward_id')
  81. reward = request.args.get('reward')
  82. rewards = current_app.config['REWARDS']
  83. points = rewards[reward]['price']
  84. username = request.args.get('username')
  85. user_id = request.args.get('rewarder_id')
  86. refund_points(db, user_id, points) # resets points
  87. refund_reward(db, reward_id) # marks the reward as refunded
  88. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  89. @ocb.route('/mgmt/adjust/<user_id>', methods=['GET', 'POST']) # Streamer manually adjusts user's points
  90. def adjust(user_id):
  91. if 'auth_code' not in session:
  92. return "Not authorized", 403
  93. db = get_db()
  94. name = request.args.get('name')
  95. points = request.args.get('points')
  96. if request.method == 'POST':
  97. user_id = request.form['user_id']
  98. name = request.form['name']
  99. newpoints = request.form['newpoints']
  100. adjust_points(db, user_id, newpoints)
  101. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  102. return render_template('adjust.html',
  103. name=name,
  104. user_id=user_id,
  105. points=points)
  106. @ocb.route('/mgmt/delete/<reward_name>', methods=['GET', 'POST'])
  107. def delete(reward_name):
  108. del_reward = current_app.config['REWARDS']
  109. del_reward.pop(reward_name)
  110. if save_rewards(del_reward):
  111. if rem_cool(reward_name):
  112. rem_from_queue(reward_name)
  113. if reread_votes():
  114. if reread_goals():
  115. pass
  116. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  117. @ocb.route('/mgmt/edit/<reward_name>', methods=['GET', 'POST'])
  118. def edit(reward_name):
  119. if 'auth_code' not in session:
  120. return "Not authorized", 403
  121. active_categories = current_app.config['ACTIVE_CAT']
  122. all_the_rewards = current_app.config['REWARDS']
  123. reward_data = all_the_rewards[reward_name]
  124. all_cats = current_app.config['ALL_CAT']
  125. if request.method == 'POST':
  126. reward_data['cooldown'] = int(request.form['cooldown'])
  127. reward_data['type'] = request.form['type']
  128. if reward_data['type'] == 'goal':
  129. reward_data['target'] = int(request.form['target'])
  130. else:
  131. reward_data['price'] = int(request.form['price'])
  132. reward_data['info'] = emoji.demojize(request.form['info'])
  133. if reward_data['type'] == 'special':
  134. reward_data['cmd'] = request.form['cmd']
  135. reward_data['categories'] = request.form.getlist('category')
  136. reward_data['cooldown'] = int(request.form['cooldown'])
  137. all_the_rewards[reward_name] = reward_data
  138. save_rewards(all_the_rewards)
  139. if reward_data['type'] == 'goal': # Sync goals and votes in the db with rewards.py
  140. reread_goals()
  141. if reward_data['type'] == 'vote':
  142. reread_votes()
  143. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  144. return render_template('edit.html',
  145. all_cats=all_cats,
  146. reward_name=reward_name,
  147. active_categories=active_categories,
  148. reward_data=reward_data)
  149. @ocb.route('/mgmt/settings', methods=['GET', 'POST']) # OwnchatBot settings panel
  150. def settings():
  151. points_interval = int(request.form['points_interval'])
  152. points_award = int(request.form['points_award'])
  153. gunicorn_logging = 'gunicorn_logging' in request.form
  154. prefix = request.form['prefix']
  155. access_token = request.form['access_token']
  156. owncast_url = request.form['owncast_url']
  157. mgmt_auth = request.form['mgmt_auth']
  158. config_dict = {
  159. 'MGMT_AUTH': mgmt_auth,
  160. 'POINTS_INTERVAL': points_interval,
  161. 'POINTS_AWARD': points_award,
  162. 'GUNICORN': gunicorn_logging,
  163. 'PREFIX': prefix,
  164. 'ACCESS_TOKEN': access_token,
  165. 'OWNCAST_URL': owncast_url
  166. }
  167. save_config(config_dict)
  168. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  169. @ocb.route('/mgmt/add/<reward_type>', methods=['GET', 'POST'])
  170. def add(reward_type):
  171. if 'auth_code' not in session:
  172. return "Not authorized", 403
  173. all_cats = current_app.config['ALL_CAT']
  174. active_categories = current_app.config['ACTIVE_CAT']
  175. all_the_rewards = current_app.config['REWARDS']
  176. if request.method == 'POST':
  177. name = request.form['name']
  178. name = name.lower() # Force the name to all lower case
  179. name = emoji.demojize(name) # Remove any emojis, because they cause UnicodeEncodeErrors
  180. name = name.replace(" ", "") # Remove any spaces from the name
  181. type = request.form['type']
  182. if type != 'category': # If we're only adding a category, skip all of this
  183. cooldown = int(request.form['cooldown'])
  184. if type == 'redeem' or type == 'special' or type == 'vote':
  185. price = int(request.form['price'])
  186. if type == 'goal':
  187. target = int(request.form['target'])
  188. info = request.form['info']
  189. info = emoji.demojize(info) # Remove any emojis, because they cause UnicodeEncodeErrors
  190. if type == 'special':
  191. cmd = request.form['cmd']
  192. categories = request.form.getlist('category')
  193. if type == 'redeem':
  194. if categories == ['']:
  195. all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'cooldown': cooldown}
  196. else:
  197. all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'categories': categories, 'cooldown': cooldown}
  198. if type == 'goal':
  199. if categories == ['']:
  200. all_the_rewards[name] = {'target': target, 'type': type, 'info': info, 'cooldown': cooldown}
  201. else:
  202. all_the_rewards[name] = {'target': target, 'type': type, 'info': info, 'categories': categories, 'cooldown': cooldown}
  203. if type == 'vote':
  204. if categories == ['']:
  205. all_the_rewards[name] = {'price': price, 'type': type, 'info': info}
  206. else:
  207. all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'categories': categories, 'cooldown': cooldown}
  208. if type == 'special':
  209. if categories == ['']:
  210. all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'cmd': cmd, 'cooldown': cooldown}
  211. else:
  212. all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'cmd': cmd, 'categories': categories, 'cooldown': cooldown}
  213. save_rewards(all_the_rewards)
  214. if type == 'goal': # Remove old goals and votes from the database
  215. reread_goals()
  216. if type == 'vote':
  217. reread_votes()
  218. else: # If we're only adding a category
  219. inactive_categories = current_app.config['INACTIVE_CAT']
  220. inactive_categories.append(name) # Add it to the INACTIVE_CAT variable
  221. reread_categories() # Write it to categories.py
  222. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  223. return render_template('add.html',
  224. all_cats=all_cats,
  225. reward_type=reward_type,
  226. active_categories=active_categories)
  227. @ocb.route('/mgmt/activate/<category>', methods=['GET', 'POST'])
  228. def activate(category):
  229. activate_category(category)
  230. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  231. @ocb.route('/mgmt/deactivate/<category>', methods=['GET', 'POST'])
  232. def deactivate(category):
  233. deactivate_category(category)
  234. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  235. @ocb.route('/mgmt/delcat/<cat_name>/<cat_act>', methods=['GET', 'POST'])
  236. def delcat(cat_name, cat_act):
  237. active_categories = current_app.config['ACTIVE_CAT']
  238. inactive_categories = current_app.config['INACTIVE_CAT']
  239. if cat_act == 'inactive':
  240. inactive_categories.remove(cat_name)
  241. else:
  242. active_categories.remove(cat_name)
  243. reread_categories()
  244. current_rewards = current_app.config['REWARDS']
  245. for reward, details in current_rewards.items(): # Remove from rewards.py as well
  246. if cat_name in details['categories']:
  247. details['categories'].remove(cat_name)
  248. save_rewards(current_rewards)
  249. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  250. @ocb.route('/mgmt/reset/<reward_name>/<reward_type>', methods=['GET', 'POST']) # Reset votes and goals to zero
  251. def reset(reward_name, reward_type):
  252. if reward_type == "goal":
  253. reset_goal(reward_name)
  254. if reward_type == "vote":
  255. reset_vote(reward_name)
  256. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  257. @ocb.route('/mgmt/rereadvotes', methods=['GET', 'POST'])
  258. def rereadv():
  259. reread_votes()
  260. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  261. @ocb.route('/mgmt/clearfulfilled', methods=['GET', 'POST'])
  262. def clearfulfilled():
  263. clear_fulfilled_rewards()
  264. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
  265. @ocb.route('/mgmt/clearqueue', methods=['GET', 'POST'])
  266. def clear_queue():
  267. clear_reward_queue()
  268. return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))