Advertisement
Tkap1

Untitled

Mar 10th, 2023
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1.  
  2. import json
  3. import websocket
  4. import requests
  5.  
  6. def on_error(ws, error):
  7.     print(f"Error: {error}")
  8.  
  9. def on_close(ws, a, b):
  10.     print("Closed the WebSocket connection")
  11.  
  12. def on_message(ws, message):
  13.     from main import get_oauth_stuff, s_command_call
  14.     from config import broadcaster_id, target_channel
  15.     import config
  16.     import tts
  17.     import secret
  18.     import utils
  19.  
  20.     message = json.loads(message)
  21.  
  22.     if message["metadata"]["message_type"] == "session_welcome":
  23.         session_id = message["payload"]["session"]["id"]
  24.  
  25.         oauth_stuff = get_oauth_stuff()
  26.  
  27.         headers = {
  28.             "Authorization": "Bearer %s" % secret.refresh_token,
  29.             "Client-Id": oauth_stuff["client_id"],
  30.             "Content-Type": "application/json",
  31.         }
  32.         data = {
  33.             "type": "channel.follow",
  34.             "version": "beta",
  35.             "condition": {
  36.                 "broadcaster_user_id": f"{broadcaster_id}",
  37.                 "moderator_user_id": f"{broadcaster_id}"
  38.             },
  39.             "transport": {
  40.                 "method": "websocket",
  41.                 "session_id": session_id,
  42.             },
  43.         }
  44.  
  45.         r = requests.post("https://api.twitch.tv/helix/eventsub/subscriptions", headers=headers, json=data)
  46.  
  47.         assert r.status_code == 202
  48.  
  49.     if message["metadata"]["message_type"] == "notification":
  50.         if message["metadata"]["subscription_type"] == "channel.follow":
  51.             user = message["payload"]["event"]["user_name"]
  52.             if hasattr(config, "follow_callback"):
  53.                 config.follow_callback(user)
  54.             else:
  55.                 default_follow_callback()
  56.  
  57.  
  58. def handle():
  59.     ws = websocket.WebSocketApp(
  60.         "wss://eventsub-beta.wss.twitch.tv/ws",
  61.         on_message=on_message,
  62.         on_error=on_error,
  63.         on_close=on_close,
  64.     )
  65.     ws.run_forever()
  66.  
  67.  
  68. def default_follow_callback(user):
  69.     import config
  70.     import tts
  71.     from main import s_command_call
  72.  
  73.     utils.send_chat_message(f"Thank you for following {user}")
  74.     tts.tts_command(s_command_call(target_channel, f"Thank you for following {user}", False))
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement