最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable button once it is clicked in Discord.js 13 - Stack Overflow

programmeradmin0浏览0评论

How can I make it so that once a user clicks a button in the message, that button gets disabled whereas the other buttons are still clickable? I'm using Discord.js v13.1.0 and this is what I've been able to write so far:

const { MessageActionRow, MessageButton } = require('discord.js');

let row = new MessageActionRow()
    .addComponents(
        but_1 = new MessageButton()
            .setCustomId('id_1')
            .setLabel('Button 1')
            .setStyle('SECONDARY'),
        but_2 = new MessageButton()
            .setCustomId('id_2')
            .setLabel('Button 2')
            .setStyle('SECONDARY')
    );

interaction.reply({ content: "Click a button", ponents: [row] })

I tried creating message ponent collector to check the id of the button clicked and edit the button but turns out .addComponents() just adds another button instead of editing it. This is what I did:

const filter = i => i.customId === 'id_1' && i.user.id === interaction.user.id;

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

collector.on('collect', async i => {
    if (i.customId === 'id_1') {
        row.addComponents(
            but_1.setLabel('Click Registered!')
                .setCustomId('id_1_clicked')
                .setDisabled(true)
       )
       interaction.editReply({ content: "Click a button", ponents: [row] });
    }
});

How can I make it so that once a user clicks a button in the message, that button gets disabled whereas the other buttons are still clickable? I'm using Discord.js v13.1.0 and this is what I've been able to write so far:

const { MessageActionRow, MessageButton } = require('discord.js');

let row = new MessageActionRow()
    .addComponents(
        but_1 = new MessageButton()
            .setCustomId('id_1')
            .setLabel('Button 1')
            .setStyle('SECONDARY'),
        but_2 = new MessageButton()
            .setCustomId('id_2')
            .setLabel('Button 2')
            .setStyle('SECONDARY')
    );

interaction.reply({ content: "Click a button", ponents: [row] })

I tried creating message ponent collector to check the id of the button clicked and edit the button but turns out .addComponents() just adds another button instead of editing it. This is what I did:

const filter = i => i.customId === 'id_1' && i.user.id === interaction.user.id;

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

collector.on('collect', async i => {
    if (i.customId === 'id_1') {
        row.addComponents(
            but_1.setLabel('Click Registered!')
                .setCustomId('id_1_clicked')
                .setDisabled(true)
       )
       interaction.editReply({ content: "Click a button", ponents: [row] });
    }
});
Share Improve this question asked Aug 19, 2021 at 6:02 Someone Like MeSomeone Like Me 511 gold badge2 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

.addComponents() will just add to the existing ponents. Since the ponents is an array, you need to change row.ponents[0].setDisabled(true) (for but_1) and row.ponents[1].setDisabled(true) (for but_2) and edit the reply with the changed row MessageActionRow.

collector.on('collect', async i => {
    if (i.customId === 'id_1') {
        row.ponents[0].setDisabled(true) //disables but_1
    }
    if (i.customId === 'id_2') {
        row.ponents[1].setDisabled(true) //disables but_2
    }
    interaction.editReply({ content: "Click a button", ponents: [row] });
})
发布评论

评论列表(0)

  1. 暂无评论