Estoy tratando de enviar mensajes directos a todos los miembros del servidor mediante un bot a través de un comando y solo envía mensajes directos a 4 personas que son administradores
message.guild.members.cache.forEach(member => { // Looping through each member of the guild. // Trying to send a message to the member. // This method might fail because of the member's privacy settings, so we're using .catch member.send(`hi`) .catch(() => (`Couldn't DM member ${member.user.tag}`)); message.channel.send(`Success`) .catch(console.error); });
Esta operación puede llevar mucho tiempo. La respuesta de @SuleymanCelik fue parcialmente correcta porque no todos los usuarios se almacenan en el caché de miembros del bot. Para obtener a todos los usuarios del servidor, debe realizar una llamada fetch() para todos los usuarios como este.
guild.members .fetch() .then(members => members.forEach(member => { member .send("Hello!") .catch(() => { console.error(`Failed to send ${member.user.tag} a message`) }) }))
puede enviar un mensaje a todos los usuarios cuya configuración de privacidad no esté activada de esta manera
message.guild.members.cache.forEach(member => { // Looping through each member of the guild. // Trying to send a message to the member. // This method might fail because of the member's privacy settings, so we're using .catch member.send("Hello, this is my message!").catch(e => console.error(`Couldn't DM member ${member.user.tag}`)); });