2

I am using Quart and Discord.py to build a discord bot. i run the two with this method.

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

i tried to deploy the project into heroku service and it makes a successful deployement but it doesn't work later when i check the logs i find this error message

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 Answers1

2

Explanation

From Changes to async initialisation in discord.py:

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

WARNING: bot.start does not provide a logger by default. If you want to see errors (which most people do), you need to set up logging yourself. Please see this Q&A

Code

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())

The code above replaces the old way:

bot.loop.create_task(app.run_task(host="0.0.0.0", port=80))
bot.run(Discord_Dev_Token)
TheFungusAmongUs
  • 1,423
  • 3
  • 11
  • 28