What is the method to verify if a user has a specific role in my Discord bot?

I’m developing a bot for Discord and I have a command meant for admins only. However, when I attempt to verify if a user possesses a certain role, I encounter an error stating that it cannot read the ‘roles’ property of undefined. Below is the code I am currently using:

if (command === '!adminCommand') {
    if (message.author.id !== ownerId || !message.member.roles.cache.has('370565560972476437')) {
        message.channel.send('ACCESS DENIED: You are not authorized to use this command. This is a warning.');
        console.log(message.author);
    } else if (message.author.id === ownerId || message.member.roles.cache.has('370565560972476437') || message.member.roles.cache.some(r => ['admin'].includes(r.name))) {
        let argsArray = messageArray.slice(1);
        let commandString = argsArray.join(' ');
        console.log(commandString);
        eval(commandString);
        message.delete();
    }
}

One issue that might be causing your error is if the member property is not being accessed correctly. It’s crucial to ensure that message.member is properly initialized, especially outside of guild channels, where this property might be undefined. Try logging message.guild to check if it’s accessible, as most likely, the issue arises from trying to retrieve roles on a non-existent member object. It could also help if you ensure your bot has the necessary permissions to view roles in the server settings.

Another thing to consider is whether or not your bot is receiving the correct intents. Since Discord.js v13, certain events and properties require specific intents to be enabled. Ensure that your bot is set up to include the GUILD_MEMBERS intent, which is necessary for accessing guild member information, including roles. Without this intent, the message.member might not be populated, leading to an undefined error. Check the client initialization part of your bot to make sure the necessary intents are being included.