【问题标题】:AttributeError: 'coroutine' object has no attribute 'edit'AttributeError: \'coroutine\' 对象没有属性 \'edit\'
【发布时间】:2022-12-15 21:21:48
【问题描述】:

我正在制作一个不和谐的机器人:

有一个异步函数对应于一个斜杠命令。我有另一个函数 count():

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

在斜杠命令函数中:

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

我收到以下错误: discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: 'async_generator' object is not iterable

所以我在 Stack Overflow 上查找了一些建议并将我的代码更改为:

global msg
msg = ctx.respond("")

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

asyncio.run(counnnt(n))

最后我得到了这个错误: discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: 'coroutine' object has no attribute 'edit'

(显然我没有在我的机器人中做 count() 而是非常相似的事情) 我感谢任何建议:-)

【问题讨论】:

    标签: python asynchronous pycord


    【解决方案1】:

    最有可能的是,ctx.respondasync 所以需要是 awaited:

    msg = await ctx.respond("")
    

    【讨论】:

      【解决方案2】:

      利用;

      msg.edit_original_response(content="A")
      

      【讨论】:

      • 请解释您的代码如何解决问题。只有代码的答案很难理解。
      • 错误代码中已经写明了原因。 Respond 没有编辑功能。这是用于编辑响应消息。
      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 2021-07-30
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多