Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda