DiscordPHP Documentation

Option extends Part
in package

Option represents an array of options that can be given to a command.

Tags
link
https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure
since
7.0.0

Table of Contents

ATTACHMENT  = 11
BOOLEAN  = 5
CHANNEL  = 7
INTEGER  = 4
MENTIONABLE  = 9
NUMBER  = 10
ROLE  = 8
STRING  = 3
SUB_COMMAND  = 1
SUB_COMMAND_GROUP  = 2
USER  = 6
$autocomplete  : bool|null
$channel_types  : array<string|int, mixed>|null
$choices  : Collection|array<string|int, Choice>|null
$created  : bool
Is the part already created in the Discord servers?
$description  : string
$description_localizations  : array<string|int, ?string>|null
$max_length  : int|null
$max_value  : int|float|null
$min_length  : int|null
$min_value  : int|float|null
$name  : string
$name_localizations  : array<string|int, ?string>|null
$options  : Collection|array<string|int, Option>
$required  : bool|null
$scriptData  : mixed
Custom script data.
$type  : int
$attributes  : array<string|int, mixed>
The parts attributes.
$discord  : Discord
The Discord client.
$factory  : Factory
The factory.
$fillable  : array<string|int, mixed>
The parts fillable attributes.
$hidden  : array<string|int, mixed>
Attributes that are hidden from debug info.
$http  : Http
The HTTP client.
$repositories  : array<string|int, mixed>
An array of repositories that can exist in a part.
$repositories_cache  : array<string|int, mixed>
An array of repositories.
$visible  : array<string|int, mixed>
Attributes which are visible from debug info.
__construct()  : mixed
Create a new part instance.
__debugInfo()  : array<string|int, mixed>
Handles debug calls from var_dump and similar functions.
__get()  : mixed
Handles dynamic get calls onto the part.
__serialize()  : array<string|int, mixed>
__set()  : void
Handles dynamic set calls onto the part.
__toString()  : string
Converts the part to a string.
__unserialize()  : void
addChoice()  : $this
Adds a choice to the option (Only for slash commands).
addOption()  : $this
Adds an option to the option.
fetch()  : ExtendedPromiseInterface<string|int, self>
Fetches any missing information about the part from Discord's servers.
fill()  : void
Fills the parts attributes from an array.
getCreatableAttributes()  : array<string|int, mixed>
Returns the attributes needed to create.
getPublicAttributes()  : array<string|int, mixed>
Returns an array of public attributes.
getRawAttributes()  : array<string|int, mixed>
Returns an array of raw attributes.
getRepositoryAttributes()  : array<string|int, mixed>
Gets the attributes to pass to repositories.
getUpdatableAttributes()  : array<string|int, mixed>
Returns the updatable attributes.
isPartial()  : bool
Whether the part is considered partial i.e. missing information which can be fetched from Discord.
jsonSerialize()  : array<string|int, mixed>
Provides data when the part is encoded into JSON. Used for JsonSerializable.
offsetExists()  : bool
Checks if an attribute exists via key. Used for ArrayAccess.
offsetGet()  : mixed
Gets an attribute via key. Used for ArrayAccess.
offsetSet()  : void
Sets an attribute via key. Used for ArrayAccess.
offsetUnset()  : void
Unsets an attribute via key. Used for ArrayAccess.
removeChoice()  : $this
Removes a choice (Only for slash commands).
removeOption()  : $this
Removes an option.
serialize()  : string|null
Serializes the data. Used for Serializable.
setAutoComplete()  : $this
Sets the autocomplete interactions for this option.
setChannelTypes()  : $this
Sets the channel types of the option.
setDescription()  : $this
Sets the description of the option.
setDescriptionLocalization()  : $this
Sets the description of the option in another language.
setMaxLength()  : $this
Sets the maximum length permitted.
setMaxValue()  : $this
Sets the maximum value permitted.
setMinLength()  : $this
Sets the minimum length permitted.
setMinValue()  : $this
Sets the minimum value permitted.
setName()  : $this
Sets the name of the option.
setNameLocalization()  : $this
Sets the name of the option in another language.
setRequired()  : $this
Sets the requirement of the option.
setType()  : $this
Sets the type of the option.
unserialize()  : void
Unserializes some data and stores it. Used for Serializable.
afterConstruct()  : void
Called after the part has been constructed.
getChoicesAttribute()  : Collection|array<string|int, Choice>|null
Gets the choices attribute.
getOptionsAttribute()  : Collection|array<string|int, Option>
Gets the options attribute.
checkForMutator()  : string|false
Checks if there is a mutator present.
getAttribute()  : mixed
Gets an attribute on the part.
setAttribute()  : void
Sets an attribute on the part.
studly()  : string
Converts a string to studlyCase.

