【问题标题】:editing messages with discord.py使用 discord.py 编辑消息
【发布时间】:2020-06-02 09:51:24
【问题描述】:

我正在尝试编辑我的机器人发送的消息,但出现错误

@client.command()
async def edit(ctx):
    message = await ctx.send('testing')
    time.sleep(0.3)
    message.edit(content='v2')

错误:

 RuntimeWarning: coroutine 'Message.edit' was never awaited
  message.edit(content='v2')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

顺便问一下,有没有什么方法可以通过简单的消息 ID 来编辑消息?

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    time.sleep() 是一个阻塞调用,这意味着它几乎把你的脚本搞砸了。您将改为使用 await asyncio.sleep()

    另外,edit() 是协程,所以需要等待。你的命令应该是这样的:

    import asyncio # if you haven't already
    
    @client.command()
    async def edit(ctx):
        message = await ctx.send('testing')
        await asyncio.sleep(0.3)
        await message.edit(content='v2')
    

    要通过 ID 编辑消息,您需要它来自的频道:

    @client.command()
    async def edit(ctx, msg_id: int = None, channel: discord.TextChannel = None):
        if not msg_id:
            channel = client.get_channel(112233445566778899) # the message's channel
            msg_id = 998877665544332211 # the message's id
        elif not channel:
            channel = ctx.channel
        msg = await channel.fetch_message(msg_id)
        await msg.edit(content="Some content!")
    

    这个命令的用法是!edit 112233445566778899 #message-channel-origin,假设前缀是!,如果消息在你正在执行命令的通道中,就不要使用通道参数。


    参考资料:

    【讨论】:

    • 谢谢!有什么方法可以通过 ID 编辑消息吗?
    • 刚刚编辑了我的答案 :^) 希望它对你有用
    • AttributeError: 模块 'discord' 没有属性 'Channel'
    • 重新编辑,抱歉哈哈 - 我有时忘记使用TextChannel
    • 有没有什么方法可以不用一直输入ID和频道来编辑?
    猜你喜欢
    • 2021-03-19
    • 2023-04-02
    • 2020-03-17
    • 1970-01-01
    • 2021-04-04
    • 2021-07-31
    • 2021-08-31
    • 2021-12-29
    相关资源
    最近更新 更多