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

120
Vistas
creating and listening to events in a while loop messes up my event
    let sentMessage = await message.channel.send({ content: "press a button" , components: [
        new MessageActionRow()
        .addComponents(
            new MessageButton().setCustomId("yamete").setLabel("yamete").setStyle("PRIMARY")
        )
    ] }) // this sends a message and attaches buttons onto it

    let isStillAcceptResponse = true
    let i = 0

    while (isStillAcceptResponse){
        i++
        if(i > 5){
            sentMessage.edit("dont press the button")
            break
        } // max loop of 5

        let inputEvent = sentMessage.createMessageComponentCollector({
                componentType: "BUTTON", time: ms("10 seconds")
        }) // this just creates a event listener for button press on the message

        // when the button is pressed, it emits a "collect" event
        inputEvent.on("collect", async input => {
            console.log("pong")

            if (Math.floor(Math.random() * 1000) % 2 !== 0){ // generate a random true/false
                sentMessage.edit({ content: "press the button again!", components: [
                    new MessageActionRow()
                    .addComponents(
                        new MessageButton().setCustomId("yamete").setLabel("yamete").setStyle("PRIMARY")
                    )
                ] }) // edits the sent message with a new message content but with the same buttons

            } else { isStillAcceptResponse = false ; sentMessage.edit("dont press the button") }
        })
    }

It sends the message and I pressed the button 1 time, it logs pong but for some reason it logs pong 5 times instead of 1. I have also tried instead to wrap the loop inside of the event but that just confused me even more so now I am stuck

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

0

I think, that you misunderstood the concept of events. Probably you are trying to stop the event from activating more than 5 times. But event#on just binds the function to the event, it doesn't pause the thread, so you just create 5 listeners, that activate all at the same time logging pong five times to the console. Best solution would be just to specify max: 5 in the collector options.

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