简体   繁体   English

在 discord.js 中获取审计日志在尝试获取删除消息的人时出错

[英]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.同样的事情发生在我身上,我唯一能想到的就是检查审计日志的创建时间,所以如果日志是在说......最后3秒内创建的,那么它很可能是你的日志想要,所以你说那个日志的executor删除了它。 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 .但如果不是,那么它找到的审计日志就不太可能是正确的,因此您可以相当肯定地说删除它的人是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 . entry 定义为: const entry = await message.guild.fetchAuditLogs({ type: 'MESSAGE_DELETE' }).then(audit => audit.entries.first())和 user 是: 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 :).我希望这对您有所帮助,可能需要进行一些试验,但我认为您会没事的 :)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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