簡體   English   中英

如何讓機器人在 discord.py 中編輯自己的消息

[英]How to make a bot edit its own message in discord.py

有沒有辦法讓機器人編輯自己的消息? 我試圖尋找答案,但找不到。

這將通過代碼完成。 您需要以某種方式在您的機器人程序中執行它。 例如,為它創建一個執行它的命令,然后你可以刪除它。

  1. 獲取消息 object。 這可以通過首先獲取通道 object 然后從中獲取消息來完成。 基本上:
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. 完成后,調用該消息 object 的編輯方法對其進行編輯。 另外,因為它是一個協程,所以你需要等待它。 然后,您需要將您希望正在編輯的消息具有的新文本傳遞給content kwarg。 例如,如果您要輸入的新文本是“消息的新內容”,您將得到以下信息:
await message.edit(content="the new content of the message")

基本上就是這樣。 通過您的機器人執行這三行代碼,它將編輯消息。

我用另一種方法嘗試了它並且它有效。 我正在使用 nextcord.py 庫。

你可以這樣做:

@bot.event:
async def on_message(message):
    if message.content == "Test!":
        ctx = await message.reply("Processing!")
        await ctx.edit("Test Successful!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM