COdeingNinja
COdeingNinja

Reputation: 387

Discord vs bot 13 not able to assign roles giving error Cannot read properties of undefined (reading 'guild')

I am using discord js v13.6. I have written in my code where after using a slash command user is assigned many roles which are stored in a array named rolenumber. The problem is I am getting this error every time

 await client.users.fetch(memberId).then((user,interaction) => {
            user.send("Give me a few moments while I verify your details");
            (async function(user){

            
            let RoleArray = rolenumber.split(","); 
            //let guild = client.guilds.cache.get(guildNumber);
            //let member = await client.users.fetch(msg);   
            
            for(let num of RoleArray) {
                //console.log("Roles Inside a for loop",num);
                let role = interaction.guild.roles.cache.get(num);
                let data = await user.roles.add(role).catch(console.error);
                //user.roles.add(num).catch(console.error);            
            }   
            
           
            let welcomeMessage = 'Welcome <@'+ user.id +'>, you are now officially part of channel.';
            const channelMessage = await client.channels.cache.get(channelNumber);
            channelMessage.send(welcomeMessage);
        })();

here the guildNumber is a guild Number which is hidden for security reason from the question. I am getting this as error message:

let role = interaction.guild.roles.cache.get(num);
                                       ^

TypeError: Cannot read properties of undefined (reading 'guild')

at E:\OutscalGit\DiscordBot\index.js:200:44
at processTicksAndRejections (node:internal/process/task_queues:96:5)

Upvotes: 0

Views: 69

Answers (2)

MrMythical
MrMythical

Reputation: 9041

Promise.prototype.then's fulfillment callback only takes 1 argument.

await client.users.fetch(memberId).then((user) => {
            user.send("Give me a few moments while I verify your details")
            // ...
})

Assuming interaction is already defined above, it should work

Upvotes: 1

mustafayelmer
mustafayelmer

Reputation: 159

interaction is undefined Are you sure, interaction is passed to here?

you can use it but it not perfect

let role = interaction?.guild?.roles?.cache?.get(num);

Upvotes: 1

Related Questions