I want to make a Discord.js bot that just checks if a member with the given ID has a role (by id). I know this is a duplicate, but .cache doesn't work for me, so I used the guild.members.fetch function:
const { Client, Intents, GuildMemberManager } = require('discord.js');
const { token, server_id } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
function validateUser(user_id) {
const guild = server_id;
guild.members.fetch(user_id)
.then(console.log)
.catch(console.error);
}
validateUser(491553591434412057)
// Login to Discord with your client's token
client.login(token);
However, I run into an error:
TypeError: Cannot read properties of undefined (reading ‘fetch‘).
Does anyone know a better way to get the roles without a written message etc. or how to fix this?