我试图在python中创建一个discord bot,但discord.py没有给予我所需的控制级别,因此我正在创建一个自定义API。我正在使用“websocket-client”模块。我已经看过discord的文档了,我找不到问题。由于某种原因,当我运行这段代码时,它给了我一个错误:
import requests
from websocket import create_connection
import json
import time
from threading import Thread
token = "<my token>"
class DiscordBot:
def __init__(self, token):
self.token = token
self.header = {"authorization": token, "content-type":"application/json"}
def connect(self):
self.ws = create_connection("wss://gateway.discord.gg/?v=9&encoding=json")
event = self.__recievejsonresponse__()
self.heartbeat_interval = event["d"]["heartbeat_interval"]/1000
heartbeatProc = Thread(target=self.heartbeat)
heartbeatProc.start()
payload = {
"op":2,
"d":{
"token":self.token,
"intents":8,
"properties":{
"os":"linux",
"browser":"chromium",
"device":"pc"
},
}
}
self.__sendjsonrequest__(payload)
self.__recieveloop__()
def __recieveloop__(self):
while True:
event = self.__recievejsonresponse__()
print(event)
self.prevEvent = event
try:
#print(f"{author}:{content}")
op_code = event["op"]
if op_code == 11:
print("heartbeat recievedd")
except:
pass
def heartbeat(self):
print("heartbeating")
heartbeatJSON = {
"op": 1,
"d": "null"
}
self.__sendjsonrequest__(heartbeatJSON)
while True:
time.sleep(self.heartbeat_interval)
heartbeatJSON = {
"op": 1,
"d": "null"
}
self.__sendjsonrequest__(heartbeatJSON)
def __recievejsonresponse__(self):
response = self.ws.recv()
if response:
return json.loads(response)
def __sendjsonrequest__(self, request):
self.ws.send(json.dumps(request))
在运行这段代码时,我得到了以下错误
{'t': None, 's': None, 'op': 11, 'd': None}
heartbeat recievedd
None
Traceback (most recent call last):
File "c:\Downloads\learn.py", line 153, in <module>
myBot.connect()
File "c:\Downloads\learn.py", line 34, in connect
self.__recieveloop__()
File "c:\Downloads\learn.py", line 38, in __recieveloop__
event = self.__recievejsonresponse__()
File "c:\Downloads\learn.py", line 110, in __recievejsonresponse__
response = self.ws.recv()
File "C:\Apps\Python310\site-packages\websocket\_core.py", line 362, in recv
opcode, data = self.recv_data()
File "C:\Apps\Python310\site-packages\websocket\_core.py", line 385, in recv_data
opcode, frame = self.recv_data_frame(control_frame)
File "C:\Apps\Python310\site-packages\websocket\_core.py", line 406, in recv_data_frame
frame = self.recv_frame()
File "C:\Apps\Python310\site-packages\websocket\_core.py", line 445, in recv_frame
return self.frame_buffer.recv_frame()
File "C:\Apps\Python310\site-packages\websocket\_abnf.py", line 338, in recv_frame
self.recv_header()
File "C:\Apps\Python310\site-packages\websocket\_abnf.py", line 294, in recv_header
header = self.recv_strict(2)
File "C:\Apps\Python310\site-packages\websocket\_abnf.py", line 373, in recv_strict
bytes_ = self.recv(min(16384, shortage))
File "C:\Apps\Python310\site-packages\websocket\_core.py", line 529, in _recv
return recv(self.sock, bufsize)
File "C:\Apps\Python310\site-packages\websocket\_socket.py", line 122, in recv
raise WebSocketConnectionClosedException(
websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.
1条答案
按热度按时间mkshixfv1#
您的bot收到心跳请求,但不回复。这会导致discord关闭WebSocket,因为它认为你停止了响应,所以下一个
__recievejsonresponse__()
失败,因为websocket关闭了。只需响应心跳就足以解决问题,但您可能希望添加额外的断开连接处理,以防连接中断。