How to track users who deleted messages in discord.js?
P粉760675452
2023-08-29 17:00:40
<p>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. </p>
<p>I tried <code>message.author</code>, but of course, that logs who sent the message, and I don't know much of the syntax so didn't try anything else. </p>
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.