python - 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
解决方案
我希望那不是你真正的不和谐机器人令牌?你有 "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]}')
推荐阅读
- centos - rMATS 错误“无法访问共享库”
- java - Java:可以调用带有未知参数的函数
- python - 我继承的类 MenuBar(tk.Menu) 不显示菜单栏
- css - 使用 create-react-app v2 进行生产构建时编译失败
- python-3.x - 日期过滤并填充到 Pandas 中的另一列
- java - 在阔叶框架 5.2.x 中显示库存的可用数量
- html - 显示模态引导程序的条件
- django - ListCreateAPIView POST 带有序列化程序中的注释字段而不是模型
- python - 从子类调用父类方法时遇到问题
- sql - 每 10 分钟查看一天的历史考勤数据