DiscordPHP Documentation

Button extends Component
in package

Buttons are interactive components that render in messages.

They can be clicked by users, and send an interaction to your app when clicked.

Tags
link
https://discord.com/developers/docs/interactions/message-components#buttons
since
7.0.0

Table of Contents

STYLE_DANGER  = 4
STYLE_LINK  = 5
STYLE_PRIMARY  = 1
STYLE_SECONDARY  = 2
STYLE_SUCCESS  = 3
TYPE_ACTION_ROW  = 1
TYPE_BUTTON  = 2
TYPE_CHANNEL_SELECT  = 8
TYPE_MENTIONABLE_SELECT  = 7
TYPE_ROLE_SELECT  = 6
TYPE_SELECT_MENU  = 3
TYPE_STRING_SELECT  = 3
TYPE_TEXT_INPUT  = 4
TYPE_USER_SELECT  = 5
$custom_id  : string|null
Custom ID to send with the button.
$disabled  : bool
Whether the button is disabled.
$discord  : Discord|null
Discord instance when the listener is set.
$emoji  : array<string|int, mixed>|null
Emoji to display on the button.
$label  : string|null
Label for the button.
$listener  : callable|null
Listener for when the button is pressed.
$style  : int
Style of button.
$url  : string|null
URL to send as the button. Only for link buttons.
__construct()  : mixed
Creates a new button.
getCustomId()  : string|null
Returns the custom ID of the button.
getEmoji()  : array<string|int, mixed>|null
Returns the emoji display on the button.
getLabel()  : string|null
Returns the label for the button.
getStyle()  : int
Returns the style for the button.
getURL()  : string|null
Returns the URL of the button. Only for link buttons.
isDisabled()  : bool
Returns whether the button is disabled.
jsonSerialize()  : array<string|int, mixed>
{@inheritDoc}
new()  : self
Creates a new button.
removeListener()  : $this
Removes the listener from the button.
setCustomId()  : $this
Sets the custom ID of the button.
setDisabled()  : $this
Sets the button as disabled/not disabled.
setEmoji()  : $this
Sets the emoji of the button.
setLabel()  : $this
Sets the label of the button.
setListener()  : $this
Sets the callable listener for the button. The `$callback` will be called when the button is pressed.
setStyle()  : $this
Sets the style of the button.
setUrl()  : $this
Sets the URL of the button. Only valid for link buttons.tatic.
generateUuid()  : string
Generates a UUID which can be used for component custom IDs.

Constants

STYLE_DANGER

public mixed STYLE_DANGER = 4
public mixed STYLE_LINK = 5

STYLE_PRIMARY

public mixed STYLE_PRIMARY = 1

STYLE_SECONDARY

public mixed STYLE_SECONDARY = 2

STYLE_SUCCESS

public mixed STYLE_SUCCESS = 3

TYPE_ACTION_ROW

public mixed TYPE_ACTION_ROW = 1

TYPE_CHANNEL_SELECT

public mixed TYPE_CHANNEL_SELECT = 8

TYPE_MENTIONABLE_SELECT

public mixed TYPE_MENTIONABLE_SELECT = 7

TYPE_ROLE_SELECT

public mixed TYPE_ROLE_SELECT = 6

TYPE_SELECT_MENU

public mixed TYPE_SELECT_MENU = 3
Tags
deprecated
7.4.0

Use Component::TYPE_STRING_SELECT

TYPE_STRING_SELECT

public mixed TYPE_STRING_SELECT = 3

TYPE_TEXT_INPUT

public mixed TYPE_TEXT_INPUT = 4

TYPE_USER_SELECT

public mixed TYPE_USER_SELECT = 5

Properties

$custom_id

Custom ID to send with the button.

private string|null $custom_id

$disabled

Whether the button is disabled.

private bool $disabled = false

$discord

Discord instance when the listener is set.

private Discord|null $discord

$emoji

Emoji to display on the button.

private array<string|int, mixed>|null $emoji

$label

Label for the button.

private string|null $label

$listener

Listener for when the button is pressed.

private callable|null $listener

$style

Style of button.

private int $style = 1

$url

URL to send as the button. Only for link buttons.

private string|null $url

Methods

__construct()

Creates a new button.

public __construct(int $style[, string|null $custom_id = null ]) : mixed
Parameters
$style : int

Style of the button.

$custom_id : string|null = null

custom ID of the button. If not given, an UUID will be used

Tags
throws
InvalidArgumentException
Return values
mixed

getCustomId()

Returns the custom ID of the button.

public getCustomId() : string|null
Return values
string|null

getEmoji()

Returns the emoji display on the button.

public getEmoji() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

getLabel()

Returns the label for the button.

public getLabel() : string|null
Return values
string|null

getStyle()

Returns the style for the button.

public getStyle() : int
Return values
int

getURL()

Returns the URL of the button. Only for link buttons.

public getURL() : string|null
Return values
string|null

isDisabled()

Returns whether the button is disabled.

public isDisabled() : bool
Return values
bool

jsonSerialize()

{@inheritDoc}

public jsonSerialize() : array<string|int, mixed>
Return values
array<string|int, mixed>

new()

Creates a new button.

public static new(int $style[, string|null $custom_id = null ]) : self
Parameters
$style : int

Style of the button.

$custom_id : string|null = null

custom ID of the button.

Return values
self

removeListener()

Removes the listener from the button.

public removeListener() : $this
Return values
$this

setCustomId()

Sets the custom ID of the button.

public setCustomId(string|null $custom_id) : $this
Parameters
$custom_id : string|null
Tags
throws
LogicException
throws
LengthException
Return values
$this

setDisabled()

Sets the button as disabled/not disabled.

public setDisabled(bool $disabled) : $this
Parameters
$disabled : bool
Return values
$this

setEmoji()

Sets the emoji of the button.

public setEmoji(Emoji|string|null $emoji) : $this
Parameters
$emoji : Emoji|string|null

Emoji to set. null to clear.

Return values
$this

setLabel()

Sets the label of the button.

public setLabel(string|null $label) : $this
Parameters
$label : string|null

Label of the button. Maximum 80 characters.

Tags
throws
LengthException
Return values
$this

setListener()

Sets the callable listener for the button. The `$callback` will be called when the button is pressed.

public setListener(callable $callback, Discord $discord[, bool $oneOff = false ]) : $this

If you do not respond to or acknowledge the Interaction, it will be acknowledged for you. Note that if you intend to respond to or acknowledge the interaction inside a promise, you should return a promise that resolves after you respond or acknowledge.

The callback will only be called once with the $oneOff parameter set to true. This can be changed to false, and the callback will be called each time the button is pressed. To remove the listener, you can pass $callback as null.

The button listener will not persist when the bot restarts.

Parameters
$callback : callable

Callback to call when the button is pressed. Will be called with the interaction object.

$discord : Discord

Discord client.

$oneOff : bool = false

Whether the listener should be removed after the button is pressed for the first time.

Tags
throws
LogicException
Return values
$this

setStyle()

Sets the style of the button.

public setStyle(int $style) : $this

If the button is originally a link button, the link attribute will be cleared. If the button was changed to a link button, the listener will be cleared.

Parameters
$style : int
Tags
throws
InvalidArgumentException
Return values
$this

setUrl()

Sets the URL of the button. Only valid for link buttons.tatic.

public setUrl(string|null $url) : $this
Parameters
$url : string|null
Tags
throws
LogicException
Return values
$this

generateUuid()

Generates a UUID which can be used for component custom IDs.

protected static generateUuid() : string
Return values
string

Search results