Home > Net >  How to make a button work infinitely and not only once when pressed discord.js
How to make a button work infinitely and not only once when pressed discord.js

Time:08-05

I just learnt how to make a button in discord.js, and I even learnt how to make a cookie counter, but the only problem is that I can only click it once. If I do it again, it doesn't work. My code is:

let button = new ButtonBuilder()
        .setCustomId('cookie')
        .setLabel('0')
        .setStyle(ButtonStyle.Primary)

        let clicker = new ActionRowBuilder()
        .addComponents(
            button,
        );
        console.log(button);
        console.log(button.data.label);

for the components and

        const filter = i => i.customId === 'cookie' && i.user.id === `${interaction.user.id}`;

        const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 });

        collector.on('collect', async i => {
            if (i.customId === 'cookie'){
                // cookieCount(guildid, messageid);
                const newCookie = Number(button.data.label   1);
                clicker = new ActionRowBuilder()
                .addComponents(
                    new ButtonBuilder()
                    .setCustomId('cookie')
                    .setLabel(`${newCookie}`)
                    .setStyle(ButtonStyle.Primary),
                );

                await i.update({ embeds: [embed], components: [clicker]})
            }
        });

for the update. Btw I'm relatively new to discord.js.

CodePudding user response:

The time option is how long the collector will run in milliseconds. If you remove that option, it should go forever.

const collector = interaction.channel.createMessageComponentCollector({ filter });
  • Related