How to track users who deleted messages in discord.js?
P粉760675452
P粉760675452 2023-08-29 17:00:40
0
1
528

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.

P粉760675452
P粉760675452

reply all (1)
P粉709307865

You can usemessageDeleteevent, 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 thefetchAuditLogsmethod to get the audit log of the server where the deleted message was located.

const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, ], partials: [ Partials.Channel, Partials.GuildMember, Partials.Message, ], }); client.on('messageDelete', async (message) => { const logs = await message.guild.fetchAuditLogs({ type: AuditLogEvent.MessageDelete, limit: 1, }); // logs.entries is a collection, so grab the first one const firstEntry = logs.entries.first(); const { executorId, target, targetId } = firstEntry; // Ensure the executor is cached const user = await client.users.fetch(executorId); if (target) { // The message object is in the cache and you can provide a detailed log here console.log(`A message by ${target.tag} was deleted by ${user.tag}.`); } else { // The message object was not cached, but you can still retrieve some information console.log(`A message with id ${targetId} was deleted by ${user.tag}.`); } });

In discord.js v14.8, there is a new eventGuildAuditLogEntryCreate. You can find out who deleted the message immediately when you receive the corresponding audit log event (GuildAuditLogEntryCreate). It requires theGuildModerationintent to be enabled.

const { AuditLogEvent, Events } = require('discord.js'); client.on(Events.GuildAuditLogEntryCreate, async (auditLog) => { // Define your variables const { action, executorId, target, targetId } = auditLog; // Check only for deleted messages if (action !== AuditLogEvent.MessageDelete) return; // Ensure the executor is cached const user = await client.users.fetch(executorId); if (target) { // The message object is in the cache and you can provide a detailed log here console.log(`A message by ${target.tag} was deleted by ${user.tag}.`); } else { // The message object was not cached, but you can still retrieve some information console.log(`A message with id ${targetId} was deleted by ${user.tag}.`); } });
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!