Disable buttons if none were clicked when timer runs out

This is a follow up question to one of my previous posts where I wanted to dynamically disable buttons based on their customId once they were clicked. While I was able to get that to work thanks to the input of the kind souls from this server, I ran into an issue while testing my functions further. If no buttons are clicked and the timer you set on the collector runs out, then the Collection that is emitted is empty: Collection(0) [Map] {} (If you click at least one, or more buttons, then you will get back: Collection(i) [Map] {Button Interaction info...}, with i being how many buttons you clicked). I was using the Collection Map to get all the buttons back, so that I could loop over them and disable them individually (I can't just disable the buttons by their name, as I am dynamically creating buttons without a name). Code for my collector (I am "filtering" on 'collect'):
const collector = response.createMessageComponentCollector({
componentType: ComponentType.Button,
time: 8_000, // return it to 30s
max: maxButtonsToClick
});
const collector = response.createMessageComponentCollector({
componentType: ComponentType.Button,
time: 8_000, // return it to 30s
max: maxButtonsToClick
});
Code for my collector on 'end' (which works fine as long as at least one button was clicked):
collector.on('end', (interaction) => {
const arrayOfActionRows = interaction.first().message.components;
const buttons = [];
for (const actionRow of arrayOfActionRows){
const componentsRow = actionRow.components;
for (let a = 0; a < componentsRow.length; a++){
const current = componentsRow[a];
buttons.push(
new ButtonBuilder()
.setCustomId(`${current.data.custom_id}`)
.setLabel(`${current.data.label}`)
.setStyle(ButtonStyle.Secondary)
.setDisabled(true)
);
}
const buttonsArray = buttonWrapper(buttons);

response.edit({
components: buttonsArray
})
}
})
collector.on('end', (interaction) => {
const arrayOfActionRows = interaction.first().message.components;
const buttons = [];
for (const actionRow of arrayOfActionRows){
const componentsRow = actionRow.components;
for (let a = 0; a < componentsRow.length; a++){
const current = componentsRow[a];
buttons.push(
new ButtonBuilder()
.setCustomId(`${current.data.custom_id}`)
.setLabel(`${current.data.label}`)
.setStyle(ButtonStyle.Secondary)
.setDisabled(true)
);
}
const buttonsArray = buttonWrapper(buttons);

response.edit({
components: buttonsArray
})
}
})
Thanks for the help in advance! Discord.js: v14.14.1 Node.js: v20.10.0
2 Replies
d.js toolkit
d.js toolkit5mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Toldi
Toldi5mo ago
It's the original embed I send along with the buttons
const response = await message.channel.send({ embeds: [unitEmbed], components: buttonsArray });
const response = await message.channel.send({ embeds: [unitEmbed], components: buttonsArray });
Oh? Didn't think of that... Lemme go test that. Thanks for the idea 👍 That ended up solving the issue, thanks for your help! 🍻