如何知道用户是否已经在addrole命令中扮演角色

时间:2020-10-16 11:33:38

标签: discord discord.js

我想发送一条错误消息,说如果用户在addrole命令中已经具有该角色,我的当前命令是:

const { MessageEmbed } = require("discord.js");

module.exports = {
  name: "addrole",
  description: "adds a role to a member",
  execute: async (message , args) => {
    if (!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("Invalid Permissions")
    let member = message.mentions.members.first();
    let role = message.mentions.roles.first();
    if (!member)
      return message.reply("Please mention a valid member of this server");
    if (!role) return message.reply("please mention a valid role of this server");

    const highest = message.member.roles.highest;

    if(highest.comparePositionTo(role) <= 0)
    return message.channel.send(`You cannot add roles equal/higher to that member's highest role`)
    
    let addroleembed = new MessageEmbed()
    .setTitle(`:white_check_mark: ${role.name} has been added to ${member.user.tag}`)
    .setFooter(`Action performed by ${message.author.tag}`)
    .setColor('77B255')
   addroleembed.setTimestamp();

    member.roles.add(role);
  message.channel.send(addroleembed)

}}  

我尝试使用

 if(!message.member.roles.cache.some(r=>[`${role.name}`].includes(r.name)))

 if (member.roles.has(role.id) {

但是这些似乎都不起作用,有人知道其背后的原因吗?如果我不能使用这两种方法,您能建议我一个解决方案吗?

2 个答案:

答案 0 :(得分:1)

您需要结合两次尝试。

if(message.member.roles.cache.has("role ID") {
    //rest of your code
} 

答案 1 :(得分:1)

你是如此亲密!

使用cache时,您所需要的只是has()

member.roles.cache.has(<RoleID>);

了解更多信息Here