[英]How to delete a specific message by ID using discord.py
I am trying to delete a message using it's ID.我正在尝试使用它的 ID 删除一条消息。 I am using discord.py.
我正在使用discord.py。
Logic Flow逻辑流程
User sends command.
用户发送命令。 example: !message hi
示例: !message hi
Bot deletes "!message hi" using User's message IDBot 使用用户的消息 ID 删除“!message hi”
Bot says "hi"机器人说“嗨”
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,然后使用它的 ID 删除它。
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)回答问题:要通过 ID 删除消息,您必须获取消息对象(首选)或通过
client.http
(非首选)
You can use the Client.get_message
function您可以使用
Client.get_message
函数
msg = await client.get_message(channel, message_id)
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)
或者,您的特定用例似乎只是删除发送的消息,因此您可以使用
on_message(msg)
提供的on_message(msg)
After you have the message, you can do:收到消息后,您可以执行以下操作:
await client.delete_message(msg)
Edit for discord.py v1.2+编辑 discord.py v1.2+
You should use the Channel.fetch_message
function instead.您应该改用
Channel.fetch_message
函数。
msg = await channel.fetch_message(message_id)
await msg.delete()
Assuming you know the channel's ID, you can simply call假设你知道频道的 ID,你可以简单地调用
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.如果可以选择获取消息,则不应使用此方法对于在任意位置删除任意消息很有用。
要删除并不获取信息使用TextChannel.delete_messages ,构建一个雪花使用discord.Object 。
await channel.delete_messages([discord.Object(id=message_id)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.