尝试在 discord.js 上获取消息 pin 审核日志不会 return 最近固定消息的日志

Attempting to get message pin audit logs on discord.js does not return the log for the most recently pinned message

所以我正在尝试让我的机器人记录消息固定和取消固定。此代码过去一直有效,但现在由于某种原因,获取的日志中没有固定触发 messageUpdate 事件的消息的条目。

const fetchedLogs = await oldMessage.guild.fetchAuditLogs({
      type: 'MESSAGE_PIN',
    });
const pinLog = fetchedLogs.entries.filter(f => f.extra.messageID == oldMessage.id).first();
const { executor } = pinLog;

显示的错误是:

Uncaught Promise Rejection TypeError: Cannot destructure property 'executor' of 'pinLog' as it is undefined.

这个确切的代码直到最近才有效。

我尝试过的事情:

  1. 试图让代码在获取日志之前休眠一段时间(最多 10 秒)。没用
  2. 试图将获取放入循环,直到 pinLog 不再是未定义的。导致无限循环。

从 v13 开始,id 属性的命名已更改(如 guide 所示)。而不是 .xxxID,现在是 .xxxId

// messageID -> messageId
const pinLog = fetchedLogs.entries.filter(f => f.extra.messageId == oldMessage.id).first()

另请注意,.find() 更适合这种情况,因为它会获得与回调匹配的第一个对象

const pinLog = fetchedLogs.entries.find(f => f.extra.messageId == oldMessage.id)