I am trying to delete a message using it's ID. I am using discord.py.
Logic Flow
User sends command. example: !message hi
Bot deletes "!message hi" using User's message ID
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.
You should use the TextChannel.fetch_message
function.
msg = await channel.fetch_message(message_id)
await msg.delete()
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)
You can use the Client.get_message
function
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)
After you have the message, you can do:
await client.delete_message(msg)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With