Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

246
Vistas
Discord JS: Extracting data from a collection

I have a command where I want to write the id of a user in a voice channel into an array. So far, I managed to write the whole collection of the user with joinedTimestamp etc. Now I want to extract the id of the user from the collection.

My command looks like this:

client.on('messageCreate', (message) => {
    let waitingroom = message.guild.channels.cache.get("952324088762867722")
    let player = [];
    let isadmin = message.member.roles.cache.has(adminRole);

    if (message.content.toLowerCase() === prefix + 'startgame' && isadmin) {
        player.push(waitingroom.members);
        console.log(player);
    }    
});

And this is what I get from the array when 2 users are in the waitingroom:

[
  Collection(2) [Map] {
    '392776445375545355' => GuildMember {
      guild: [Guild],
      joinedTimestamp: 1646940658665,
      premiumSinceTimestamp: null,
      nickname: null,
      pending: false,
      communicationDisabledUntilTimestamp: null,
      _roles: [Array],
      user: [User],
      avatar: null
    },
    '849388169614196737' => GuildMember {
      guild: [Guild],
      joinedTimestamp: 1647168003344,
      premiumSinceTimestamp: null,
      nickname: 'Test-Account [Nils]',
      pending: false,
      communicationDisabledUntilTimestamp: null,
      _roles: [],
      user: [User],
      avatar: null
    }
  }
]

But I only want the user ids from the collection. I appreciate any help.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Better solution, see accepted answer.

First you need to create a variable with an empty array.
Then you can use the .map function of the member Collection to push the user id's to the array.

let arr = [];
<Channel>.members.map(user => {
    arr.push(user.id);
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

The accepted answer is a bit verbose. Collection#map() returns an array and accepts a callback function, so all you have to do is to iterate over the members and return their ids:

let waitingRoom = message.guild.channels.cache.get('952324088762867722')
let memberIDs = waitingRoom.members.map(member => member.id)

Now, memberIDs is an array of the snowflakes/IDs of the members. No need to create a new array or use the push method. Actually, you should never really use map with push or other methods that mutate arrays.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda