DiscordPHP Documentation

Webhook extends Part
in package

Webhooks are a low-effort way to post messages to channels in Discord. They do not require a bot user or authentication to use.

Tags
link
https://discord.com/developers/docs/resources/webhook#webhook-resource
since
5.0.0
@property

?string|null $guild_id The guild ID this webhook is for, if any.

@property

?string|null $channel_id The channel ID this webhook is for, if any.

Table of Contents

Constants

TYPE_APPLICATION  = 3
TYPE_CHANNEL_FOLLOWER  = 2
TYPE_INCOMING  = 1

Properties

$application_id  : string|null
$avatar  : string|null
$channel  : Channel|null
$created  : bool
Is the part already created in the Discord servers?
$guild  : Guild|null
$id  : string
$messages  : WebhookMessageRepository
$name  : string|null
$scriptData  : mixed
Custom script data.
$source_channel  : Channel|null
$source_guild  : Guild|null
$token  : string|null
$type  : int
$url  : string|null
$user  : User|null
$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.

Methods

__construct()  : mixed
Create a new part instance.
__debugInfo()  : array<string|int, mixed>
__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
execute()  : PromiseInterface<string|int, Message|void>
Executes the webhook with an array of data.
executeGitHub()  : PromiseInterface
Executes a GitHub-compatible webhook.
executeSlack()  : PromiseInterface
Executes a Slack-compatible webhook.
fetch()  : PromiseInterface<string|int, static>
Fetches any missing information about the part from Discord's servers.
fill()  : void
Fills the parts attributes from an array.
getConstants()  : array<string|int, mixed>
Returns an array of constant names and their values.
getCreatableAttributes()  : array<string|int, mixed>
getDiscord()  : Discord
Get the Discord instance that owns this Part.
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>
getUpdatableAttributes()  : array<string|int, mixed>
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.
serialize()  : string|null
Serializes the data. Used for Serializable.
unserialize()  : void
Unserializes some data and stores it. Used for Serializable.
updateMessage()  : PromiseInterface<string|int, Message>
Edits a previously-sent webhook message from the same token.
afterConstruct()  : void
Called after the part has been constructed.
attributeCarbonHelper()  : Carbon|null
Helps with getting ISO8601 timestamp attributes.
attributeCollectionHelper()  : ExCollectionInterface
Helps with getting Part attributes.
attributePartHelper()  : Part|null
Helps with getting Part attributes.
checkForGetMutator()  : string|false
Checks if there is a get mutator present.
checkForSetMutator()  : string|false
Checks if there is a set mutator present.
getAttribute()  : mixed
Gets an attribute on the part.
getChannelAttribute()  : Channel|null
Gets the channel the webhook belongs to.
getGuildAttribute()  : Guild|null
Gets the guild the webhook belongs to.
getSourceChannelAttribute()  : Channel|null
Gets the source channel attribute.
getSourceGuildAttribute()  : Guild|null
Gets the source guild attribute.
getUrlAttribute()  : string|null
Gets the webhook url attribute.
getUserAttribute()  : User|null
Gets the user that created the webhook.
makeOptionalAttributes()  : array<string|int, mixed>
Return key-value attributes if it has been filled.
setAttribute()  : void
Sets an attribute on the part.
studly()  : string
Converts a string to studlyCase.

Constants

TYPE_APPLICATION

public mixed TYPE_APPLICATION = 3

TYPE_CHANNEL_FOLLOWER

public mixed TYPE_CHANNEL_FOLLOWER = 2

TYPE_INCOMING

public mixed TYPE_INCOMING = 1

Properties

$application_id

public string|null $application_id

The bot/OAuth2 application that created this webhook.

$avatar

public string|null $avatar

The avatar of the webhook.

$channel read-only

public Channel|null $channel

The channel this webhook is for, if any.

$created

Is the part already created in the Discord servers?

public bool $created = false

Whether the part has been created.

$guild read-only

public Guild|null $guild

The guild this webhook is for, if any.

$id

public string $id

The id of the webhook.

$name

public string|null $name

The name of the webhook.

$scriptData

Custom script data.

Relying on this variable with dynamic caching is discouraged.

public mixed $scriptData

Used for storing custom information, used by end products.

$source_channel

public Channel|null $source_channel

The partial channel that this webhook is following (returned for Channel Follower Webhooks).

$source_guild

public Guild|null $source_guild

The partial guild of the channel that this webhook is following (returned for Channel Follower Webhooks).

$token

public string|null $token

The token of the webhook.

$type

public int $type

The type of webhook.

$url

public string|null $url

The url used for executing the webhook (returned by the webhooks OAuth2 flow).

$user

public User|null $user

The user that created the webhook.

$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 = ['id', 'type', 'guild_id', 'channel_id', 'user', 'name', 'avatar', 'token', 'application_id', 'source_guild', 'source_channel', 'url']

The array of attributes that can be mass-assigned.

Tags
inheritDoc

$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 = ['messages' => \Discord\Repository\Channel\WebhookMessageRepository::class]

Repositories.

Tags
inheritDoc

$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.

__debugInfo()

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

__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.

__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>

execute()

Executes the webhook with an array of data.

public execute(MessageBuilder|array<string|int, mixed> $data[, bool|null $queryparams = [] ]) : PromiseInterface<string|int, Message|void>
Parameters
$data : MessageBuilder|array<string|int, mixed>
$queryparams : bool|null = []

['with_components'] Whether to respect the components field of the request. When enabled, allows application-owned webhooks to use all components and non-owned webhooks to use non-interactive components. (defaults to false)

Tags
link
https://discord.com/developers/docs/resources/webhook#execute-webhook
Return values
PromiseInterface<string|int, Message|void>

