'Change button style if clicked in discord.js
I need help about changing the button style in Discord.JS. interaction.component.setStyle("DANGER") works, but for multi-buttons inside an Action Row doesn't work. I tried <row>.components, it shows an array of MessageButton. I can also <row>.components[0].setStyle("DANGER") but it can't do the clicked one. Can you please help? Thank you!
Solution 1:[1]
Using interaction.message.components[0] would return the first MessageActionRow.
Take a look at the image that I've linked below to see the numbers that I've been talking about.
In order to edit button 5, you would have to get the 2nd button of the 2nd row.
This would be interaction.components[1].components[1]. You can then edit stuff like the style, label, etc. by running the usual .setLabel("Something") function.
Don't forget to reply to the interaction. In your case you would have to update the original interaction-message.
A possible way to insert this would be:
const { Client } = require("discord.js")
const client = new Client(/*Your intents*/)
//Your code to create the buttons, etc.
client.on("interactionCreate", interaction=>{
if(interaction.isButton()){
if(interaction.customId=="your custom button id"){
//Remember to start counting with 0. 0 in the code equals 1 in real life...
interaction.message.components[1].interaction[1].setStyle("DANGER")
interaction.update({
components: interaction.components
})
}
}
})
client.login("your token")
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 |
