Discord.js Mention, User Undefined

Tags: , , , ,



EDIT: I have found the solution by myself, thank you everybody who sees this post! The solution is :

/// SIMPLY ADD || message.member
const taggedUser = message.mentions.members.first() || message.guild.members.cache.get(args[1]) || message.member

That is the solution to this problem, it’s not the same as message.author but at least it works now!

Original: I’m having a little problem with my script! Any help would be highly appreciated!

Here’s the problem: This is the first script, this script doesn’t have any problems in it, it all worked out smoothly, except that this script sends a message instead of changing the let, it would make me create multiple embeds. But in this example, it uses a simple message.send instead

    if(command === 'test'){
/// ALL OF THIS WORKS
        const taggedUser = message.mentions.members.first() || message.guild.members.cache.get(args[1])
        if(!taggedUser){
            if(message.member.roles.cache.has('745227998893965322')){
                message.channel.send('Yes, you have that role #1')
            }else{
                message.channel.send('No, you don't have that role #1')
            }
        }
        if(taggedUser.user.id === message.author.id){
            if(message.member.roles.cache.has('745227998893965322')){
                message.channel.send('Yes, you have that role #2')
            }else{
                message.channel.send('No, you don't have that role #2')
            }
        }
        if(taggedUser.user.id != message.author.id){
            if(taggedUser.roles.cache.has('745227998893965322')){
                message.channel.send('Yes, that user have that role')
            }else{
                message.channel.send('No, that user doesn't have that role')
            }
        }
    }

While in this script in the other hand, it works when mentioning a user, and it works when we mention ourselves. If we didn’t add any mention, the log would say that the User is undefined. The reason why i use a let name so i can easily change the text there instead of creating multiple embeds

    if(command === 'test2'){
        const taggedUser = message.mentions.members.first() || message.guild.members.cache.get(args[1])

        let name
        if(!taggedUser){ /// THE PROBLEM IS RIGHT HERE, WHEN NO MENTION, USER IS UNDEFINED
            if(message.member.roles.cache.has('745227998893965322')){
                name = 'Yes, you have that role #1'
            }else{
                name = 'No, you don't have that role #1'
            }
        }
/// THE REST OF THIS WORKS FOR SOME REASON
        if(taggedUser.user.id === message.author.id){
            if(message.member.roles.cache.has('745227998893965322')){
                name = 'Yes, you have that role #2'
            }else{
                name = 'No, you don't have that role #2'
            }
        }
        if(taggedUser.user.id != message.author.id){
            if(taggedUser.roles.cache.has('745227998893965322')){
                name = 'Yes, that user have that role'
            }else{
                name = 'No, that user doesn't have that role'
            }
        }

        message.channel.send(`${name}`)
    }

Is there any solution to this? I have tried a different method but it just gives me multiple new errors. Thank you for reading this

Answer

I’m sorry that this is a late reply, but in case somebody who has the same problem as me, this is the solution:

/// SIMPLY ADD || message.member const taggedUser = message.mentions.members.first() || message.guild.members.cache.get(args[1]) || message.member

You just simply add the message.member, it doesn’t act the same as message.author but it works fine if you may edit it correctly!

You can use this script to find members’ usernames that has been set on your server, and check if the member that you mentioned has the role or not!

Special thank you for those who sees this post, i hope it helps you! Thanks Jytesh for suggesting me to make it as the answer Thank you a2br for suggesting me to make it as the answer as well

I’m so sorry, i’m really new at Stackoverflow, but i’ll try my best to help you!



Source: stackoverflow