Discord机器人,如何获取所有已连接的“公会/服务器”

时间:2020-05-24 05:46:56

标签: javascript bots discord.js

我正在尝试使用client.guilds,但似乎只是返回了这个我不知道如何解析的真正奇怪的对象。

对象:

GuildManager {
  cacheType: [Function: Collection],
  cache: Collection(1) [Map] {
    '11111111111111111' => Guild {
      members: [GuildMemberManager],
      channels: [GuildChannelManager],
      roles: [RoleManager],
      presences: [PresenceManager],
      voiceStates: [VoiceStateManager],
      deleted: false,
      available: true,
      id: '1111111111111111111',
      shardID: 0,
      name: 'My Server Name',
      icon: '11111111111111111111111111111',
      splash: null,
      region: 'us-west',
      memberCount: 46,
      large: false,
      features: [],
      applicationID: null,
      afkTimeout: 300,
      afkChannelID: null,
      systemChannelID: null,
      embedEnabled: undefined,
      premiumTier: 0,
      premiumSubscriptionCount: 0,
      verificationLevel: 'NONE',
      explicitContentFilter: 'DISABLED',
      mfaLevel: 0,
      joinedTimestamp: 1590266012969,
      defaultMessageNotifications: 'ALL',
      systemChannelFlags: [SystemChannelFlags],
      vanityURLCode: null,
      description: null,
      banner: null,
      rulesChannelID: null,
      publicUpdatesChannelID: null,
      ownerID: '111111111111111111',
      emojis: [GuildEmojiManager]
    }
  }
}

我只是不知道如何解析它来获取我连接的服务器的名称。现在我只连接到一台服务器,但是将来会有更多,我只想说:

client.guilds.forEach(server => {
    console.log(guild.name+" id: "+guild.id);
});

2 个答案:

答案 0 :(得分:0)

该对象是您可以迭代的基本集合。

begin()

答案 1 :(得分:0)

原来,我不得不引用缓存对象:

client.guilds.cache.forEach(server => {
    console.log(server.name + " id: " + server.id);
});