将2个JSON数组合并为成员角色(DISCORD.JS)

时间:2018-01-24 16:47:04

标签: javascript arrays json node.js discord.js

我正在尝试合并配置文件中的2个JSON数组,以将它们推送到成员角色

JSON配置

{
    "adminRoles": [
        "ADMIN",
        "LA REINA DE BELLEZA"
    ],
    "moderatorRoles": [
        "MODERATOR",
        "BOT"
    ]
}

然后将组合的2个数组推入此

// This command must be limited to mods and admins.
if (!message.member.roles.some(r => [COMBINED_ROLES_HERE].includes(r.name))) {
return message.reply("Sorry, you don't have permissions to use this!");
}

请有人指出我正确的方向。

我试过

console.log(client.config.adminRoles.concat(client.config.moderatorRoles));

然后返回

[ "ROLE 1", "ROLE 2" etc... ]

但这不起作用。我也试过.join()并且可以返回

"ROLE 1", "ROLE 2" etc...

但这也行不通。

1 个答案:

答案 0 :(得分:1)

const roles = {
  "adminRoles": [
    "ADMIN",
    "LA REINA DE BELLEZA"
  ],
  "moderatorRoles": [
    "MODERATOR",
    "BOT"
  ]
}

const message = {
  member: {
    roles: [
      { name: 'testing 123' }
    ]
  }
};

// then push the combined 2 arrays into this
const combinedRoleNames = Object.keys(roles);
const combineRoles = combinedRoleNames
  .map(x => roles[x])
  .reduce((acc, curr) => acc.concat(curr), []);

console.log(combinedRoleNames);
console.log(combineRoles);

// if you want to assign to member.roles, uncomment following line
// message.member.roles = combineRoles.map(name => ({ name }));

if (!message.member.roles.some(r => combineRoles.includes(r.name))) {
  console.log('sorry');
  // return message.reply("Sorry, you don't have permissions to use // this!");
} else {
  console.log('found');
}