Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

294
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda