简体   繁体   English

Discord.js 删除角色

[英]Discord.js remove roles

I have this issue on my bot command.我的机器人命令有这个问题。 It doesn't remove any other specified roles other than the first defined one.它不会删除除第一个定义的角色之外的任何其他指定角色。

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);
}

This is the part where everything is defined.这是定义一切的部分。

Your callback function for find() is incorrect, and actually find() returns the first found element in an array.您对find()的回调 function 不正确,实际上find() 返回数组中找到的第一个元素 You could use .filter() to get a collection of elements instead and use the .includes() method to check if the role name is in the list provided.您可以使用.filter()获取元素集合,并使用.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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM