#Components v2 - SectionBuilder with button

40 messages · Page 1 of 1 (latest)

outer cargo
#

Hey everyone. I'm trying to add a ButtonBuilder to a SectionBuilder, nested in a ContainerBuilder. But when trying to post the message, I get an error:

path/node_modules/@discordjs/builders/dist/index.mjs:486
      throw new RangeError("Non-premium buttons must have a label and/or an emoji.");
            ^

RangeError: Non-premium buttons must have a label and/or an emoji.
    at validateRequiredButtonParameters (path/node_modules/@discordjs/builders/dist/index.mjs:486:13)
    at ButtonBuilder.toJSON (path/node_modules/@discordjs/builders/dist/index.mjs:654:5)
    at SectionBuilder.toJSON (path/node_modules/@discordjs/builders/dist/index.mjs:2102:59)       
    at path/src/commands/polls/polls.js:271:34
    at Array.forEach (<anonymous>)
    at Object.action (path/src/commands/polls/polls.js:252:20)

Node.js v20.11.1

I tried to look at an error in my code but I cannot find one.

const container = new ContainerBuilder();

//create button
const buttonId = "polls_" + idx.toString();
const button = createButton(buttonId, field, ButtonStyle.Secondary, emote);
const fieldSection = new SectionBuilder().setButtonAccessory(button);

//line 271
console.log(fieldSection.toJSON());

container.addSectionComponents(fieldSection);
interaction.channel.send({flags: MessageFlags.IsComponentsV2, components: [container]});

//creatButton function:
export const createButton = (id, label, style, emoji) => {
  const button = new ButtonBuilder().setCustomId(id).setStyle(style);
  if (label) button.setLabel(label);
  if (emoji) button.setEmoji(emoji);
  return button;
};

When investigating the Builders, I have this weird accessory in the SectionBuilder:

SectionBuilder {
  data: { type: 9 },
  //components
  accessory: ButtonBuilder {
    data: {
      type: 2,
      data: {
        type: 2,
        emoji: undefined,
        custom_id: 'polls_0',
        style: 2,
        label: 'Choice 1'
      }
    }
  }
}
cosmic roseBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • 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
scarlet basin
#

emoji is undefined

little trellis
outer cargo
# scarlet basin emoji is undefined

I have the same issue when passing an emote ^^

ButtonBuilder {
  data: {
    type: 2,
    data: {
      type: 2,
      emoji: { id: undefined, name: '1️⃣', animated: false },
      custom_id: 'polls_0',
      style: 2,
      label: 'Choice 1'
    }
  }
}
little trellis
#

What version of djs are you on

outer cargo
#

14.19.3

little trellis
#

Do you have any other buttons in your application

outer cargo
#

I have some that I used before this djs version, all working fine with ActionRows. But when trying to adapt my code to Components V2, I have this weird ButtonBuilder.

little trellis
#

Your section is mal formed. I missed it you need to add a text display component.

outer cargo
#

I have some but skipped that part because I was above the character limit ^^

#
SectionBuilder {
  data: { type: 9 },
  components: [
    TextDisplayBuilder { data: [Object] },
    TextDisplayBuilder { data: [Object] }
  ],
  accessory: ButtonBuilder { data: { type: 2, data: [Object] } }
}
little trellis
#

I see

#

Well I need to see the full code. bc what you shared look fine

shut meteorBOT
#

To share long code snippets, use a service like gist, sourcebin, pastebin, or similar instead of posting them as large code blocks or files.

gleaming bison
outer cargo
#

Oh that's right, I forgot this one. I've edited the post above to have it inside

console.log(fieldSection.toJSON());
silent belfry
#

At least from your snippet, the error is that you are creating a Section with no TextDisplay in it.

Instead, send the button in an ActionRow

outer cargo
silent belfry
#

Ah I see, I missed that part... My bad

outer cargo
#

Np ^^

gleaming bison
outer cargo
#

I've added some "console.log" in the corresponding "index.mjs" file

gleaming bison
#

So in a completely different place. That doesn't tell you anything about the state of your code right here. Why not log right there instead?

outer cargo
#

It was just the next step because I have the same content in my code. I was looking at djs ^^

gleaming bison
gleaming bison
outer cargo
outer cargo
# gleaming bison And you apparently don't have the same content, because if you did then it would...

The logs from the console logs in the polls.js file:

button ButtonBuilder {
  data: {
    type: 2,
    emoji: { id: undefined, name: '1️⃣', animated: false },
    custom_id: 'polls_0',
    style: 2,
    label: 'Choice 1'
  }
}
section SectionBuilder {
  data: { type: 9 },
  components: [
    TextDisplayBuilder { data: [Object] },
    TextDisplayBuilder { data: [Object] }
  ],
  accessory: ButtonBuilder { data: { type: 2, data: [Object] } }
}
section ButtonBuilder {
  data: {
    type: 2,
    data: {
      type: 2,
      emoji: [Object],
      custom_id: 'polls_0',
      style: 2,
      label: 'Choice 1'
    }
  }
}
gleaming bison
outer cargo
#
results {
  fields: [ 'Choice 1', 'Choice 2', 'Choice 3' ],
  emotes: [ '1️⃣', '2️⃣', '3️⃣' ]
}
#

And if you want the command input:

gleaming bison
#

Ah, you need to import the builders from discord.js, not /builders

#

Else the internal instanceof check fails

outer cargo
#

Oh

#

Okay now it works