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

290
Vistas
How to keep InteractionCollector alive after discord js bot restart?

My bot reads proposals from Google Forms and puts them into a channel where people can vote Yes or No by clicking the corresponding MessageButton.

After the message is sent, the InteractionCollector is set up as follows:

function setupPollCollector(proposal, message) {
  const pollFilter = async (i) => {
    if (i.customId.startsWith(proposal.id + '_vote')) {
      await i.deferReply({ ephemeral: true })
      return true
    } else {
      return false
    }
  }
  const pollCollector = message.createMessageComponentCollector({ filter: pollFilter, time: dCfig.pollTime })

  pollCollector.on('collect', i => {
    console.log(i.user.tag, 'voted', i.customId.substring(i.customId.indexOf('_vote') + 5))

    const getVote = (i) => i.customId.endsWith('Yes') ? dCfig.emojis['vote_yes'] : dCfig.emojis['vote_no']
    let vote = getVote(i)
    const prevVoteKVPair = mapToArray(pollCollector.collected).find(([id, int]) => int.user.tag === i.user.tag && int.id !== i.id)
    const prevVote = prevVoteKVPair ? getVote(prevVoteKVPair[1]) : 'Nothing'
    vote = (prevVote === vote) ? 'Nothing' : vote

    // Remove previous votes of the same user
    if (prevVoteKVPair) {
      pollCollector.collected.delete(prevVoteKVPair[0])
      vote === 'Nothing' ? pollCollector.collected.delete(i.id) : null
    }

    i.editReply({
      content: bold(underscore(`Your vote for ${proposal.projectName} has been registered!`))
        + '\n\nPrevious vote: ' + prevVote
        + '\nCurrent vote: ' + vote
        + (vote !== 'Nothing' ? '\n\nTo remove your vote, vote ' + vote + ' again.' : ''),
      ephemeral: true
    })
  })

The problem comes whenever I have to restart the bot to make changes to the code. All buttons stop working and show "This interaction failed"

Is there any way to keep alive all active collectors after the bot restarts?

The only solution I can think of is to:

  1. Read clicks through Client.on('interactionCreate', ...
  2. Persist every vote onto the database whenever a user clicks a button
  3. Store on database the time when the poll should end so I can periodically check what buttons I have to manually disable.

Aka. not use createMessageComponentCollector and, instead, code all its logic myself. This feels like a terribly dirty solution, is there any better way of resuming button interactions after bot reboot?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Send the buttons with a unique customId. Then you can listen to client#interactionCreate and check if the ButtonInteraction#customId matches then update the message.

about 4 years ago · Juan Pablo Isaza Denunciar

0

One solution could be to get the MessageComponentCollector instance out of the function so it is always active even if the function isn't called. That way you create the message on function, but the collector is always active. You can then pass some variables for IDs of the messages of things like that.

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