'How do i disable the button after its been clicked a certain amount of times discord js
so basically, i would pass a number as an argument and i want to disable the button after it was clicked that many times. However, when i use this code, it returns how many times it has been clicked but it doesnt disable the button.
message.channel.send({
content: "Click on the button below to enroll for fate adventure!",
components: [row],
});
const filter = (m) => m.customId === "enrollButton";
const collector = message.channel.createMessageComponentCollector({
filter: filter,
max: parseInt(args[1]),
time: 15000,
});
collector.on("collect", (i) => {
i.reply(`Clicked`);
});
collector.on("end", (collected) => {
row.components[0].setDisabled(true);
message.channel.send(`Clicked ${collected.size} times`);
});
Solution 1:[1]
Simply EDIT the message with everything keeping it the same but with the button edited to be disabled. For example:
collector.on("end", collected => {
collected.first().message.edit({
components: collected.first().message.components
.map(c => c.map(c => c.setDisabled(true)))
})
});
Solution 2:[2]
One way to give your code context of how many times the button has been pressed is to add a property to your client.
client.buttonClicks = 0;
And increment it on the collect event. Then, check if the limit has been reached. If so, end the collector and disable the button.
collector.on("collect", (i) => {
i.reply(`Clicked`);
client.buttonClicks++;
if (client.buttonClicks == LIMIT) {
collector.end("limit");
}
});
collector.on("end", (collected) => {
// Disable here
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Elitezen |
