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

491
Views
Discord.js v13 check if button already has been pressed

is there a way using collectors/interactionCreate to detect if a user already pressed a specific button on a specific message? One way I tried doing it was to save it in an array like this

const alreadyPressed = []
const filter = m => m.customId === "No" && m.messageId === interaction.messageId && m.user.id === interaction.user.id;
collector = embedMsg.createMessageComponentCollector({filter: filter, time: 10000});//86400000

collector.on('collect', async i => {
    client.logger("collector1 collected")
    client.logger(alreadyPressed.indexOf(i.user.id, i.messageId))
    if (alreadyPressed.indexOf(i.user.id, i.messageId)) {
        return i.reply({ content: `You've already voted ${i.user.username}!`, ephemeral: true })
    } else {
        i.reply({ content: `Thank you for your vote ${i.user.username}!`, ephemeral: true });
        alreadyPressed.push(i.user.id, i.messageId)
    }
});   
collector.on('end', collected => {
    client.logger(collected.size)
    // if (collected.size < 10) {
        
    // }
    delete alreadyPressed[0]
    delete alreadyPressed[1]
});

but the bot always ends up responding with "You've already voted" so what would be a viable way of checking this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are using Array#indexOf() which returns -1 if it doesn't find it (JavaScript sees it as a truthy value). I would also recommend you putting the values into an object or its own array (when pushing)

const alreadyPressed = []
// ...
collector.on('collect', async i => {
    // logs
    if (alreadyPressed.includes({userID: i.user.id, messageID: i.messageId})) {
        return i.reply({ content: `You've already voted ${i.user.username}!`, ephemeral: true })
    } else {
        i.reply({ content: `Thank you for your vote ${i.user.username}!`, ephemeral: true });
        alreadyPressed.push({userID: i.user.id, messageID: i.messageId})
    }
})
about 4 years ago · Juan Pablo Isaza Report

0

as MrMythical said, Array#indexOf() will just return -1 since it can't find it, I tried this out a little bit and found out that using and storing the values in an array is probably what your looking for. I put them together too since putting them seperate is not needed then I use .find() to search the array if it matches the users id and the id of the message that the button is on. This would be a viable way of doing it

const alreadyPressed = []
const filter = m => m.customId === "No"
collector = embedMsg.createMessageComponentCollector({filter: filter, time: 10000});//86400000

collector.on('collect', async i => {
    if (!!alreadyPressed.find(id => {  
        return id.ID === i.user.id+i.message.id
      })) {
        i.reply({ content: `You've already voted ${i.user.username}!`, ephemeral: true })
      } else {
        i.reply({ content: `Thank you for your vote ${i.user.username}!`, ephemeral: true });
        alreadyPressed.push({ID: i.user.id+i.message.id})
    }
});   
collector.on('end', collected => {
    client.logger(collected.size)
    // if (collected.size < 10) {
        
    // }
    delete alreadyPressed
});
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!