0

So previously I have been using this following code to check if a member had a certain role.

if (message.member.roles.some(role => role.name === 'role'))

It always used to work for me. However, now it seems to return an error. I have narrowed it down, to message.member returning Null. I have tried changing it guildMember and the same issue.

the error in question is:

TypeError: Cannot read property 'roles' of undefined

Has there been any changes to this? Can someone help me try to find a new way to check if they have a role?

Edit

This is where the message comes from

client.on('message', message => {
    
    console.log("Member: " + message.member);

This returns null favlues for message.member.

Rhys M
  • 37
  • 1
  • 6

2 Answers2

1

Edit: I've seen the console log for the 'message' object that you have shared in the comment and below is my update from that:

  1. As we can see from the below image, message.member is 'null' due to which you were getting the error that you cannot find the property 'roles' of undefined (member which is null here).

enter image description here

  1. I believe what you are looking for here is message.guild.roles here which is a collection (Map). Please see the screenshot below.

enter image description here

You should try to retrieve this roles Map and iterate over it for the in the following way:

const roles = message.guild.roles;

You can then use roles.entries() in order to get the key-value pairs from the roles MAP and iterate over them and use the if condition that you were initially using.



Like the error message states, this is happening because 'member' is undefined. You can either have a guard clause like mentioned by user Paulquappe in his answer, but the guard clause would be for member and not roles, so something like this:

if (message.member && message.member.roles.some(role => role.name === 'role'))

or, if you are going to transpile your code, you can use optional chaining from ES2020 (this is a very new feature in JS, therefore I would not recommend it unless you are going to transpile your code):

The optional chaining operator acts like a guard clause, but the syntax only requires that you use a '?' before chaining using the dot syntax.

if (message.member?.roles.some(role => role.name === 'role'))
Link
  • 1,198
  • 4
  • 14
  • Thanks for the help. However it will always return null and I dont know why it would do that. This just stops the error from coming up, but im trying to find a way where I can fix it, and find another way to check a members role. – Rhys M Nov 13 '20 at 12:24
  • 1
    It is returning null from whereever he data is coming from. You will have to share more of your code in order for us to identify that. – Link Nov 13 '20 at 12:25
  • Yes, I have just edited the post to show where the message is coming from. – Rhys M Nov 13 '20 at 16:59
  • Can you console.log(message) object and share the result? I would like to see if member prop is available on message. And if the property name is something else. A screenshot would be good. – Link Nov 13 '20 at 17:09
  • https://pastebin.com/vrdg9Wvu, here is the message log – Rhys M Nov 13 '20 at 17:32
  • Thanks. However im trying to check if a member who used a command has a certain role. How am I supposed to go about this if message.member is null? – Rhys M Nov 13 '20 at 18:16
  • @RhysM right above the roles Map there’s a members map within the guild object. If you know the member username or something, you might be able to retrieve the role of that member too. However , you will need to see what is inside the members Map using ***message.guild.members.entries()*** – Link Nov 13 '20 at 18:50
0

Most likely there is no 'roles' prop on member. I suggest you create a guard clause and ensure the props is there before accesing it.

You can read about guard clause here:

How do you implement a guard clause in JavaScript?

Paulquappe
  • 144
  • 2
  • 6
  • 15
  • 1
    There is no 'member', that is the reason it says: Cannot read property 'roles' of 'undefined'. Therefore, member is undefined. – Link Nov 13 '20 at 10:31
  • just apply guard clause to member? Same principle. But someone already wrote the code for you. – Paulquappe Nov 13 '20 at 16:14
  • It will still give an error because member is undefined. Guard clause should be applied to member. – Link Nov 13 '20 at 16:14