Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!