四分之一discord.py上的Heroku:无法在非异步上下文中访问循环属性

kh212irz  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(89)

我正在使用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
7rfyedvj

7rfyedvj1#

说明

Changes to async initialisation in discord.py开始:
虽然Client.run仍然可以工作,但是如果在异步上下文之外访问Client.loop属性,则会导致错误。为了进行任何类型的异步初始化,建议将代码重构为“main”函数。

警告:默认情况下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)

相关问题