Discord.js:roles.some(r => !message.member.roles.cache.has(r)) 不起作用
Discord.js: roles.some(r => !message.member.roles.cache.has(r)) doesn't work
所以我从单一的 STAFF 命令(例如 if(message.member.roles.cache.has(staff_id))
)切换到更灵活的方法 - 如果成员有助手或 mod 或管理员角色等。我使用 some
像这样的数组方法:
const roles = [
"859471924753072188",
"836214627243262002",
"922170188089139261",
"836214626923315221",
"922170903394127873",
"859878718599987200",
"836214626617655296"
]
const isntStaff = roles.some(r => !message.member.roles.cache.has(r))
现在每当我在某些代码中使用它时,例如在这个代码中:
...
if (
rudeWords.some(word =>
message
.toString()
.toLowerCase()
.includes(word)
) &&
isntStaff
) {
message.delete({ reason: `Blacklisted Word Detected` });
message.channel
.send({ content: `${message.author}, do not use that word here, thank you.` })
.then(msg => {
setTimeout(() => msg.delete(), 3000)
})
即使我有助手或 mod 或管理员命令,它也不允许我使用这个词。
它还在控制台中不记录任何内容。
您的代码需要这些角色中的每一个。您正在检查该数组中是否有任何 不在 成员角色缓存中的角色。相反,将 !
放在外面
const roles = [
"859471924753072188",
"836214627243262002",
"922170188089139261",
"836214626923315221",
"922170903394127873",
"859878718599987200",
"836214626617655296"
]
const isntStaff = !roles.some(r => message.member.roles.cache.has(r))
根据这个Discord.js Guide.
还有另一种方法可以做到这一点
这是您如何执行此操作的示例:
// Check if they have one of many roles
const roles = [
"859471924753072188",
"836214627243262002",
"922170188089139261",
"836214626923315221",
"922170903394127873",
"859878718599987200",
"836214626617655296"
]
if (message.member.roles.cache.some(r => roles.includes(r.id)) ) {
// has one of the roles
}else {
// has none of the roles
}
所以我从单一的 STAFF 命令(例如 if(message.member.roles.cache.has(staff_id))
)切换到更灵活的方法 - 如果成员有助手或 mod 或管理员角色等。我使用 some
像这样的数组方法:
const roles = [
"859471924753072188",
"836214627243262002",
"922170188089139261",
"836214626923315221",
"922170903394127873",
"859878718599987200",
"836214626617655296"
]
const isntStaff = roles.some(r => !message.member.roles.cache.has(r))
现在每当我在某些代码中使用它时,例如在这个代码中:
...
if (
rudeWords.some(word =>
message
.toString()
.toLowerCase()
.includes(word)
) &&
isntStaff
) {
message.delete({ reason: `Blacklisted Word Detected` });
message.channel
.send({ content: `${message.author}, do not use that word here, thank you.` })
.then(msg => {
setTimeout(() => msg.delete(), 3000)
})
即使我有助手或 mod 或管理员命令,它也不允许我使用这个词。 它还在控制台中不记录任何内容。
您的代码需要这些角色中的每一个。您正在检查该数组中是否有任何 不在 成员角色缓存中的角色。相反,将 !
放在外面
const roles = [
"859471924753072188",
"836214627243262002",
"922170188089139261",
"836214626923315221",
"922170903394127873",
"859878718599987200",
"836214626617655296"
]
const isntStaff = !roles.some(r => message.member.roles.cache.has(r))
根据这个Discord.js Guide.
还有另一种方法可以做到这一点这是您如何执行此操作的示例:
// Check if they have one of many roles
const roles = [
"859471924753072188",
"836214627243262002",
"922170188089139261",
"836214626923315221",
"922170903394127873",
"859878718599987200",
"836214626617655296"
]
if (message.member.roles.cache.some(r => roles.includes(r.id)) ) {
// has one of the roles
}else {
// has none of the roles
}