gpt4 book ai didi

python - Heroku 上的 Quart discord.py : loop attribute cannot be accessed in non-async contexts

转载 作者:行者123 更新时间:2023-12-05 05:34:07 25 4
gpt4 key购买 nike

我正在使用 Quart 和 Discord.py 构建一个 discord 机器人。我用这种方法运行两个。

app.app_context()
bot.loop.create_task(app.run_task(host="0.0.0.0", port=80))
bot.run(Discord_Dev_Token)

我尝试将项目部署到 heroku 服务中,部署成功,但稍后无法正常工作当我检查日志时,我发现了这条错误消息

2022-09-10T22:08:17.191305+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
2022-09-10T22:08:17.191350+00:00 app[web.1]: File "/app/mvc.py", line 688, in <module>
2022-09-10T22:08:17.191694+00:00 app[web.1]: bot.loop.create_task(app.run_task(host="0.0.0.0", port=PORT))
2022-09-10T22:08:17.191707+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/client.py", line 108, in __getattr__
2022-09-10T22:08:17.191797+00:00 app[web.1]: raise AttributeError(msg)
2022-09-10T22:08:17.191827+00:00 app[web.1]: AttributeError: loop attribute cannot be accessed in non-async contexts. Consider using either an asynchronous main function and passing it to asyncio.run or using asynchronous initialisation hooks such as Client.setup_hook

最佳答案

解释

来自 Changes to async initialisation in discord.py :

While Client.run still works, accessing the Client.loop attribute willnow result in an error if it's accessed outside of an async context.In order to do any sort of asynchronous initialisation, it isrecommended to refactor your code out into a "main" function.

警告:bot.start 默认不提供记录器。如果你想看到错误(大多数人都这样做),你需要自己设置日志记录。请看this Q&A

代码

async def main():
async with bot:
bot.loop.create_task(app.run_task(host="0.0.0.0", port=80))
await bot.start(Discord_Dev_Token)

asyncio.run(main())

上面的代码替换了旧的方式:

bot.loop.create_task(app.run_task(host="0.0.0.0", port=80))
bot.run(Discord_Dev_Token)

关于python - Heroku 上的 Quart discord.py : loop attribute cannot be accessed in non-async contexts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73675547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com