Message returned if wait parameter is set true.

executeGitHub()

Executes a GitHub-compatible webhook.

public executeGitHub(mixed $data[, bool|null $queryparams = [] ]) : PromiseInterface

Add a new webhook to your GitHub repo (in the repo's settings), and use this endpoint as the "Payload URL." You can choose what events your Discord channel receives by choosing the "Let me select individual events" option and selecting individual events for the new webhook you're configuring. The supported events are commit_comment, create, delete, fork, issue_comment, issues, member, public, pull_request, pull_request_review, pull_request_review_comment, push, release, watch, check_run, check_suite, discussion, and discussion_comment.

Parameters
$data : mixed
$queryparams : bool|null = []

['wait'] Waits for server confirmation of message send before response (defaults to true; when false a message that is not saved does not return an error)

Tags
since

v10.27.0

Return values
PromiseInterface

executeSlack()

Executes a Slack-compatible webhook.

public executeSlack(mixed $data[, bool|null $queryparams = [] ]) : PromiseInterface

Refer to Slack's documentation for more information. Discord does not support Slack's channel, icon_emoji, mrkdwn, or mrkdwn_in properties.

Parameters
$data : mixed
$queryparams : bool|null = []

['wait'] Waits for server confirmation of message send before response (defaults to true; when false a message that is not saved does not return an error)

Tags
since

v10.27.0

Return values
PromiseInterface

fetch()

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

public fetch() : PromiseInterface<string|int, static>
Tags
throws
RuntimeException

The part is not fetchable.

Return values
PromiseInterface<string|int, static>

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.

Tags
see
self::setAttribute()

getConstants()

Returns an array of constant names and their values.

public getConstants() : array<string|int, mixed>
Tags
since
10.19.0
Return values
array<string|int, mixed>

An associative array where keys are constant names and values are their values.

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()

public getRepositoryAttributes() : array<string|int, mixed>
Tags
inheritDoc
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.

Attributes
#[ReturnTypeWillChange]

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.

offsetUnset()

Unsets an attribute via key. Used for ArrayAccess.

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

The attribute key.

serialize()

Serializes the data. Used for Serializable.

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

A string of serialized data.

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.

updateMessage()

Edits a previously-sent webhook message from the same token.

public updateMessage(string $message_id, MessageBuilder $builder[, array<string|int, mixed> $queryparams = [] ]) : PromiseInterface<string|int, Message>
Parameters
$message_id : string

ID of the message to update.

$builder : MessageBuilder

The new message.

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

Query string params to add to the request.

Tags
link
https://discord.com/developers/docs/resources/webhook#edit-webhook-message
Return values
PromiseInterface<string|int, Message>

afterConstruct()

Called after the part has been constructed.

protected afterConstruct() : void

attributeCarbonHelper()

Helps with getting ISO8601 timestamp attributes.

protected attributeCarbonHelper(string $key) : Carbon|null
Parameters
$key : string

The attribute key.

Tags
throws
Exception
since
10.19.0
Return values
Carbon|null

attributeCollectionHelper()

Helps with getting Part attributes.

protected attributeCollectionHelper(string $key, string $class[, string|null $discrim = 'id' ]) : ExCollectionInterface
Parameters
$key : string

The attribute key.

$class : string

The attribute class.

$discrim : string|null = 'id'

The attribute discriminator.

Tags
throws
Exception
since
10.19.0
Return values
ExCollectionInterface

attributePartHelper()

Helps with getting Part attributes.

protected attributePartHelper(string $key, string $class[, array<string|int, mixed> $extraData = [] ]) : Part|null
Parameters
$key : string

The attribute key.

$class : string

The attribute class.

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

Extra data to pass to the part constructor.

Tags
throws
Exception
since
10.19.0
Return values
Part|null

checkForGetMutator()

Checks if there is a get mutator present.

protected checkForGetMutator(string $key) : string|false
Parameters
$key : string

The attribute name to check.

Tags
since
10.0.0

Replaces checkForMutator($key, 'get')

Return values
string|false

Either a string if it is a method or false.

checkForSetMutator()

Checks if there is a set mutator present.

protected checkForSetMutator(string $key) : string|false
Parameters
$key : string

The attribute name to check.

Tags
since
10.0.0

Replaces checkForMutator($key, 'set')

Return values
string|false

Either a string if it is a method or false.

getAttribute()

Gets an attribute on the part.

protected 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.

getChannelAttribute()

Gets the channel the webhook belongs to.

protected getChannelAttribute() : Channel|null
Return values
Channel|null

getGuildAttribute()

Gets the guild the webhook belongs to.

protected getGuildAttribute() : Guild|null
Return values
Guild|null

getSourceChannelAttribute()

Gets the source channel attribute.

protected getSourceChannelAttribute() : Channel|null
Tags
since
10.23.0
Return values
Channel|null

getSourceGuildAttribute()

Gets the source guild attribute.

protected getSourceGuildAttribute() : Guild|null
Tags
since
10.23.0
Return values
Guild|null

getUrlAttribute()

Gets the webhook url attribute.

protected getUrlAttribute() : string|null
Return values
string|null

getUserAttribute()

Gets the user that created the webhook.

protected getUserAttribute() : User|null
Return values
User|null

makeOptionalAttributes()

Return key-value attributes if it has been filled.

protected makeOptionalAttributes(array<string|int, mixed> $attributes) : array<string|int, mixed>

To be used with fields marked "optional?" from the API.

Parameters
$attributes : array<string|int, mixed>

Names of optional attribute

Return values
array<string|int, mixed>

setAttribute()

Sets an attribute on the part.

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

The key to the attribute.

$value : mixed

The value of the attribute.

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

        
On this page

Search results