AttributeError: 'coroutine' object has no attribute 'edit'

Question:

I am making a discord bot:

There is an async function that corresponds to a slash command. I have another function called count():

async def count(n):
    for i in range(n):
        yield i

and in the slash command function:

msg = ctx.respond("")
for i in count(n):
    await msg.edit(i)

I got the following error:
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: 'async_generator' object is not iterable

So I looked up some suggestions on Stack Overflow and changed my code to:

global msg
msg = ctx.respond("")

async def counnnt(n):
    async for i in count(n):
        await msg.edit(i)

asyncio.run(counnnt(n))

Finally I got this error:
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: 'coroutine' object has no attribute 'edit'

(obviously I am not doing count() in my bot but something very similar)
I appreciate any suggestions 🙂

Asked By: easonoob

||

Answers:

Use;

msg.edit_original_response(content="A")
Answered By: Loademon

Most probably, ctx.respond is async so needs to be awaited:

msg = await ctx.respond("")
Answered By: Yevhen Kuzmovych
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.