問題描述
我為服務器制作了一個簡單的報價機器人,但管理員只希望 mod+ 的人能夠添加報價以避免垃圾郵件.我去了文檔并做了所有事情,但我無法讓它工作.這是我所擁有的:
I made a simple quote bot for a server, but the admin only wants mod+ people to be able to add quotes to avoid spam. I went to the documentation and did everything, but I can't get this to work. Here's what I have:
//other code
else if (command === "addquote" && arg) {
let adminRole = message.guild.roles.find("name", "Admin");
let modRole = message.guild.roles.find("name", "Mod");
if(message.member.roles.has(adminRole) || message.member.roles.has(modRole)){
const hasArr = arr.some((el) => {
return el.toLowerCase().replace(/s/g, '') === arg.toLowerCase().replace(/s/g, '');
});
if(hasArr){
message.channel.send(arg.replace(/s+/g,' ').trim() + " is already a Quote");
} else {
fs.appendFileSync('./Quotes.txt', '
' + arg);
message.channel.send("Quote added: " + arg);
arr.push(arg);
}
}
}
非常挑剔.有時,如果用戶具有 mod 角色,它會起作用,但大多數時候它不會.如果我這樣做了
It's very finicky. Sometimes it will work if the user has the mod role, most of the times it wont. If I do
console.log(message.memeber.roles.has(adminRole));
console.log(message.memeber.roles.has(modRole));
兩者都將輸出為 false,但會起作用嗎?老實說,我現在不知道.
both will output to false, but will work? Honestly, I have no idea at this point.
推薦答案
discord.js api 已更新,由于 .exists()
已被棄用,因此有更好的方法.
The discord.js api has been updated and there is a better way since .exists()
has been deprecated.
if (message.member.roles.cache.some(role => role.name === 'Whatever')) {}
這比 .find()
好,因為 .find()
返回角色對象(或未定義),然后將其轉換為布爾值..some()
方法默認返回一個布爾值.
This is better than .find()
because .find()
returns the role object (or undefined) which is then converted into a boolean. The .some()
method returns a boolean by default.
這篇關于查明某人是否有角色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!