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

494
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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