Search code examples
node.jsdiscorddiscord.jscollectdiscord-buttons

Discord.js v13 trying to collect button and check the button id


I am trying to collect a button and then check if button.id is cash , here is my code

      const msg = message.reply({embeds: [dembed], components: [row1, row2]}).then(function (msg) {

        const filter = i => {
          i.deferUpdate();
          return i.user.id === message.author.id;
        };
        
        const collector = message.createMessageComponentCollector({ componentType: 'BUTTON', filter ,time: 15000 });

        collector.on('collect', i => {

          if(i.customId === 'cash') {
            msg.channel.send("test")
          }
        })

        })

Solution

  • When creating the messageComponentCollector you are telling it to monitor the first message which the user posted for any button clicks. Since the buttons are in the msg variable instead of the message variable, you will just need to change that one to msg. Your fixed code should look like this:

    const msg = message.reply({ embeds: [dembed], components: [row1, row2] }).then(function (msg) {
        const filter = i => {
            i.deferUpdate();
            return i.user.id === message.author.id;
        };
        const collector = msg.createMessageComponentCollector({ componentType: 'BUTTON', filter, time: 15000 });
        collector.on('collect', i => {
            if (i.customId === 'cash') {
                msg.channel.send("test")
            }
        })
    
    })