【问题标题】:How to make a bot edit its own message in discord.py如何让机器人在 discord.py 中编辑自己的消息
【发布时间】:2020-08-16 14:07:50
【问题描述】:

有没有办法让机器人编辑自己的消息?我试图寻找答案,但找不到答案。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    这将通过代码完成。您需要以某种方式在您的机器人程序中执行它。例如,为它创建一个执行它的命令,然后你可以删除它。

    1. 获取消息对象。这可以通过首先获取通道对象然后从中获取消息来完成。基本上:
    channel = bot.get_channel(id_of_the_channel)
    message = await channel.fetch_message(id_of_the_message)
    
    # make sure that you change "id_of_the_channel" for the id of the channel (as an integer)
    # and make sure to change "id_of_the_message" for the id of the message (as an integer)
    # you can get those by enabling Developer Mode in the Appearance settings in discord
    # and right-clicking on the channel to get its id, and right-clicking on the message to get
    # its id as well.
    
    1. 完成后,调用该消息对象的edit 方法对其进行编辑。另外,因为它是一个协程,你需要等待它。然后,您需要将您希望正在编辑的消息具有的新文本传递给 content kwarg。例如,如果您要输入的新文本是“消息的新内容”,您将得到以下信息:
    await message.edit(content="the new content of the message")
    

    基本上就是这样。通过你的机器人执行这三行代码,它会编辑消息。

    【讨论】:

      猜你喜欢
      • 2021-05-24
      • 2022-01-05
      • 2022-01-05
      • 2020-11-07
      • 2020-10-10
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      相关资源
      最近更新 更多