Discord.js 删除角色

时间:2021-03-26 08:19:11

标签: javascript node.js discord.js

我的机器人命令有这个问题。除了第一个定义的角色之外,它不会删除任何其他指定的角色。

if (args[0] === "yes") {
    const exists = users.find((u) => u.name === message.member.user.tag);
    if (!exists) {
        return message.channel.send("You are not registered into a clan.");
    }
    await RegistryModel.findOneAndDelete({ name: message.member.user.tag });

    let gangroles = message.guild.roles.cache.find(role => role.name === "Eliminaries", "Street Legends/Locos", "The Circle", "Critical Killers", "Crime Master", "Brazil Printer Mafia", "Saint Bude", "Communist Party Of Nevada", "The Cola Association", "National Choppa", "Squad Of Skilled", "Myth", "Shelby Family", "Shooters Family Gang", "Terminator", "Century Street Gang", "Phoenix Core", "Knights of Despair", "Garuda Team", "Sando Gang", "Liberators", "Celestial Blue", "Mystic", "Taniman", "Crimson", "Black Blood Mafia", "Crypts", "Terror", "Hydras");
    message.member.roles.remove(gangroles.id).catch(err => console.log(err))
    message.channel.send(registerdone);
}

这是定义一切的部分。

1 个答案:

答案 0 :(得分:1)

您对 find() 的回调函数不正确,实际上是数组中的 find() returns the first found element。您可以改用 .filter()get a collection of elements,并使用 .includes() 方法检查角色名称是否在提供的 list 中。

if (args[0] === 'yes') {
  const exists = users.find((u) => u.name === message.member.user.tag);
  if (!exists) {
    return message.channel.send('You are not registered into a clan.');
  }
  await RegistryModel.findOneAndDelete({ name: message.member.user.tag });

  const list = [
    'Eliminaries',
    'Street Legends/Locos',
    'The Circle',
    'Critical Killers',
    'Crime Master',
    'Brazil Printer Mafia',
    'Saint Bude',
    'Communist Party Of Nevada',
    'The Cola Association',
    'National Choppa',
    'Squad Of Skilled',
    'Myth',
    'Shelby Family',
    'Shooters Family Gang',
    'Terminator',
    'Century Street Gang',
    'Phoenix Core',
    'Knights of Despair',
    'Garuda Team',
    'Sando Gang',
    'Liberators',
    'Celestial Blue',
    'Mystic',
    'Taniman',
    'Crimson',
    'Black Blood Mafia',
    'Crypts',
    'Terror',
    'Hydras',
  ];

  // get a collection of roles that have names included in the list array
  let gangroles = message.guild.roles.cache.filter((role) =>
    list.includes(role.name)
  );
  // remove every matching roles
  gangroles.each((r) => {
    message.member.roles.remove(r.id).catch((err) => console.log(err));
  });

  message.channel.send(registerdone);
}