Quiero hacer un bot Discord.js que solo verifique si un miembro con la identificación dada tiene un rol (por identificación). Sé que esto es un duplicado, pero .cache no funciona para mí, así que usé la función guild.members.fetch :
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);Sin embargo, me encuentro con un error:
TypeError: Cannot read properties of undefined (reading 'fetch').¿Alguien sabe una mejor manera de obtener los roles sin un mensaje escrito, etc. o cómo solucionar esto?
El problema es que tu guild no es un gremio sino una cadena ( server_id ). Necesitarás obtener el gremio con esta identificación:
const guild = client.guilds.cache.get(server_id); if (!guild) return console.log(`Can't find the guild with ID ${server_id}`); guild.members.fetch(user_id) .then(member => { // member.roles.cache is a collection of roles the member has console.log(member.roles.cache) if (member.roles.cache.has('ROLE ID')) console.log('member has the role') }) .catch(console.error);