Skip to content

Easily send/delete/update a message to discord with embeds and/or files/attachments.

Notifications You must be signed in to change notification settings

renzbobz/DiscordWebhook-PHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 

Repository files navigation

DiscordWebhook-PHP

Easily send/delete/update a message to discord with embeds and/or files.

Getting started

Installation

Download the DiscordWebhook.php file and then require it to your project and you're ready to go!

require "DiscordWebhook.php";

Usage

Create new instance and set your desired initial values.

$dw = new DiscordWebhook($webhook);
# or
$dw = new DiscordWebhook($options);
# or
$dw = new DiscordWebhook($webhook, $options);

Options

Name Type Default Description
username string Bot name
avatarUrl string Bot avatar url
webhook string Discord webhook url
wait boolean false Wait for message to be sent, returns a message object on success
threadId snowflake Send the message to that thread
parseJSON boolean true Automatically json parse body
curlOpts array Custom curl options

Send plain message

$msg = $dw
  ->newMessage("Hello")
  ->send();
# same as
$msg = $dw
  ->newMessage()
  ->setContent("Hello")
  ->send();

Create new thread

$msg = $dw
  ->newThread("Thread name")
  ->setContent("Hello thread!")
  ->send();

Send message to thread

$msg = $dw
  ->newMessage()
  ->setContent("Hello thread!")
  ->setThreadId($threadId)
  ->send();

Embed message

$msg = $dw
  ->newMessage()
  ->setContent("Hello world!")
  ->setTitle("Embed title")
  ->setDescription("Embed description")
  ->setRandomColor()
  ->send();

Send files

$msg = $dw
  ->newMessage()
  ->setContent("You can also add content and embeds too!")
  ->setTitle("Embed title")
  ->addFile("fileId", "files/file.txt")
  ->addFile("fileId2", "files/image.jpg", "Custom_name.jpg")
  ->send();

Embed message with attachment files

$msg = $dw
  ->newMessage()
  ->setTitle("Embed title")
  ->setImage("attachment://beautiful_image.jpg")
  ->addFile("fileId1", "files/image.jpg")
  ->addAttachment("fileId1", "beautiful_image.jpg")
  ->send();

Get and create new message instance

$msg = $dw->getMessage($msgId, false); // only the message id is copied
$msg2 = $dw->getMessage($msgId); // whole message is copied except for files
$msg2
  ->setContent("Content updated.")
  ->send();

Update and delete message

# Required option: [ "wait" => true ];
$msg = $dw
  ->newMessage()
  ->setContent("This message will be updated in 5 seconds.");
$sendRes = $msg->send(); // If success, this will automatically set the message id for you, to use update/delete/get method flawlessly (wait option must be enabled to get the message object)
sleep(5);
$updateRes = $msg
  ->setContent("Message updated. This message will be deleted in 5 seconds.")
  ->update();
sleep(5);
$deleteRes = $msg->delete();

More example

Response Object

Name Type Description
success boolean Returns true if response code is in 2xx range
body string | array Response body (auto json parse, if parseJSON option is true)
code int Response code
curl_error string Curl error message

Methods

New message

newMessage(?string $content);

New thread

newThread(string $name);

Set thread Id

setThreadId(snowflake $id);

Set Message Id

setMessageId(snowflake $msgId);

Get and create new message

$copyMsg == true ? Copy the whole message (including embeds,attachments,bot name/avatar,etc., except for files).

$copyMsg == false ? Copy the message id only.

getMessage(snowflake $msgId, ?bool $copyMsg = true);

Get message

get(?snowflake $msgId): ResponseObject;

Send message

If response is success and (wait option is enabled or message object is detected), this will automatically set the message id.

send(?string $webhook): ResponseObject;
send(?array $options): ResponseObject;

Update message

update(?snowflake $msgId): ResponseObject;

Delete message

delete(?snowflake $msgId): ResponseObject;

Set Bot username

setUsername(string $username);

Set Bot avatar

setAvatarUrl(string $avatarUrl);

Set Webhook

setWebhook(string $webhook);

Set Is wait message

waitMessage(?bool $wait = true);

Set Message Content

setContent(string $content);
prependContent(string $content);
appendContent(string $content);

Set Message Text-to-speech

setTts(?bool $tts = true);

Set Multiple Embed

addEmbed(object $embed, ?int $index = null, ?bool $replace = false);

Set Files

# Associative array
$file = [
  "id" => string,
  "path" => string,
  "name" => ?string,
  "type" => ?string,
];
# Indexed array
$file = [
  string $id,
  string $path,
  ?string $name,
  ?string $type
];
addFile(associative array $file);
addFile(string $id, ?string $path, ?string $name, ?string $type);
addFiles(associative|indexed array ...$files);

Set Attachments

$attachmentObject - Attachment object

# Associative array
$attachment = [
  "id" => string,
  "filename" => string,
  ...$attachmentObject,
];
addAttachment(associative array $attachment);
addAttachment(string $id, ?string $filename);
addAttachments(associative array ...$attachments);

Set Flag

$flag - Message flags

setFlag(int $flag);

Set Allowed mentions

$allowedMentionsObject - Allowed mentions object

setAllowedMentions(array $allowedMentionsObject);

Formatting

toJSON(): string;

Embed Methods

Title

setTitle(string $title, ?string $url);
prependTitle(string $title);
appendTitle(string $title);

Title Url

setUrl(string $url);

Description

setDescription(string $description);
prependDescription(string $description);
appendDescription(string $description);

Color

$color can be a hex, decimal, or rgb (comma separated).

setColor(int $color);
setColor(string $color);
setRandomColor();

Timestamp

setTimestamp(?string $ts = date('c'));

Author

# Associative array
$author = [
  "name" => string,
  "url" => ?string,
  "icon_url" => ?string,
  "proxy_icon_url" => ?string,
];
setAuthor(string $name, ?string $url, ?string $iconUrl, ?string $proxyIconUrl);
setAuthor(associative array $author);

Footer

# Associative array
$footer = [
  "text" => string,
  "icon_url" => string,
  "proxy_icon_url" => ?string,
];
setFooter(string $text, ?string $iconUrl, ?string $proxyIconUrl);
setFooter(associative array $footer);

Image

# Associative array
$image = [
  "url" => string,
  "proxy_url" => ?string,
  "height" => ?int,
  "width" => ?int,
];
setImage(string $url, ?string $proxyUrl, ?int $height, ?int $width);
setImage(associative array $image);

Thumbnail

$thumbnail = [
  # Associative array
  "url" => string,
  "proxy_url" => ?string,
  "height" => ?int,
  "width" => ?int,
];
setThumbnail(string $url, ?string $proxyUrl, ?int $height, ?int $width);
setThumbnail(associative array $thumbnail);

Add Field

# Associative array
$field = [
  "name" => string,
  "value" => ?string,
  "inline" => ?bool,
];
# Indexed array
$field = [
  string $name,
  ?string $value,
  ?bool $inline,
];
addField(associative array $field);
addField(string $name, ?string $value, ?bool $inline);
addFields(associative|indexed array ...$fields);

About

Easily send/delete/update a message to discord with embeds and/or files/attachments.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages