How to know if a user already has a role in an addrole command

peter 23

I want to send an error message saying that the user already has the role if they do in an addrole command.My current command is:

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)

}}  

I have tried using

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

and

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

But neither of these seem to work, Anyone know the reason behind this? If I can't use either of these methods, Can you suggest me a solution?

Elitezen

You are so close!

All you need is cache when using has()

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

Learn More Here

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to know if a user already has a role in an addrole command

分類Dev

How to know if a flutter image has been seen by the user

分類Dev

How to give role to user for reaction

分類Dev

How can a team decide to pick a user story for a sprint, when the Product Manager has already decided on PBI ordering?

分類Dev

Jenkins: How can I know if an automatic process or a user has triggered a build?

分類Dev

How to know if the user has recently updated their Chrome Extension so I can alert them

分類Dev

How can I know if a given user has an admin account in a window server 2012 machine?

分類Dev

How to know the command run by a process?

分類Dev

how to create "role" with "Another AWS account" role type by cli command?

分類Dev

In ASP.NET Core 2.1 how do I add menu items after the user has logged in based on Role?

分類Dev

How to determine if user doesn't contain a role

分類Dev

How to add a role to a user in a specific guild

分類Dev

How to assign default user role in entrust?

分類Dev

How to know if a set of threads has finished

分類Dev

How to know when a block has been completed

分類Dev

How to know if the winform has stopped functioning/responding

分類Dev

How to know if users in my application already sent payment in Paypal?

分類Dev

MQTT:How to know when user disconnect

分類Dev

How to get "error" message from @commands.has_role()

分類Dev

How to display all permissions a role has in laravel 5 entrust?

分類Dev

Can I get a list of all resources for which a user has been added to a role?

分類Dev

How to change the texture of a content that has been loaded already?

分類Dev

How to create keycloak client role programmatically and assign to user

分類Dev

React how to know how many child components a parent has?

分類Dev

How can I add to a already running process an additional 'shutdown' command?

分類Dev

SFINAE : Know if a function already exist or no

分類Dev

"You cannot install this app because another user has already installed an incompatible version on this device"

分類Dev

checking user role and restriction for user

分類Dev

How to forbid user to insert 0 on TextBox if it's empty or already 0?

Related 関連記事

  1. 1

    How to know if a user already has a role in an addrole command

  2. 2

    How to know if a flutter image has been seen by the user

  3. 3

    How to give role to user for reaction

  4. 4

    How can a team decide to pick a user story for a sprint, when the Product Manager has already decided on PBI ordering?

  5. 5

    Jenkins: How can I know if an automatic process or a user has triggered a build?

  6. 6

    How to know if the user has recently updated their Chrome Extension so I can alert them

  7. 7

    How can I know if a given user has an admin account in a window server 2012 machine?

  8. 8

    How to know the command run by a process?

  9. 9

    how to create "role" with "Another AWS account" role type by cli command?

  10. 10

    In ASP.NET Core 2.1 how do I add menu items after the user has logged in based on Role?

  11. 11

    How to determine if user doesn't contain a role

  12. 12

    How to add a role to a user in a specific guild

  13. 13

    How to assign default user role in entrust?

  14. 14

    How to know if a set of threads has finished

  15. 15

    How to know when a block has been completed

  16. 16

    How to know if the winform has stopped functioning/responding

  17. 17

    How to know if users in my application already sent payment in Paypal?

  18. 18

    MQTT:How to know when user disconnect

  19. 19

    How to get "error" message from @commands.has_role()

  20. 20

    How to display all permissions a role has in laravel 5 entrust?

  21. 21

    Can I get a list of all resources for which a user has been added to a role?

  22. 22

    How to change the texture of a content that has been loaded already?

  23. 23

    How to create keycloak client role programmatically and assign to user

  24. 24

    React how to know how many child components a parent has?

  25. 25

    How can I add to a already running process an additional 'shutdown' command?

  26. 26

    SFINAE : Know if a function already exist or no

  27. 27

    "You cannot install this app because another user has already installed an incompatible version on this device"

  28. 28

    checking user role and restriction for user

  29. 29

    How to forbid user to insert 0 on TextBox if it's empty or already 0?

ホットタグ

アーカイブ