无效的 Discord.JS 服务器成员缓存响应

huangapple go评论68阅读模式
英文:

Invalid Discord.JS Guild Member Cache Responses

问题

我的机器人运行着一个提醒系统。在过去的6-8个月里,这个系统一直运行得很顺利,没有出现任何问题。最近它开始对特定用户不起作用,经过调查,当我查看它所在的服务器的缓存时,只返回了两个成员。它返回了我的用户(我不是Discord的所有者),以及它自己。我以前从未遇到过这个问题,所以我不知道为什么它不起作用。使用Fetch方法不起作用,因为它不允许我向获取的用户发送直接消息。

而且,令人困惑的是,这个服务器实际上有104个成员。这就是我不明白为什么它只返回2个成员。

我尝试过使用缓存的成员、获取成员、从缓存中获取服务器、根据频道成员查找成员。

无效的 Discord.JS 服务器成员缓存响应

这是提醒系统正在使用的代码。

function reminderCheck() {
  if(settingsCheck('Reminders') === true){
    let array = fs.readdirSync(`${__dirname}/reminders/`)
    const currentDate = new Date();
    const timestampInMs = currentDate.getTime();
    const unixTimestamp = Math.floor(currentDate.getTime() / 1000);
    array.forEach(async (element) => {
      let rawData = fs.readFileSync(`${__dirname}/reminders/${element}`)
      let jsonData = JSON.parse(rawData);
      if(jsonData.time < unixTimestamp){
        await client.guilds.cache.get(config.guildID).members.cache.get(jsonData.user_id).send({ content: `<@${jsonData.user_id}> **SCHEDULED REMINDER** \n<t:${jsonData.time}>: ${jsonData.reminder}`})
        fs.unlinkSync(`${__dirname}/reminders/${element}`)
        console.log(client.guilds.cache)
      }
    })
  }
}
英文:

My bot runs a reminder system. For the past 6-8 months, this system has been working without a single issue. Recently it started to not work for a particular user, and upon investigating, when I look at my cache for the Guild it's running off, it only returns two members. It returns my user (I do not own the Discord), and itself. I've never had this issue before, so I'm not sure why it's not working. Using the Fetch method does not work, as it won't let me send a DM to a user it fetches.

And for reference, the Guild actually has 104 members. That's why I'm confused why it's returning just 2.

I've tried using cached members, fetching members, getting the guild from the cache, finding members based on channel members.

无效的 Discord.JS 服务器成员缓存响应

Here is the code that the reminder system is using.

  if(settingsCheck(&#39;Reminders&#39;) === true){
    let array = fs.readdirSync(`${__dirname}/reminders/`)
    const currentDate = new Date();
    const timestampInMs = currentDate.getTime();
    const unixTimestamp = Math.floor(currentDate.getTime() / 1000);
    array.forEach(async (element) =&gt; {
      let rawData = fs.readFileSync(`${__dirname}/reminders/${element}`)
      let jsonData = JSON.parse(rawData);
      if(jsonData.time &lt; unixTimestamp){
        await client.guilds.cache.get(config.guildID).members.cache.get(jsonData.user_id).send({ content: `&lt;@${jsonData.user_id}&gt; **SCHEDULED REMINDER** \n&lt;t:${jsonData.time}&gt;: ${jsonData.reminder}`})
        fs.unlinkSync(`${__dirname}/reminders/${element}`)
        console.log(client.guilds.cache)
      }
    })
  }
}

</details>


# 答案1
**得分**: 1

我与Discord.JS的工作人员进行了交流,他们能够帮助我。如果缓存不是最新的,您可以运行client.guilds.cache.get(GuildID).members.fetch(),这将更新缓存。在Discord.JS中,没有fetchAllMembers这个函数。

<details>
<summary>英文:</summary>

I spoke with Discord.JS&#39;s staff team and they were able to help me out. If the cache isn&#39;t up to date, you can run client.guilds.cache.get(GuildID).members.fetch(), and this will update the cache. fetchAllMembers is not a function in Discord.JS.

</details>



# 答案2
**得分**: -1

这个问题几乎总是由一个原因引起的,那就是你没有启用意图。

转到[开发者门户][1]

然后在你的机器人上启用这些意图
[![开发者门户意图][2]][2]

此外,考虑使用[CRON][3],因为它可能简化你的提醒系统。

[1]: https://discord.com/developers/applications/
[2]: https://i.stack.imgur.com/WQbwE.png
[3]: https://www.npmjs.com/package/node-cron

<details>
<summary>英文:</summary>

This issue almost always leads to one reason and thats that you dont have intents enabled. 

Go to [Developer Portal][1]


Then enable these intents on your bot
[![Developer Portal Intents][2]][2]


Also consider using [CRON][3] as it may simplify your reminder system


  [1]: https://discord.com/developers/applications/
  [2]: https://i.stack.imgur.com/WQbwE.png
  [3]: https://www.npmjs.com/package/node-cron

</details>



huangapple
  • 本文由 发表于 2023年6月1日 17:33:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定