我正在尝试编写一个不和谐的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()
,因为这是一个集合,但是我不确定日志是否按日期排序...
答案 0 :(得分:1)
我认为最好的方法是获取审核日志并按日期对它们进行排序,因为正如您还说过的那样,我不确定您是否会始终按时间顺序获取它们。
您需要:
这是我要怎么做:
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'
})
})