如何让机器人在discord.py 中编辑自己的消息

4 python discord.py

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

小智 6

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

  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.
Run Code Online (Sandbox Code Playgroud)
  1. 完成此操作后,您可以调用该消息对象的编辑方法来编辑它。另外,因为它是一个协程,所以您需要等待它。然后,您需要将您希望编辑的消息具有的新文本传递给content kwarg。例如,如果您要输入的新文本是“消息的新内容”,您将看到以下内容:
await message.edit(content="the new content of the message")
Run Code Online (Sandbox Code Playgroud)

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