I have a problem when I define "member" like that:
const guild = client.guilds.cache.get('my guild id')
const member = guild.members.cache.get('the member id that I need')
When I use it, I can't get its properties because the console tells me that it is undefined. So what's the problem?
First of all you need to make sure that you've enabled the GUILD_MEMBERS intent on the Discord developer portal in your application. After you've done that you need to include the intent in the Client.
const { Client, Intents } = require('discord.js');
const client = new Client({intents: [Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]});
After you've done this, it should work. If it doesn't work, the member isn't fetched or the user is not in the specified guild. If the member is in your server you need to fetch the user by using the fetch function. The fetch function will return a Promise which will return the GuildMember if the Promise is fullfilled. If the Promise is rejected, the member is not in your server or there was an error while trying to access the member. You can fetch the user in this way:
const { Client, Intents } = require('discord.js');
const client = new Client({intents: [Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]});
client.once('ready', async () => {
var guild = client.guilds.cache.get('The Guild ID');
if(!guild){
try {
guild = await client.guilds.fetch('The Guild ID');
} catch (error) {
return console.log(`Error while fetching the guild: `, error);
}
}
var member = guild.members.cache.get('The User ID');
if(!member){
try {
member = await guild.members.fetch(`The User ID`);
} catch (error) {
return console.log(`Error while fetching the member: `, error);
}
}
});
client.login('Your token');