Search code examples
javascriptdiscorddiscord.jsdiscord-buttons

How to disable button after clicked on discord.js v12


Discord.js v12 Disable Buttons Function


how do I disable a button on discord.js after it is clicked? I tried using the collector.on function, since that seems to do it but I can't make it work with my code and I know, I didn't copy-paste but I can't seem to work it out and its stressing me a lot, i looked through all the web to get answers but nothing anywhere. the discord-buttons js guide just disappeared. Well you've come to the right place you can simply do the following below its explained and can really help you 😃.


So because the last answer was not right but had some correct formats this is what you wanna do. The code is explained the best i can below


The Code:

// After you have made your buttons and the embed you then need to do
// You can do this method a lot of times just make sure to add a cooldown.
// You can also do the same with the MessageActionRow() constructor
const embed = new Discord.MessageEmbed().setColor('red').setDescription('click the below buttons').setTimestamp()
const dbutton = new MessageButton().setStyle('blurple').setID('lol').setLabel('disabled button').setDisabled(true).setEmoji('😃')
const button = new MessageButton().setStyle('blurple').setID('yes').setLabel('enabled button').setEmoji('😃')
const after = await message.channel.send({ embed: embed, components: [button] })

client.on("clickButton", async (button) => {
if (button.id === "yes") {
message.channel.send({ content: "you clicked the first button" })
after.edit({ embed: embed, components: [dbutton] })
} 
button.reply.defer();
});

The above embed: and components: are supported in v12. I hope this helped you with further questions


Solution

  • MessageButton class has a "setDisabled" method that you can set to true.

    When done, it won't automatically disable it. You need to edit the embed, passing the components again.

         const button11 = 
    
        const row = new MessageActionRow()
            .addComponents(
                 new MessageButton()
                     .setStyle('green')
                     .setID('yes')
                     .setLabel('Lock')
                     .setEmoji('🔒')
             )
    
        const supportembedy = new Discord.MessageEmbed()
                                  .set(....)
    
        let botMsg = await channel.send(supportembedy, row);
    
        const filter = i => ((i.customId === "yes") || (i.customId === "no")) && i.user.id === message.author.id 
        const collector = interaction.channel.createMessageComponentCollector({filter, time: 15000})
    
        collector.on("collect", async (i) => {
            if (i.customId === 'yes') {
                row[0].setDisabled(true)
                // first way
                botMsg.edit({ embeds: [supportembedy], components: [row] })
                //second way
                i.update({ embeds: [supportembedy], components: [row] })
            }
        }