Constants

ATTACHMENT

public mixed ATTACHMENT = 11

BOOLEAN

public mixed BOOLEAN = 5

CHANNEL

public mixed CHANNEL = 7

INTEGER

public mixed INTEGER = 4

MENTIONABLE

public mixed MENTIONABLE = 9

SUB_COMMAND

public mixed SUB_COMMAND = 1

SUB_COMMAND_GROUP

public mixed SUB_COMMAND_GROUP = 2

Properties

$autocomplete

public bool|null $autocomplete

Enable autocomplete interactions for this option.

$channel_types

public array<string|int, mixed>|null $channel_types

If the option is a channel type, the channels shown will be restricted to these types.

$choices

public Collection|array<string|int, Choice>|null $choices

Choices for STRING, INTEGER, and NUMBER types for the user to pick from, max 25. Only for slash commands.

$created

Is the part already created in the Discord servers?

public bool $created = false

Whether the part has been created.

$description

public string $description

1-100 character description.

$description_localizations

public array<string|int, ?string>|null $description_localizations

Localization dictionary for the description field. Values follow the same restrictions as description.

$max_length

public int|null $max_length

For option type STRING, the maximum allowed length (minimum of 1, maximum of 6000).

$max_value

public int|float|null $max_value

If the option is an INTEGER or NUMBER type, the maximum value permitted.

$min_length

public int|null $min_length

For option type STRING, the minimum allowed length (minimum of 0, maximum of 6000).

$min_value

public int|float|null $min_value

If the option is an INTEGER or NUMBER type, the minimum value permitted.

$name

public string $name

Name of the option.

$name_localizations

public array<string|int, ?string>|null $name_localizations

Localization dictionary for the name field. Values follow the same restrictions as name.

$required

public bool|null $required

If the parameter is required or optional--default false.

$scriptData

Custom script data.

public mixed $scriptData

Used for storing custom information, used by end products.

Tags
deprecated
10.0.0

Relying on this variable with dynamic caching is discouraged.

$type

public int $type

Type of the option.

$attributes

The parts attributes.

protected array<string|int, mixed> $attributes = []

The parts attributes and content.

$discord

The Discord client.

protected Discord $discord

Client.

$fillable

The parts fillable attributes.

protected array<string|int, mixed> $fillable = ['type', 'name', 'name_localizations', 'description', 'description_localizations', 'required', 'choices', 'options', 'channel_types', 'min_value', 'max_value', 'min_length', 'max_length', 'autocomplete']

The array of attributes that can be mass-assigned.

$hidden

Attributes that are hidden from debug info.

protected array<string|int, mixed> $hidden = []

Attributes that are hidden from public.

$http

The HTTP client.

protected Http $http

Client.

$repositories

An array of repositories that can exist in a part.

protected array<string|int, mixed> $repositories = []

Repositories.

$repositories_cache

An array of repositories.

protected array<string|int, mixed> $repositories_cache = []

$visible

Attributes which are visible from debug info.

protected array<string|int, mixed> $visible = []

Methods

__construct()

Create a new part instance.

public __construct(Discord $discord[, array<string|int, mixed> $attributes = [] ][, bool $created = false ]) : mixed
Parameters
$discord : Discord

The Discord client.

$attributes : array<string|int, mixed> = []

An array of attributes to build the part.

$created : bool = false

Whether the part has already been created.

Return values
mixed

__debugInfo()

Handles debug calls from var_dump and similar functions.

public __debugInfo() : array<string|int, mixed>
Tags
throws
Exception
see
Part::getPublicAttributes()

This function forwards onto getPublicAttributes.

Return values
array<string|int, mixed>

An array of public attributes.

__get()

Handles dynamic get calls onto the part.

public __get(string $key) : mixed
Parameters
$key : string

The attributes key.

Tags
throws
Exception
see
Part::getAttribute()

This function forwards onto getAttribute.

Return values
mixed

The value of the attribute.

__serialize()

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

__set()

Handles dynamic set calls onto the part.

public __set(string $key, mixed $value) : void
Parameters
$key : string

