Rapptz / discord.py

An API wrapper for Discord written in Python.
http://discordpy.rtfd.org/en/latest
MIT License
14.5k stars 3.74k forks source link

client.login and client.start: RuntimeWarning: coroutine 'x' was never awaited #7783

Closed sandbands closed 2 years ago

sandbands commented 2 years ago

Summary

Can't run the client because client.login and client.start werent awaited but i cant await them

Reproduction Steps

client.login(token)
client.start(reconnect=True)

i get

RuntimeWarning: coroutine 'client.login was never awaited RuntimeWarning: coroutine 'client.start' was never awaited

i tried

await client.login(token)
await client.start(reconnect=True)

but it told me i cant have await calls outside of functions so i tried

async def main():
    await client.login(token)
    await client.start(reconnect=True)

main()

but main() has to be awaited. and since I cant await outside of an async function i cant call main to await those two.

Minimal Reproducible Code

No response

Expected Results

client is supposed to run

Actual Results

Got errors

C:\Users\ramal\projects\server-nuker\main.py:102: RuntimeWarning: coroutine 'Client.login' was never awaited
  client.login(token)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\ramal\projects\server-nuker\main.py:103: RuntimeWarning: coroutine 'Client.start' was never awaited
  client.start(reconnect=True)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Intents

default

System Information

Checklist

Additional Context

No response

XuaTheGrate commented 2 years ago

With 2.0, the recommended way to start a bot is by using asyncio.run, as such:

async def main():
    async with client:
        await client.start(...)

asyncio.run(main())

GitHub issues should be used for bug reports, for further questions, please join the discord.py support server: https://discord.gg/dpy

sandbands commented 2 years ago

trying that just gives me this

Traceback (most recent call last):
  File "C:\Users\ramal\projects\server-nuker\main.py", line 119, in <module>
    asyncio.run(main())
  File "C:\Users\ramal\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\ramal\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete
    return future.result()
  File "C:\Users\ramal\projects\server-nuker\main.py", line 113, in main
    async with client:
AttributeError: __aenter__
XuaTheGrate commented 2 years ago

For versions below 2.0, use client.run instead.