簡體   English   中英

Discord.js 刪除角色

[英]Discord.js remove roles

我的機器人命令有這個問題。 它不會刪除除第一個定義的角色之外的任何其他指定角色。

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

這是定義一切的部分。

您對find()的回調 function 不正確,實際上find() 返回數組中找到的第一個元素 您可以使用.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