抱歉标题混乱,我会澄清的。我试图让机器人检查用户在他们的 quick.db 清单中是否有某个角色,如果有,它会装备这个角色。我遇到的问题是,即使该角色在清单中,它也会返回该角色未被拥有的错误。我感觉问题出在 if (db.has(message.author.id + '.hot rod red'))
行上,因为我不太确定如何使用 quick.db 格式化检查角色。抱歉,代码凌乱,如果有人知道如何解决这个问题,请告诉我,谢谢!
if (db.has(message.author.id + '.hot rod red')) {
if (message.member.roles.cache.some(role => role.name === 'hot rod red')) {
let embed = new Discord.MessageEmbed().setDescription('You already have this role equipped!');
return message.channel.send(embed);
} else {
await message.guild.members.cache.get(user.id).roles.add('733373020491481219');
let embed = new Discord.MessageEmbed().setDescription(`You now have the ${message.guild.roles.cache.get('733373020491481219')} role!`);
message.channel.send(embed);
user.roles.remove(user.roles.highest);
}
} else {
let embed = new Discord.MessageEmbed().setDescription('You do not own this role!'); // ERROR HERE; GIVES ROLE EVEN WITHOUT OWNING
return message.channel.send(embed);
}
答案 0 :(得分:1)
可能尝试类似
let check = db.get(message.author.id+'.hot rod red')
并检查它是否为真/假,我会说使用字符串而不是布尔值,因为您可以使用 if(check === 'false/true'){}
。你也可以这样做
if(!check || check === 'false'){ return
let embed = new Discord.MessageEmbed().setDescription('You do not own this role!');
return message.channel.send(embed);
}
所以最终的代码是:
let check = await db.get(message.author.id + '.hot rod red');
let rejectEmbed = new Discord.MessageEmbed()
.setDescription('You do not own this role!');
if(!check){
return message.channel.send(rejectEmbed)
}
if(check === 'true'){
if (message.member.roles.cache.some(role => role.name === 'hot rod red')) {
let embed = new Discord.MessageEmbed().setDescription('You already have this role!');
return message.channel.send(embed);
} else {
await message.guild.members.cache.get(user.id).roles.add('733373020491481219');
let embed = new Discord.MessageEmbed().setDescription(`You now have the ${message.guild.roles.cache.get('733373020491481219')} role!`);
message.channel.send(embed);
user.roles.remove(user.roles.highest); // I'm unsure why this is here, this could be causing a potential error
}
}
如果还有什么问题,欢迎留言!!我希望这有帮助。