简体   繁体   English

Discord JS - 当我加入更多公会时,使用 client.guilds 的 .cache 属性会导致错误吗?

[英]Discord JS - Will using the .cache property of client.guilds cause an error further down the line when I am in more guilds?

I am trying to find the guild owner ID for all of my guilds to send them a direct message at a given time.我正在尝试查找我所有公会的公会所有者 ID,以便在给定时间向他们发送直接消息。 My current implementation where I know all the guildIDs already and just iterate through them: message.client.guilds.cache.get(guildID).ownerID This currently works and is fine but I wonder about its robustness when I increase my guild count.我当前的实现,我已经知道所有的 guildID 并且只是遍历它们: message.client.guilds.cache.get(guildID).ownerID这目前可以工作并且很好,但我想知道当我增加我的公会计数时它的稳健性。 I am currently testing on just 2 guilds but could reach upwards of 1000.我目前仅在 2 个公会上进行测试,但可能会达到 1000 个以上。

Lots of the older tutorials and questions on this site suggest not needing to use the .cache property of message.client.guilds and just getting your desired property like this message.client.guilds.get(desired_property) However when I try and go by their advice I get an error.该站点上的许多较旧的教程和问题都建议不需要使用message.client.guilds.cache属性,而只需获取所需的属性,例如message.client.guilds.get(desired_property)但是当我尝试使用 go 时他们的建议我得到一个错误。 This question had a similar problem to the one I was having but then fixed it when I used the .cache property of message.client which solved my problem previous problem.这个问题与我遇到的问题有类似的问题,但是当我使用message.client.cache属性时解决了它,这解决了我以前的问题。 I then think I found why as this answer to a similar questions tells me that all user objects have been cached然后我想我找到了原因,因为这个对类似问题的回答告诉我所有用户对象都已被缓存

What will this mean when I have upwards of 1000 guilds?当我有超过 1000 个公会时,这意味着什么? Dont worry about sharding that is something I can think about later.不要担心分片,这是我以后可以考虑的事情。

I have a loose understanding of how a cache works but will this therefore break down when I get into more guilds as not all of the guilds will be cached and therefore when I use message.client.guilds.cache.get(guildID).ownerID will I only get the ownerIDs of those guilds that have been cached or are all of the guilds the bot is in cached all the time?我对缓存的工作原理有一个松散的理解,但是当我进入更多公会时这会因此崩溃,因为并非所有公会都会被缓存,因此当我使用message.client.guilds.cache.get(guildID).ownerID我会只获取那些已被缓存的公会的所有者ID,还是机器人一直在缓存的所有公会? If this will not always return all the ownerIDs when the bot begins having thousands of guilds and therefore potentially not all of the guilds are cached is there a way in which I can fix it so it will always return them all?如果当机器人开始拥有数千个公会时这并不总是返回所有的 ownerID,因此可能不是所有的公会都被缓存,有没有办法可以修复它,所以它总是会全部返回?

You can also use .map :您还可以使用.map

let list = bot.guilds.cache.map(g => g.ownerID).join('\n');
message.channel.send(list)

Not sure about cache errors though, I've had some with users, but that was kind of expected.虽然不确定缓存错误,但我与用户有过一些错误,但这是意料之中的。 (used .users.fetch instead, that's a promise so make sure to use await if you're going to try that) (使用.users.fetch代替,那是 promise 所以如果你要尝试,请确保使用await

( [ ${client.guilds.cache.map(g => g.name).join(", \n ")} ] ); ( [ ${client.guilds.cache.map(g => g.name).join(", \n ")} ] );

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

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