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
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.