Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何读取服务器角色Discord.js的对象_Javascript_Node.js_Object_Discord_Discord.js - Fatal编程技术网

Javascript 如何读取服务器角色Discord.js的对象

Javascript 如何读取服务器角色Discord.js的对象,javascript,node.js,object,discord,discord.js,Javascript,Node.js,Object,Discord,Discord.js,我有下面的代码,它基本上将rolemager对象分配给变量roles var-roles=receivedMessage.guild.roles; log(角色['cache']); 现在,日志返回一些混乱(部分)如下所示: Collection [Map] { '529502688183058443' => Role { guild: Guild { members: [GuildMemberManager], channels: [GuildCha

我有下面的代码,它基本上将
rolemager
对象分配给变量
roles

var-roles=receivedMessage.guild.roles;
log(角色['cache']);
现在,日志返回一些混乱(部分)如下所示:

Collection [Map] {
  '529502688183058443' => Role {
    guild: Guild {
      members: [GuildMemberManager],
      channels: [GuildChannelManager],
      roles: [RoleManager],
      presences: [PresenceManager],
      voiceStates: [VoiceStateManager],
      deleted: false,
      available: true,
      id: '529502688183058443',
      shardID: 0,
      name: 'GuyDyamond Studios',
      icon: '36e1f32f9fc98818872ca4583d50cc52',
      splash: null,
      discoverySplash: null,
      region: 'us-east',
      memberCount: 6,
      large: false,
      features: [],
      applicationID: null,
      afkTimeout: 300,
      afkChannelID: null,
      systemChannelID: '529502688183058445',
      embedEnabled: undefined,
      premiumTier: 0,
      premiumSubscriptionCount: 0,
      verificationLevel: 'MEDIUM',
      explicitContentFilter: 'DISABLED',
      mfaLevel: 0,
      joinedTimestamp: 1600020773558,
      defaultMessageNotifications: 'ALL',
      systemChannelFlags: [SystemChannelFlags],
      maximumMembers: 250000,
      vanityURLCode: null,
      vanityURLUses: null,
      description: null,
      banner: null,
      rulesChannelID: null,
      publicUpdatesChannelID: null,
      preferredLocale: 'en-US',
      ownerID: '423162345108275213',
      emojis: [GuildEmojiManager]
    },
    id: '529502688183058443',
    name: '@everyone',
    color: 0,
    hoist: false,
    rawPosition: 0,
    permissions: Permissions { bitfield: 104324673 },
    managed: false,
    mentionable: false,
    deleted: false
  },
  '529672467200081922' => Role {
    guild: Guild {
      members: [GuildMemberManager],
      channels: [GuildChannelManager],
      roles: [RoleManager],
      presences: [PresenceManager],
      voiceStates: [VoiceStateManager],
      deleted: false,
      available: true,
      id: '529502688183058443',
      shardID: 0,
      name: 'GuyDyamond Studios',
      icon: '36e1f32f9fc98818872ca4583d50cc52',
      splash: null,
      discoverySplash: null,
      region: 'us-east',
      memberCount: 6,
      large: false,
      features: [],
      applicationID: null,
      afkTimeout: 300,
      afkChannelID: null,
      systemChannelID: '529502688183058445',
      embedEnabled: undefined,
      premiumTier: 0,
      premiumSubscriptionCount: 0,
      verificationLevel: 'MEDIUM',
      explicitContentFilter: 'DISABLED',
      mfaLevel: 0,
      joinedTimestamp: 1600020773558,
      defaultMessageNotifications: 'ALL',
      systemChannelFlags: [SystemChannelFlags],
      maximumMembers: 250000,
      vanityURLCode: null,
      vanityURLUses: null,
      description: null,
      banner: null,
      rulesChannelID: null,
      publicUpdatesChannelID: null,
      preferredLocale: 'en-US',
      ownerID: '423162345108275213',
      emojis: [GuildEmojiManager]
    },

现在,我已经尝试了所有方法,但是我似乎无法迭代对象
角色
。我尝试了一个
forEach
函数,一个
for…in…
循环,等等,什么都没有。此外,我试图获取每个角色的名称并将其放入数组中,但似乎无法获取
name
参数。有关如何执行此操作的任何帮助?

如果您只需要名称,可以使用
Array.prototype.map

console.log(
receivedMessage.guild.roles.cache.map((角色)=>role.name)
);

示例结果:

['Admin'、'Mod'、'VIP'、'Member'、'Muted'、'everybody']

要通过每个角色迭代函数,请使用:

receivedMessage.guild.roles.cache.forEach((角色)=>{
console.log(role.id);
});

非常感谢您!这确实有效。我没有想到遍历缓存,而是遍历整个角色。谢谢