我正在使用Quart和Discord.py来构建一个discord bot。我用这个方法运行这两个机器人。
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
1条答案
按热度按时间7rfyedvj1#
说明
从Changes to async initialisation in discord.py开始:
虽然Client.run仍然可以工作,但是如果在异步上下文之外访问Client.loop属性,则会导致错误。为了进行任何类型的异步初始化,建议将代码重构为“main”函数。
警告:默认情况下
bot.start
不提供记录器。如果你想看到错误(大多数人都这样做),你需要自己设置日志。请参见this Q&A编号
上面的代码替换了旧的方法: