简体   繁体   English

Discord.js - 正确使用:for (const guild of client.guilds.cache)?

[英]Discord.js - Correct use of: for (const guild of client.guilds.cache)?

I have (what probably is a very simple) question:我有(可能是一个非常简单的)问题:

I have been using我一直在使用


client.guilds.cache.map((guild) => {

and so on, and that works fine but I know that as the elements increase, a for loop is more efficient.依此类推,效果很好,但我知道随着元素的增加,for循环会更有效。

I tried:我试过了:

for (const guild of client.guilds.cache) {
  console.log(guild.name)

but that returns: undefined但返回:未定义

If I console.log(guild) I get the guild info in the console and name is absolutely a part of is如果我 console.log(guild) 我在控制台中获得公会信息,并且名称绝对是其中的一部分

From console:从控制台:

[
  '',
  Guild {
    members: GuildMemberManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular]
    },
    channels: GuildChannelManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular]
    },
    roles: RoleManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular]
    },
    presences: PresenceManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]]
    },
    voiceStates: VoiceStateManager {
      cacheType: [class Collection extends Collection],
      cache: Collection [Map] {},
      guild: [Circular]
    },
    deleted: false,
    available: true,
    id: '',
    shardID: 0,
    name: "GA's Testingserver!",
    icon: '11a80a894a50cafd46c95451ed83f939',
    splash: null,
    discoverySplash: null,
    region: 'europe',
    memberCount: 5,
    large: false,
    features: [],
    applicationID: null,
    afkTimeout: 300,
    afkChannelID: null,
    systemChannelID: '',
    embedEnabled: undefined,
    premiumTier: 0,
    premiumSubscriptionCount: 0,
    verificationLevel: 'NONE',
    explicitContentFilter: 'DISABLED',
    mfaLevel: 0,
    joinedTimestamp: 1598456514616,
    defaultMessageNotifications: 'ALL',
    systemChannelFlags: SystemChannelFlags { bitfield: 0 },
    maximumMembers: 100000,
    maximumPresences: null,
    approximateMemberCount: null,
    approximatePresenceCount: null,
    vanityURLCode: null,
    vanityURLUses: null,
    description: null,
    banner: null,
    rulesChannelID: null,
    publicUpdatesChannelID: null,
    preferredLocale: 'en-US',
    ownerID: '',
    emojis: GuildEmojiManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular]
    }
  }
]

For some reason, I can't wrap my head around this...出于某种原因,我无法解决这个问题......

According to discord.js documentation , guild.cache has type collection not array .根据discord.js 文档guild.cache具有类型collection而不是array

You can iterate over each guild in the collection:您可以遍历集合中的每个guild

client.guilds.cache.each(guild => {
    console.log(guild.name);
});

Or you can iterate over each guild in the array:或者您可以遍历数组中的每个guild

for (const guild of client.guilds.cache.array()) {
    console.log(guild.name);
}

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

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