首页 > 解决方案 > RuntimeError:事件循环已关闭(对于不和谐的 Py 机器人)

问题描述

嘿,我对在 python 中编码机器人很陌生,我已经收到这个错误很多,我不知道出了什么问题,请有人帮忙。

cogs = [levelsys]

client = commands.Bot(command_prefix="-", intents=discord.Intents.all())

for i in range(len(cogs)):
    cogs[i].setup(client)

client.run("Token")

更多错误信息:

         > & C:/Users/User/AppData/Local/Programs/Python/Python39/python.exe d:/Bot/TheHubProjects/LevelSys/main.py
Traceback (most recent call last):
  File "d:\Bot\TheHubProjects\LevelSys\main.py", line 9, in <module>
    client.run("Token")
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 723, in run
    return future.result()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 702, in runner
    await self.start(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 666, in start
    await self.connect(reconnect=reconnect)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 601, in connect
    raise PrivilegedIntentsRequired(exc.shard_id) from None
discord.errors.PrivilegedIntentsRequired: Shard ID None is requesting privileged intents that have not been explicitly enabled in the developer portal. It is recommended to go to https://discord.com/developers/applications/ and explicitly enable the privileged intents within your application's page. If this is not possible, then consider disabling the privileged intents instead.
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000014FBE5BA280>
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
PS D:\Bot> & C:/Users/User/AppData/Local/Programs/Python/Python39/python.exe d:/Bot/TheHubProjects/LevelSys/main.py
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 300, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 254, in request     
    raise HTTPException(r, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "d:\Bot\TheHubProjects\LevelSys\main.py", line 12, in <module>
    client.run("Token")
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 723, in run
    return future.result()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 702, in runner
    await self.start(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 665, in start
    await self.login(*args, bot=bot)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 511, in login
    await self.http.static_login(token.strip(), bot=bot)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 304, in static_login
    raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001BD6BABA280>
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
    self._check_closed()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

标签: pythonpython-3.xdiscord.py

解决方案


我希望那不是你真正的不和谐机器人令牌?你有 "commands_prefix="-" 它只是 "command_prefix="-"

编辑:我相信它在加载 cog,尝试将您的 cog 类放在它自己的名为 cogs 的文件夹中。此函数使用 .py 获取 cogs 文件夹中的所有文件,删除 .py 并加载它们。您使用 load_extension 所以希望这可以工作,除此之外它必须是 cog 类中的错误或“intents =”错误

# Load All Categorys in cogs folder
Import os
for filename in os.listdir('./cogs'):
  if filename.endswith('.py'):
    client.load_extension(f'cogs.{filename[:-3]}')

推荐阅读