-1

Hey, I have a question. Since I now use Discord JS 14 & the Discord JS 13 method to edit the style of a button doesn't work, I want to ask here if someone has a solution.

This is how we did in it Discord.JS 13:

interaction.component.setLabel("Label")
interaction.update({
  components: interaction.message.components
});

I have tried many thing but couldn't find a solution so I want to ask here.

Rob
  • 14,746
  • 28
  • 47
  • 65

1 Answers1

1

I don't know if you still search for a solution, but I found this related answer to a question.

Long story short, you need to rebuild all buttons, as you no longer can update an already existing button's style.

For comparison, here's my old code for updating the clicked button's style to primary, and every others to secondary :

const actionsRows = message.components.map((actionsRow) => ({
  ...actionsRow,
  components: actionsRow.components.map((button) => {
    const buttonStyle = button.customId === clickedButtonId
      ? 'SUCCESS'
      : 'SECONDARY';
    button.setStyle(buttonStyle);
    button.setDisabled(true);

    return button;
  }),
}));

message.edit({ components: actionsRows });

And here's the new code :

const newActionRowEmbeds = message.components.map((oldActionRow) => {
  const updatedActionRow = new ActionRowBuilder();

  updatedActionRow.addComponents(
    oldActionRow.components.map((buttonComponent) => {
      const newButton = ButtonBuilder.from(buttonComponent);
      const buttonStyle = buttonComponent.customId === clickedButtonId
        ? ButtonStyle.Primary
        : ButtonStyle.Secondary;
      newButton.setStyle(buttonStyle);
      newButton.setDisabled(true);

      return newButton;
    }),
  );
  return updatedActionRow;
});

message.edit({ components: newActionRowEmbeds });