【问题标题】:Combining 2 JSON Arrays into member roles (DISCORD.JS)将 2 个 JSON 数组组合成成员角色 (DISCORD.JS)
【发布时间】:2018-07-03 19:15:26
【问题描述】:

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

JSON 配置

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

然后将合并后的2个数组push进去

// 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...

但这也行不通。

【问题讨论】:

  • 当您的值为[ "ADMIN", "LA REINA DE BELLEZA" ][ "MODERATOR", "BOT" ] 时,您如何获得[ "ROLE 1", "ROLE 2" etc... ]
  • 我写这些只是为了说明退货的风格。我实际上是在返回 ADMIN、LA REINA DE BELLEZA 等......
  • 好的。你想达到什么目标?请使用message.member.roles更新问题。
  • 所以当我做if (!message.member.roles.some(r => ["ADMIN", "MODERATOR"].includes(r.name))) { 时它可以工作,所以当我连接然后加入返回“ADMIN”、“MODERATOR”等时,它应该可以工作,但机器人正在回复带有我没有正确权限的消息,这是错误的,因为我具有管理员不和谐角色。所以我想知道这是discord.js的限制还是我做错了什么。
  • 记录COMBINED_ROLES_HERE 的值并检查它是否已经是一个数组。

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


【解决方案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');
}

【讨论】:

  • 所以你说角色不能存在于 config.json 文件中?
  • 顺便说一句,我只是质疑角色是否仍然可以存在于可以使用 client.config.adminRoles 等访问的 config.json 文件中...
  • 你可以用你的json值替换,上面的ocde是为了演示目的。
  • const combinedRoleNames = Object.keys(roles);这一行替换为const combinedRoleNames = Object.keys(client.config);,然后就可以去掉roles这个变量了。
猜你喜欢
  • 2020-11-16
  • 2022-10-01
  • 2021-11-03
  • 2021-08-14
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
相关资源
最近更新 更多