ButtonBuilder class

const button = new ButtonBuilder(data?: object);

constructor

The constructor provides one optional argument which is an object which provides the data for the ButtonBuilder class.

The object may contain the following information:

  • custom_id: An id so you can recognize the button when it's being pressed by a member. The id must be unique and a string between 1-100 characters.

  • style: A number which defines which kind of style the button should have.

  • label: The text which should be on the button. The string must be between 1-100 characters.

  • emoji: An object which defines the emoji which should be on the button, if you want an emoji on the button. The object should provide whether the emoji is animated or not, the id of the emoji and the name of the emoji.

  • disabled: A boolean which defines whether the button can be pressed or not.

  • url: A url for the member to go to when the member presses the button.

  • type: A number which defines the type of component of the button. (2 means it's a button)

Properties

data

An object which makes it a valid object for the Discord API.

{
    custom_id?: string;
    style: number;
    label: string;
    emoji?: {
        animated: boolean;
        id?: string;
        name: string;
    }
    disabled: boolean;
    url?: string;
    type: number;
}

Functions

setCustomId

button.setCustomId(customId: string) : ButtonBuilder;

Sets a custom id for the button which will be returned to the bot once the button is pressed and makes you able to recognize the button. The function returns the same ButtonBuilder class so you don't have to declare it everytime again.

ArgumentDescription

customId

A string of max 100 characters which will be returned to the bot in the buttonClick event so you can recognize the button which was pressed by the member.

setDisabled

button.setDisabled(disabled?: boolean) : ButtonBuilder;

Defines whether the button may be pressed by a member or not. The function returns the same ButtonBuilder class so you don't have to declare it everytime again.

ArgumentDescription

disabled

A boolean which defines whether the button should be disabled or may be pressed by a member.

setLabel

button.setLabel(label: string) : ButtonBuilder;

Sets a label for the button which is visible to server members. The function returns the same ButtonBuilder class so you don't have to declare it everytime again.

ArgumentDescription

label

A string of max 100 characters which should be the text on the button.

setText

button.setText(text: string) : ButtonBuilder;

Identical to the setLabel function. Sets a label for the button which is visible to server members. The function returns the same ButtonBuilder class so you don't have to declare it everytime again.

ArgumentDescription

text

A string of max 100 characters which should be the text on the button.

setStyle

button.setStyle(style: string | number) : ButtonBuilder;

Sets the style for the button and defines how the button will look like. The function returns the same ButtonBuilder class so you don't have to declare it everytime again.

ArgumentDescription

style

A string or a number which defines the style of the button. In the table below is explained which strings and numbers are allowed.

ColorStyle type

Blurple button

For a blurple button you can use the strings blurple and primary. You can also use the number 1 to define the blurple color.

Gray button

For a gray button you can use the strings gray, grey and secondary. You can also use the number 2 to define the gray color.

Green button

For a green button you can use the strings green and success. You can also use the number 3 to define the green color.

Red button

For a red button you can use the strings red and danger. You can also use the number 4 to define the red button.

setURL

button.setURL(url: string) : ButtonBuilder;

Sets a url for the button so when a member presses the button, the member will be redirected to the website. When setting the url, the style type will automatically change to a url button style. The function returns the same ButtonBuilder class so you don't have to declare it everytime again.

ArgumentDescription

url

The url of the website the member should be redirected to when pressing the button. The url must be a valid website url.

setEmoji

button.setEmoji(string | Emoji) : ButtonBuilder;

Adds an emoji to the button. The emoji must be a unicode emoji or an instance of the Emoji class. The function returns the same ButtonBuilder class so you don't have to declare it everytime again.

ArgumentDescription

emoji

The emoji which should be added to the button. The emoji must be a unicode emoji or an instance of the Emoji class.

toJSON

Converts the ButtonBuilder class to an object to make it a valid object for the Discord API. The function returns the same data as the data property.

{
    custom_id?: string;
    style: number;
    label: string;
    emoji?: {
        animated: boolean;
        id?: string;
        name: string;
    }
    disabled: boolean;
    url?: string;
    type: number;
}

Last updated