javascript - 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);
}
这是定义一切的部分。
解决方案
您的回调函数find()
不正确,实际上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);
}
推荐阅读
- python - 在 python 中将 lambda 与 groupby 一起使用时的 SettingWithCopyWarning
- html - 如何使框(div)位于我之前放置的框下方
- ruby-on-rails - 我应该写什么来代替 ***** 或者我如何在 RSpec 中访问这个本地变量
- python - 如何用python循环遍历json?
- kernel - 内核中的 OpenCL 分析功能
- django - 在 django-allauth: integrations with Kakao 中,我输入了文档中的所有键
- python - 迭代 Json 并在 python 中构建新的 json
- oracle - 使用 for 循环删除前 10 行
- reactjs - 实现 redux-persist
- javascript - 表单中的标签标签未出现