The attributes key.

$value : mixed

The attributes value.

Tags
see
self::setAttribute()

This function forwards onto setAttribute.

Return values
void

__toString()

Converts the part to a string.

public __toString() : string
Tags
throws
Exception
see
Part::getPublicAttributes()

This function encodes getPublicAttributes into JSON.

Return values
string

A JSON string of attributes.

__unserialize()

public __unserialize(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>
Return values
void

addChoice()

Adds a choice to the option (Only for slash commands).

public addChoice(Choice $choice) : $this
Parameters
$choice : Choice

The choice.

Tags
throws
OverflowException

Command exceeds maximum 25 choices.

Return values
$this

addOption()

Adds an option to the option.

public addOption(Option $option) : $this
Parameters
$option : Option

The option.

Tags
throws
OverflowException

Command exceeds maximum 25 sub options.

Return values
$this

fetch()

Fetches any missing information about the part from Discord's servers.

public fetch() : ExtendedPromiseInterface<string|int, self>
Tags
throws
RuntimeException

The part is not fetchable.

Return values
ExtendedPromiseInterface<string|int, self>

fill()

Fills the parts attributes from an array.

public fill(array<string|int, mixed> $attributes) : void
Parameters
$attributes : array<string|int, mixed>

An array of attributes to build the part.

Return values
void

getCreatableAttributes()

Returns the attributes needed to create.

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

getPublicAttributes()

Returns an array of public attributes.

public getPublicAttributes() : array<string|int, mixed>
Tags
throws
Exception
Return values
array<string|int, mixed>

An array of public attributes.

getRawAttributes()

Returns an array of raw attributes.

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

Raw attributes.

getRepositoryAttributes()

Gets the attributes to pass to repositories.

public getRepositoryAttributes() : array<string|int, mixed>

Note: The order matters for repository tree (top to bottom).

Return values
array<string|int, mixed>

Attributes.

getUpdatableAttributes()

Returns the updatable attributes.

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

isPartial()

Whether the part is considered partial i.e. missing information which can be fetched from Discord.

public isPartial() : bool
Return values
bool

jsonSerialize()

Provides data when the part is encoded into JSON. Used for JsonSerializable.

public jsonSerialize() : array<string|int, mixed>
Tags
throws
Exception
see
Part::getPublicAttributes()

This function forwards onto getPublicAttributes.

Return values
array<string|int, mixed>

An array of public attributes.

offsetExists()

Checks if an attribute exists via key. Used for ArrayAccess.

public offsetExists(string $key) : bool
Parameters
$key : string

The attribute key.

Return values
bool

Whether the offset exists.

offsetGet()

Gets an attribute via key. Used for ArrayAccess.

public offsetGet(string $key) : mixed
Parameters
$key : string

The attribute key.

Tags
throws
Exception
see
Part::getAttribute()

This function forwards onto getAttribute.

Return values
mixed

offsetSet()

Sets an attribute via key. Used for ArrayAccess.

public offsetSet(string $key, mixed $value) : void
Parameters
$key : string

The attribute key.

$value : mixed

The attribute value.

Tags
see
Part::setAttribute()

This function forwards onto setAttribute.

Return values
void

offsetUnset()

Unsets an attribute via key. Used for ArrayAccess.

public offsetUnset(string $key) : void
Parameters
$key : string

The attribute key.

Return values
void

removeChoice()

Removes a choice (Only for slash commands).

public removeChoice(string|Choice $choice) : $this
Parameters
$choice : string|Choice

Choice object or name to remove.

Return values
$this

removeOption()

Removes an option.

public removeOption(string|Option $option) : $this
Parameters
$option : string|Option

Option object or name to remove.

Return values
$this

serialize()

Serializes the data. Used for Serializable.

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

A string of serialized data.

setAutoComplete()

Sets the autocomplete interactions for this option.

public setAutoComplete(bool|null $autocomplete) : $this
Parameters
$autocomplete : bool|null

enable autocomplete interactions for this option.

Tags
throws
DomainException

Command option type is not string/integer/number.

Return values
$this

setChannelTypes()

Sets the channel types of the option.

public setChannelTypes(array<string|int, mixed>|null $types) : $this
Parameters
$types : array<string|int, mixed>|null

types of the channel.

Return values
$this

setDescription()

Sets the description of the option.

public setDescription(string $description) : $this
Parameters
$description : string

description of the option.

Tags
throws
LengthException

$description is more than 100 characters.

Return values
$this

setDescriptionLocalization()

Sets the description of the option in another language.

public setDescriptionLocalization(string $locale, string|null $description) : $this
Parameters
$locale : string

Discord locale code.

$description : string|null

Localized description of the option.

Tags
throws
LengthException

$description is more than 100 characters.

Return values
$this

setMaxLength()

Sets the maximum length permitted.

public setMaxLength(int|null $max_length) : $this
Parameters
$max_length : int|null

For option type STRING, the maximum allowed length (minimum of 1).

Tags
throws
LogicException
throws
LengthException
Return values
$this

setMaxValue()

Sets the maximum value permitted.

public setMaxValue(int|float|null $max_value) : $this
Parameters
$max_value : int|float|null

integer for INTEGER options, double for NUMBER options

Return values
$this

setMinLength()

Sets the minimum length permitted.

public setMinLength(int|null $min_length) : $this
Parameters
$min_length : int|null

For option type STRING, the minimum allowed length (minimum of 0).

Tags
throws
LogicException
throws
LengthException
Return values
$this

setMinValue()

Sets the minimum value permitted.

public setMinValue(int|float|null $min_value) : $this
Parameters
$min_value : int|float|null

integer for INTEGER options, double for NUMBER options.

Return values
$this

setName()

Sets the name of the option.

public setName(string $name) : $this

CHAT_INPUT command option names must match the following regex ^[\w-]{1,32}$ with the unicode flag set. If there is a lowercase variant of any letters used, you must use those. Characters with no lowercase variants and/or uncased letters are still allowed.

Parameters
$name : string

name of the option. Slash command option names are lowercase.

Tags
throws
LengthException

$name is more than 32 characters.

Return values
$this

setNameLocalization()

Sets the name of the option in another language.

public setNameLocalization(string $locale, string|null $name) : $this

CHAT_INPUT command option names must match the following regex ^[\w-]{1,32}$ with the unicode flag set. If there is a lowercase variant of any letters used, you must use those. Characters with no lowercase variants and/or uncased letters are still allowed.

Parameters
$locale : string

Discord locale code.

$name : string|null

Localized name of the option. Slash command option names are lowercase.

Tags
throws
LengthException

$name is more than 32 characters.

Return values
$this

setRequired()

Sets the requirement of the option.

public setRequired([bool $required = false ]) : $this
Parameters
$required : bool = false

requirement of the option (default false)

Return values
$this

setType()

Sets the type of the option.

public setType(int $type) : $this
Parameters
$type : int

type of the option.

Tags
throws
InvalidArgumentException

$type is not 1-11.

Return values
$this

unserialize()

Unserializes some data and stores it. Used for Serializable.

public unserialize(string $data) : void
Parameters
$data : string

Some serialized data.

Tags
see
Part::setAttribute()

The unserialized data is stored with setAttribute.

Return values
void

afterConstruct()

Called after the part has been constructed.

protected afterConstruct() : void
Return values
void

getChoicesAttribute()

Gets the choices attribute.

protected getChoicesAttribute() : Collection|array<string|int, Choice>|null
Return values
Collection|array<string|int, Choice>|null

A collection of choices.

getOptionsAttribute()

Gets the options attribute.

protected getOptionsAttribute() : Collection|array<string|int, Option>
Return values
Collection|array<string|int, Option>

A collection of options.

checkForMutator()

Checks if there is a mutator present.

private checkForMutator(string $key, string $type) : string|false
Parameters
$key : string

The attribute name to check.

$type : string

Either get or set.

Return values
string|false

Either a string if it is callable or false.

getAttribute()

Gets an attribute on the part.

private getAttribute(string $key) : mixed
Parameters
$key : string

The key to the attribute.

Tags
throws
Exception
Return values
mixed

Either the attribute if it exists or void.

setAttribute()

Sets an attribute on the part.

private setAttribute(string $key, mixed $value) : void
Parameters
$key : string

The key to the attribute.

$value : mixed

The value of the attribute.

Return values
void

studly()

Converts a string to studlyCase.

private static studly(string $string) : string

This is a port of updated Laravel's implementation, a non-regex with static cache. The Discord\studly() is kept due to unintended bug and we do not want to introduce BC by replacing it. This method is private static as we may move it outside this class in future.

Parameters
$string : string

The string to convert.

Tags
since
10.0.0
Return values
string

Search results