const teamSelectionCollector = gameSettingsMsg.createMessageComponentCollector({ componentType: 'BUTTON' })
      teamSelectionCollector.on('collect', async (buttonInteraction) => {
        if (buttonInteraction.user.id !== turn.id) {
          return buttonInteraction.reply({ content: `You're not allowed to click button`, ephemeral: true })
        } if (turn.id === cap1.id) {
          const selectedPlayer = remainingPlayers.find((player) => player.id === buttonInteraction.customId)
          team1.push(selectedPlayer)
          turn = cap2
          await buttonInteraction.reply({ content: `You have selected ${selectedPlayer.mention}`, ephemeral: true })
        } else {
          const selectedPlayer = remainingPlayers.find((player) => player.id === buttonInteraction.customId)
          team2.push(selectedPlayer)
          turn = cap1
          await buttonInteraction.reply({ content: `You have selected ${selectedPlayer.mention}`, ephemeral: true })
        }
        remainingPlayers = remainingPlayers.filter((player) => player.id !== buttonInteraction.customId)
        const updatedGameSettingMsg = Components.genSelectionBoard(cap1, cap2, team1, team2, remainingPlayers, turn)
        if (!remainingPlayers.length) teamSelectionCollector.stop()
      
        return gameSettingsMsg.edit(updatedGameSettingMsg)
      })

      Javascript language logo
      teamSelectionCollector

      Components

      The code creates a new message component collector, teamSelectionCollector, and sets it up to collect button interactions. The teamSelectionCollector object has an on() function that will be called whenever a button interaction is detected.

      The first thing the on() function does is check the user's id against the turn id. If the user is not on the current turn, the on() function returns and the button interaction is ignored.

      Next, the on() function checks to see if the button interaction is for the first or second turn. If it is for the first turn, the function checks to see if the selectedPlayer mention from the button interaction matches the player id of the player on the current turn. If they do not match, the function updates the turn id and calculates the new game settings.

      If the selectedPlayer is on the current turn, the on() function checks to see if they have already selected a player. If they have not, the function updates the turn id and calculates the new game settings.

      Finally, the on() function returns the newly generated game settings to the calling function.

      Shortcut: teamSelection.Collector

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.