Conditional display of Button Components in Text Channel (based on user role?)
I have a couple of buttons:
const queueButton = new ButtonBuilder()
.setLabel('Join Queue')
.setStyle(ButtonStyle.Primary)
.setCustomId('join-queue-button');
const leaveButton = new ButtonBuilder()
.setLabel('Leave Queue')
.setStyle(ButtonStyle.Secondary)
.setCustomId('leave-queue-button');
const buttonRow = new ActionRowBuilder()
.addComponents(queueButton, leaveButton)
And am sending them to the text chat thus:
async function sendEmbed() {
const textChannel = client.channels.cache.get(textChannelId);
if (textChannel) {
await textChannel.send({components: [buttonRow]})
.then((message) => {
console.log('Embed sent successfully')
})
.catch(console.error);
const collector = textChannel.createMessageComponentCollector({
componentType: ComponentType.Button
});
collector.on('collect', async (interaction) => {
if (interaction.customId === 'join-queue-button') {
await handleJoinButton(interaction)
} else if (interaction.customId === 'leave-queue-button') {
await handleLeaveButton(interaction)
}
});
} else {
console.error('Text channel not found or not text-based');
}
}
But would like only certain users viewing the chat to have the view:
- Join button visible, leave button hidden
Otherwise, users would see:
- Join button hidden, leave button visible
Is this possible maybe by using roles for users or some other way?2 Replies
- 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!Fair enough. I'll just put in some handling for the redundant buttons. Thanks 🙂