問題描述
我正在嘗試使用它的 ID 刪除一條消息.我正在使用 discord.py.
I am trying to delete a message using it's ID. I am using discord.py.
邏輯流程
用戶發送命令.示例:!message hi
Bot 使用用戶的消息 ID 刪除!message hi"
機器人說嗨"
User sends command. example: !message hi
Bot deletes "!message hi" using User's message ID
Bot says "hi"
我已經知道如何讓它復制我的消息,但我很難讓它刪除它們.我不想說它會在消息成為之前刪除消息,否則在繁忙的服務器上它可能無法正常工作.我想獲取命令消息的 ID,然后使用它的 ID 將其刪除.
I have figured out how to get it to copy my messages, but I am having difficulty getting it to delete them. I didn't want to say that it deletes the message before it's one otherwise on busy servers it might not work. I wanted to get the command message's ID then delete it using it's ID.
推薦答案
更新為 discord.py v1.2+
您應該使用 TextChannel.fetch_message
函數.
msg = await channel.fetch_message(message_id)
await msg.delete()
原始答案(不再有效):
要回答這個問題:要按 ID 刪除消息,您必須獲取消息對象(首選)或通過 client.http
(非首選)
您可以使用 Client.get_message
函數
You can use the Client.get_message
function
msg = await client.get_message(channel, message_id)
或者,您的特定用例似乎只是刪除已發送的消息,因此您可以只使用 on_message(msg)
提供的消息收到消息后,您可以:
Alternately, your specific use case seems to just be deleting the message that was sent, so you could just use the message supplied by on_message(msg)
After you have the message, you can do:
await client.delete_message(msg)
第二個選項:使用client.http
假設你知道頻道的 ID,你可以簡單地調用
Second option: Using client.http
Assuming you know the channel's ID, you can simply call
await client.http.delete_message(channel_id, message_id)
這種方法雖然對刪除任意位置的任意消息很有用,但如果獲取消息是可行的選擇,則不應使用此方法.
This method while useful for deleting arbitrary messages in arbitrary places shouldn't be used if getting the message is feasably an option.
這篇關于如何使用 discord.py 按 ID 刪除特定消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!