简体   繁体   中英

fetching audit logs in discord.js getting some error on trying to get the person who deleted message

i am trying to make a code that gives me log about who deleted the message but sometimes it gives me person other than the person who really deleted is that may be because of internet problem or there is something wrong with this code i am trying to get it from the audit logs

client.on('messageDelete', async msg => {
  let logs = await msg.guild.fetchAuditLogs({type: "MESSAGE_DELETED"});
  let entry = logs.entries.first();
  let messageDeletedEmbed = new Discord.RichEmbed()
        .setAuthor(`${msg.author.tag}`, msg.author.avatarURL)
        .setThumbnail(msg.author.avatarURL)
        .setColor("#e8de17")
        .setTitle('A message has been deleted 🧹')
        .addField("**Message Sent By :**", "<@" + msg.author.id + ">")
        .addField("**Message Channel :**", msg.channel) 
        .addField("**Message Content :**", msg.content)  
        .addField("**Deleted By :**", '<@' + `${entry.executor.id}` + '>') 
        .setTimestamp()
        .setFooter(`${msg.guild.name}`,msg.guild.iconURL);
        let LoggingChannel = msg.guild.channels.find(ch => ch.name === "logs")
        if(!LoggingChannel) return;
        if (msg.author == client.user) return;
        LoggingChannel.send(messageDeletedEmbed); 
});

Same thing happens to me, the only thing I could come up with was checking to see when the audit log was created, so if the log was created within say... the last 3 seconds, then it's very likely that it's the log you want, so you say the executor of that log deleted it. But if it wasn't, then it's not very likely at all that the audit log it finds is the right one, so you can say with decent certainty that the person who deleted it was the message.author .

`function deleteCheck() {
  if (entry.createdTimestamp > Date.now() - 2000) {
    return user;
  }

  else if (!entry.createdTimestamp > Date.now() - 1500) {
    return message.author.username;
  }

  else {
    return message.author.username;
  }
}`

This may be a little inefficient, but I basically had this send the return value of the function. entry is defined as: const entry = await message.guild.fetchAuditLogs({ type: 'MESSAGE_DELETE' }).then(audit => audit.entries.first()) and user is: let user = entry.executor.username .
I hope this helps you, and it may take a bit of experimenting, but I think you'll be fine :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM