如何使用discord.py通过ID删除特定邮件 [英] How to delete a specific message by ID using discord.py
问题描述
我正在尝试使用其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
Bot说 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.
推荐答案
要回答这个问题: ID,您必须获取消息对象(首选)或通过 client.http
(首选)
To answer the question: To delete a message by ID, you must either fetch the message object (preferred) or go through client.http
(not-preferred)
您可以使用 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)
编辑discord.py v1.2 +
您应使用 Channel.fetch_message
函数代替。
You should use the Channel.fetch_message
function instead.
msg = await channel.fetch_message(message_id)
await msg.delete()
第二种选择:使用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删除特定邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!