I've just started learning how to create a discord bot and I'm trying to figure out how to log who deleted a message.
I tried message.author
, but of course, that logs who sent the message, and I don't know much of the syntax so didn't try anything else.
You can use
messageDelete
event, which is triggered when a message is deleted. You can check the audit log to see if a user deleted other users' messages.First, make sure you have the required intents:
Guilds
,GuildMembers
, andGuildMessages
. You'll also needpartials
:Channel
,Message
, andGuildMember
, to handle messages sent before your bot comes online.Once a message is deleted, you can use the
fetchAuditLogs
method to get the audit log of the server where the deleted message was located.In discord.js v14.8, there is a new event
GuildAuditLogEntryCreate
. You can find out who deleted the message immediately when you receive the corresponding audit log event (GuildAuditLogEntryCreate
). It requires theGuildModeration
intent to be enabled.