DiscordPHP Documentation

MessageBuilder
in package
implements JsonSerializable

Helper class used to build messages.

Tags
since
7.0.0
author

David Cole david.cole1340@gmail.com

Interfaces, Classes, Traits and Enums

JsonSerializable

Table of Contents

$allowed_mentions  : array<string|int, mixed>|null
Allowed mentions object for the message.
$attachments  : array<string|int, Attachment>|null
Attachments to send with this message.
$avatar_url  : string|null
Override the default avatar of the webhook.
$components  : array<string|int, Component>|null
Components to send with this message.
$content  : string|null
Content of the message.
$embeds  : array<string|int, array<string|int, mixed>>|null
Array of embeds to send with the message.
$files  : array<string|int, array<string|int, mixed>>|null
Files to send with this message.
$flags  : int|null
Flags to send with this message.
$nonce  : int|string|null
A nonce that can be used for message roundtrips with the gateway (up to 25 characters).
$replyTo  : Message|null
Message to reply to with this message.
$sticker_ids  : array<string|int, string>
IDs of up to 3 stickers in the server to send in the message.
$tts  : bool
Whether the message is text-to-speech.
$username  : string|null
Override the default username of the webhook.
addAttachment()  : $this
Adds attachment(s) to the builder.
addComponent()  : $this
Adds a component to the builder.
addEmbed()  : $this
Adds an embed to the builder.
addFile()  : $this
Adds a file attachment to the builder.
addFileFromContent()  : $this
Adds a file attachment to the builder with a given filename and content.
addSticker()  : $this
Adds a sticker to the builder. Only used for sending message or creating forum thread.
clearAttachments()  : $this
Removes all attachments from the message.
clearFiles()  : $this
Removes all files from the builder.
getAttachments()  : array<string|int, Attachment>
Returns all the attachments in the builder.
getComponents()  : array<string|int, Component>
Returns all the components in the builder.
getStickers()  : array<string|int, string>
Returns all the sticker ids in the builder.
getTts()  : bool
Returns the value of TTS of the builder.
jsonSerialize()  : array<string|int, mixed>
{@inheritDoc}
new()  : static
Creates a new message builder.
numFiles()  : int
Returns the number of files attached to the builder.
removeComponent()  : $this
Removes a component from the builder.
removeSticker()  : $this
Removes a sticker from the builder.
requiresMultipart()  : bool
Returns a boolean that determines whether the message needs to be sent via multipart request, i.e. contains files.
setAllowedMentions()  : $this
Sets the allowed mentions object of the message.
setAvatarUrl()  : $this
Override the default avatar URL of the webhook. Only used for executing webhook.
setComponents()  : $this
Sets the components of the message. Removes the existing components in the process.
setContent()  : $this
Sets the content of the message.
setEmbeds()  : $this
Sets the embeds for the message. Clears the existing embeds in the process.
setNonce()  : $this
Sets the nonce of the message. Only used for sending message.
setReplyTo()  : $this
Sets this message as a reply to another message. Only used for sending message.
setStickers()  : $this
Sets the stickers of the builder. Removes the existing stickers in the process.
setTts()  : $this
Sets the TTS status of the message. Only used for sending message or executing webhook.
setUsername()  : $this
Override the default username of the webhook. Only used for executing webhook.

Properties

$allowed_mentions

Allowed mentions object for the message.

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

$avatar_url

Override the default avatar of the webhook.

private string|null $avatar_url

$embeds

Array of embeds to send with the message.

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

$files

Files to send with this message.

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

$flags

Flags to send with this message.

private int|null $flags

$nonce

A nonce that can be used for message roundtrips with the gateway (up to 25 characters).

private int|string|null $nonce

$sticker_ids

IDs of up to 3 stickers in the server to send in the message.

private array<string|int, string> $sticker_ids = []

$tts

Whether the message is text-to-speech.

private bool $tts = false

$username

Override the default username of the webhook.

private string|null $username

Methods

addAttachment()

Adds attachment(s) to the builder.

public addAttachment(mixed ...$attachments) : $this
Parameters
$attachments : mixed
Return values
$this

addComponent()

Adds a component to the builder.

public addComponent(Component $component) : $this
Parameters
$component : Component

Component to add.

Tags
throws
InvalidArgumentException

Component is not a type of ActionRow or SelectMenu

throws
OverflowException

Builder exceeds 5 components.

Return values
$this

addEmbed()

Adds an embed to the builder.

