Stephan Yazvinski
Stephan Yazvinski

Reputation: 578

Cannot read property 'roles' of undefines Discord

    const role = message.guild.roles.cache.find(role => role.name === 'MUTED');
    const member = message.mentions.members.first();


if (message.content.startsWith("https://discord.gg/")){
    message.delete (); 
    ruleBreaker = ruleBreaker + 1;

    if (ruleBreaker < 2){
    message.reply ('Dont even try me. I WILL mute you if you send it again.');
    }
    if (ruleBreaker == 2){
        message.reply("you have been muted for 15 munites.")
        member.roles.add(role);
    }

}

I keep getting the error, "Cannot read property 'roles' of undefines Discord" and this code is directly copied from https://discordjs.guide/popular-topics/common-questions.html#legend. Been staring at this for days. I don't know what's wrong.

Upvotes: 0

Views: 50

Answers (1)

duckboycool
duckboycool

Reputation: 2455

It looks like you took this code from the "How do I add a role to a guild member?", which isn't doing quite what I think you want. Here, the const member is being sent to the first person mentioned in the message, and undefined if there are no mentions. If you want it to be the person who sent the message, just use message.author.

Upvotes: 1

Related Questions