Home  >  Q&A  >  body text

How to track users who deleted messages in discord.js?

<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>
P粉760675452P粉760675452417 days ago582

reply all(1)I'll reply

  • P粉709307865

    P粉7093078652023-08-30 15:32:16

    You can use messageDeleteevent, 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, and GuildMessages. You'll also need partials: Channel, Message, and GuildMember, 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.

    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 event GuildAuditLogEntryCreate. You can find out who deleted the message immediately when you receive the corresponding audit log event (GuildAuditLogEntryCreate). It requires the GuildModeration intent 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}.`);
      }
    });
    

    reply
    0
  • Cancelreply