簡體   English   中英

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

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

我有(可能是一個非常簡單的)問題:

我一直在使用


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

依此類推,效果很好,但我知道隨着元素的增加,for循環會更有效。

我試過了:

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

但返回:未定義

如果我 console.log(guild) 我在控制台中獲得公會信息,並且名稱絕對是其中的一部分

從控制台:

[
  '',
  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]
    }
  }
]

出於某種原因,我無法解決這個問題......

根據discord.js 文檔guild.cache具有類型collection而不是array

您可以遍歷集合中的每個guild

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

或者您可以遍歷數組中的每個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