如何使用 discord.py 按 ID 删除特定消息 [英] How to delete a specific message by ID using discord.py

查看:21
本文介绍了如何使用 discord.py 按 ID 删除特定消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用它的 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 删除特定消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