从审核日志中获取最新禁令

时间:2020-05-31 09:50:18

标签: javascript discord.js

我正在尝试编写一个不和谐的bot,以查找审计日志中的最新禁令。
我目前有:

client.on('guildBanAdd', guild => {
  guild.fetchAuditLogs().then(logs => {
    logs.entries.filter(l => l.action === 'MEMBER_BAN_ADD')
      .forEach(log => {
        if (Date.now() - log.createdTimestamp > 1000) return

        const logsChannel = guild.channels.find(ch => ch.name === 'bans-logs')
        const embed = new Discord.RichEmbed()
          .setDescription(`**New Ban**
            **${log.executor.tag}** banned **${log.target.tag}**`)
          .setColor("RED")
          .setTimestamp(log.createdTimestamp)

        logsChannel.send(embed)
      })
  })
})

我考虑使用.first(),因为这是一个集合,但是我不确定日志是否按日期排序...

1 个答案:

答案 0 :(得分:1)

我认为最好的方法是获取审核日志并按日期对它们进行排序,因为正如您还说过的那样,我不确定您是否会始终按时间顺序获取它们。
您需要:

  • 获取所有审核日志
  • 按日期对条目进行排序:您可以使用在Dates属性中找到的createdAt
  • 获取最新版本,现在应该是第一个

这是我要怎么做:

client.on('guildBanAdd', guild => {
  guild.fetchAuditLogs()
    .then(logs => {
      let ban = logs.entries
        .filter(e => e.action === 'MEMBER_BAN_ADD')
        .sort((a, b) => b.createdAt - a.createdAt) // Reverse chronological order
        .first() // Get the first, which is the latest

      // You can now send your embed using the entry stored in 'ban'
    })
})