messageDelete event and partials discord.js v12

Viewed 362

Lets say that I have a messageDelete event that I want to get the data from. Since I am required to have the Channel and Message partials for the messageDelete event to fire, the message object returned in the messageDelete event will be partial.

When I'm trying to fetch this message, it returns an error saying that the fetched message is unkown.

So how can I get information like the content etc. from the deleted message?

My current code:

client.on("messageDelete", async message => {
    if (message.partial) await message.fetch() // this is where the error occurs
    console.log(message.content) // will only work on non partial messages
})

Is there any way around this, cause it would be useful to get the information from past deleted messages.

EDIT

Toasty recommended that I use the audit logs, to which I have used the following code

client.on("messageDelete", async message => {
    console.log(message) // seeing whats avaliable in the return
    if (message.partial) console.log("message was partial") // checking if the message was partial to compare with non partial messages
    if (message.guild) {
        const fLogs = await message.guild.fetchAuditLogs({limit:1, type:"MESSAGE_DELETE"}) //getting audit logs
        const log = fLogs.entries.first()
        let {executor, target} = log
        console.log("Message deleted by "+executor.tag+" in "+target) // responding.
    }
})

Output:

message was partial
Message deleted by CT-1409 "Echo"#0093 in 606323576714559489

So I can get the who and the (sort of) what of the message that was deleted. I still cannot get the rest of the message information, as if I tried to fetch the message with the target id, it would give me Unkown Message again. But also when I logged the message object to start with, I noticed that there was a decent amount of information already present, which may mean some data would still be accessible from a partial message. I don't know how much, but perhaps enough for what I need.

1 Answers

That you can't get any information of the deleted message is probably because the message..has been deleted.

As you can read here, this is not possible and would also be a violation of the rules and a violation of the privacy of users.

But...

...if you have a command for deleting messages, you could get the information of the message before you delete it and do stuff with it

Alternatively, you could work with the audit logs

Related