How can I prevent the messageDelete event from emitting if the bot deletes the message?

I dont want emit the messageDelete when i use message.delete()
14 Replies
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs16mo ago
It's good practice to make your bots ignore other bots, including itself. This can be done through a single line of code:
if (<message>.author.bot) return;
if (<message>.author.bot) return;
<message> being whatever you defined message as.
monbrey
monbrey16mo ago
Thats not what they asked though. The bot might be deleting messages from other people too
AlexInCube
AlexInCube16mo ago
I will ask another one, how do I know who deleted the message? I need the event to be triggered if a user deleted the message. And if it was deleted by a bot, then it should not be triggered. It doesn't matter who originally sent the message.
monbrey
monbrey16mo ago
You need to check the audit logs. You can do this either from the messageDelete event, or the guildAuditLogEntryCreate event An audit log is not created if a bot deleted a message, or a user deleted their own message
AlexInCube
AlexInCube16mo ago
I will try to experiment with guildAuditLogEntryCreate
ncls.
ncls.16mo ago
Assuming that you just don't want it to be triggered when the same bot deletes a message, another very trivial idea would be to create a space in your client variable like client.deletedMessages where you store the message IDs when you delete a message in an array and then your messageDelete event handler checks if the message ID of the deleted message is inside that array and if it is, it takes it out and returns and if it's not, it just does what it would usually do.
AlexInCube
AlexInCube16mo ago
guildAuditLogEntryCreate is not emitted if message have only embeds and no content...
monbrey
monbrey16mo ago
I dont see why it wouldnt be Unless its for one of the reasons I said, bot deleted the message or user deleted their own message
AlexInCube
AlexInCube16mo ago
i delete the bot messages in Discord Gui and when i delete the bot message with text like "Hello world" event is triggered but when bot message have only embeds, event is not triggered
monbrey
monbrey16mo ago
¯\_(ツ)_/¯ We cant control what the API does
AlexInCube
AlexInCube16mo ago
i know I'm tired of using workarounds, but I guess I'll have to think of something else
ncls.
ncls.16mo ago
My "workaround" would work without the audit log but it depends on your use case if it's suitable
AlexInCube
AlexInCube16mo ago
I used this method after all, the others don't work for me thanks you