Python Discord: RuntimeError: Event loop is closed when launched from a specific network

Python Discord: RuntimeError: Event loop is closed when launched from a specific network

RuntimeError: Event loop is closed 尝试 运行 discord 中的任何机器人 连接到我的 wifi 网络时 时出错。当连接到另一个网络时,一切正常。

代码示例:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())

bot.run(TOKEN)

我在尝试 运行 此代码时遇到上述错误。我准确地传递了正确的访问令牌并启用了所有意图:

我正在使用 py-cord,但在使用 discord-py 库时也出现了问题。

我使用命令

安装了最新版本的py-cord==2.0.0b7
pip install git+https://github.com/Pycord-Development/pycord

错误全文:

Traceback (most recent call last):
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 550, in connect
    self.ws = await asyncio.wait_for(coro, timeout=60.0)
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\asyncio\tasks.py", line 483, in wait_for
    return fut.result()
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\gateway.py", line 330, in from_client
    socket = await client.http.ws_connect(gateway)
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\http.py", line 214, in ws_connect
    return await self.__session.ws_connect(url, **kwargs)
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\client.py", line 769, in _ws_connect
    raise WSServerHandshakeError(
aiohttp.client_exceptions.WSServerHandshakeError: 503, message='Invalid response status', url=URL('wss://gateway.discord.gg/?encoding=json&v=10&compress=zlib-stream')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "PythonCode.py", line 11, in <module>
    bot.run(TOKEN)
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 715, in run
    return future.result()
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 694, in runner
    await self.start(*args, **kwargs)
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 658, in start
    await self.connect(reconnect=reconnect)
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 610, in connect
    ws_params.update(sequence=self.ws.sequence, resume=True, session=self.ws.session_id)
AttributeError: 'NoneType' object has no attribute 'sequence'
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000173E3CD74C0>
Traceback (most recent call last):
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 719, in call_soon
    self._check_closed()
  File "C:\Users\denisnumb\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 508, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

如果问题仅出现在特定的 Wi-Fi 网络中,可能值得尝试重新启动路由器 - 这就是问题的解决方案