【问题标题】:Edit message in Python在 Python 中编辑消息
【发布时间】:2021-04-26 02:12:30
【问题描述】:

我希望我的机器人在一段时间后发送一条消息并再次处理它 我已经尝试过的:

@client.command()
async def test(ctx):
    await ctx.send ('content')
    await asyncio.sleep(3)
    await message.edit(content="newcontent")

错误信息:

AttributeError: module 'discord.message' has no attribute 'edit'

我使用以下:

  • Python 版本:3.7.4
  • discord.py 版本 1.6.0

【问题讨论】:

标签: python python-3.x discord discord.py discord.py-rewrite


【解决方案1】:

您必须先定义message

@client.command()
async def test(ctx):
    message = await ctx.send("content")
    await asyncio.sleep(3)
    await message.edit(content="newcontent")

【讨论】:

    【解决方案2】:

    您的代码中有一些错误。一:你的ctx.send和你的消息('content')之间有一个空格。您当前的代码:

    await ctx.send ('content')
    

    应改为:

    await ctx.send('content')
    

    上述内容无需更改,但建议更改。另外,确保定义message:

    message = await ctx.send('content')
    

    那你可以edit留言:

    await message.edit('new_content')
    

    【讨论】:

    • message.edit 是协程,应该等待
    • 感谢您告诉我。我当时很着急,忘了在协程的开头加上await关键字。
    • 在 Python 中,函数名和括号之间的空格不会影响函数的运行。虽然不鼓励在函数名和括号之间放置不必要的空格,但这不会引发错误。
    猜你喜欢
    • 1970-01-01
    • 2021-03-31
    • 2021-12-18
    • 2012-05-30
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多