Home > Software engineering >  How to keep InteractionCollector alive after discord js bot restart?
How to keep InteractionCollector alive after discord js bot restart?

Time:05-10

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?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related