TypeError: Cannot read property 'roles' of undefined

my code:

client.on('message', msg =>{

if(msg.content === 'test'){

    let guild = msg.guild

    

    let role = msg.guild.roles.cache.find(r => r.name === 'test');

    const member = msg.mentions.members.first();

    member.roles.add(role);

  }

});

and this is error:

member.roles.add(role);
^

TypeError: Cannot read property ‘roles’ of undefined

(“discord.js”: “^12.2.0”)