public addEmbed(mixed ...$embeds) : $this
Parameters
$embeds : mixed
Tags
throws
OverflowException

Builder exceeds 10 embeds.

Return values
$this

addFile()

Adds a file attachment to the builder.

public addFile(string $filepath[, string|null $filename = null ]) : $this

Note this is a synchronous function which uses file_get_contents and therefore should not be used when requesting files from an online resource. Fetch the content asynchronously and use the addFileFromContent function for tasks like these.

Parameters
$filepath : string

Path to the file to send.

$filename : string|null = null

Name to send the file as. null for the base name of $filepath.

Return values
$this

addFileFromContent()

Adds a file attachment to the builder with a given filename and content.

public addFileFromContent(string $filename, string $content) : $this
Parameters
$filename : string

Name to send the file as.

$content : string

Content of the file.

Return values
$this

addSticker()

Adds a sticker to the builder. Only used for sending message or creating forum thread.

public addSticker(string|Sticker $sticker) : $this
Parameters
$sticker : string|Sticker

Sticker to add.

Tags
throws
OverflowException

Builder exceeds 3 stickers.

Return values
$this

clearAttachments()

Removes all attachments from the message.

public clearAttachments() : $this
Return values
$this

clearFiles()

Removes all files from the builder.

public clearFiles() : $this
Return values
$this

getAttachments()

Returns all the attachments in the builder.

public getAttachments() : array<string|int, Attachment>
Return values
array<string|int, Attachment>

getComponents()

Returns all the components in the builder.

public getComponents() : array<string|int, Component>
Return values
array<string|int, Component>

getStickers()

Returns all the sticker ids in the builder.

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

getTts()

Returns the value of TTS of the builder.

public getTts() : bool
Return values
bool

jsonSerialize()

{@inheritDoc}

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

new()

Creates a new message builder.

public static new() : static
Return values
static

numFiles()

Returns the number of files attached to the builder.

public numFiles() : int
Return values
int

removeComponent()

Removes a component from the builder.

public removeComponent(Component $component) : $this
Parameters
$component : Component

Component to remove.

Return values
$this

removeSticker()

Removes a sticker from the builder.

public removeSticker(string|Sticker $sticker) : $this
Parameters
$sticker : string|Sticker

Sticker to remove.

Return values
$this

requiresMultipart()

Returns a boolean that determines whether the message needs to be sent via multipart request, i.e. contains files.

public requiresMultipart() : bool
Return values
bool

setAvatarUrl()

Override the default avatar URL of the webhook. Only used for executing webhook.

public setAvatarUrl(string $avatar_url) : $this
Parameters
$avatar_url : string

New webhook avatar URL.

Return values
$this

setComponents()

Sets the components of the message. Removes the existing components in the process.

public setComponents(array<string|int, mixed> $components) : $this
Parameters
$components : array<string|int, mixed>

New message components.

Return values
$this

setContent()

Sets the content of the message.

public setContent(string $content) : $this
Parameters
$content : string

Content of the message. Maximum 2000 characters.

Tags
throws
LengthException
Return values
$this

setEmbeds()

Sets the embeds for the message. Clears the existing embeds in the process.

public setEmbeds(array<string|int, Embed>|array<string|int, mixed> $embeds) : $this
Parameters
$embeds : array<string|int, Embed>|array<string|int, mixed>
Return values
$this

setNonce()

Sets the nonce of the message. Only used for sending message.

public setNonce([int|string|null $nonce = null ]) : $this
Parameters
$nonce : int|string|null = null

Nonce of the message.

Tags
throws
LengthException

$nonce string exceeds 25 characters.

Return values
$this

setReplyTo()

Sets this message as a reply to another message. Only used for sending message.

public setReplyTo([Message|null $message = null ]) : $this
Parameters
$message : Message|null = null
Return values
$this

setStickers()

Sets the stickers of the builder. Removes the existing stickers in the process.

public setStickers(array<string|int, mixed> $stickers) : $this
Parameters
$stickers : array<string|int, mixed>

New sticker ids.

Return values
$this

setTts()

Sets the TTS status of the message. Only used for sending message or executing webhook.

public setTts([bool $tts = false ]) : $this
Parameters
$tts : bool = false
Return values
$this

setUsername()

Override the default username of the webhook. Only used for executing webhook.

public setUsername(string $username) : $this
Parameters
$username : string

New webhook username.

Tags
throws
LengthException

$username exceeds 80 characters.

Return values
$this

Search results