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

289
Views
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 answers
Answer question

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 Report

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