'I got an error with websocket and json (Discord Gateway)

when i finished the code there was MULTIPLE ERRORS and i was personally confused.

import os
import websocket #pip install websocket-client
import json
import time
import threading

def send_json_req(ws, request):
    ws.send(json.dumps(request))

def recieve_json_response(ws):
    response = ws.recv()
    if response:
        return json.loads(response)

def heartbeat(interval, ws):
    print('Heartbeat Began')
    while True:
        time.sleep(interval)
        heartbeatJSON = {
            "op": 1,
            "d": "null"
        }
        send_json_req(ws, heartbeatJSON)
        print("Heartbeat sent")

ws = websocket.WebSocket("wss://gateway.discord.gg/?v=6&encoding=json")
event = recieve_json_response(ws)

heartbeat_interval = event['d']['heartbear_interval'] / 1000
threading._start_new_thread(heartbeat, (heartbeat_interval, ws))

authtoken = Discord_Token
payload = {
    'op':1,
    "d": {
        "token": authtoken,
        "properties": {
            "$os": "linux",
            "$browser": "chrome",
            "$device": "pc"
        }
    }
}
send_json_req(ws, payload)

while True:
    event = recieve_json_response(ws)

    try:
        print(f"{event['d']['author']['username']}: {event['d']['content']}")
        op_code = event('op')
        if op_code == 11:
            print('Heartbeat received')
    except:
        pass

Theres been a bunch of errors about the websocket directory? and idk what it means. Can someone help?? and the error is kinda confusing me a lot i dont really know what to do?

the please add some details is annoying me also im new to stackoverflow.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source