Easily send/delete/update a message to discord with embeds and/or files.
Download the DiscordWebhook.php file and then require it to your project and you're ready to go!
require "DiscordWebhook.php";
Create new instance and set your desired initial values.
$dw = new DiscordWebhook($webhook);
# or
$dw = new DiscordWebhook($options);
# or
$dw = new DiscordWebhook($webhook, $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 |
$msg = $dw
->newMessage("Hello")
->send();
# same as
$msg = $dw
->newMessage()
->setContent("Hello")
->send();
$msg = $dw
->newThread("Thread name")
->setContent("Hello thread!")
->send();
$msg = $dw
->newMessage()
->setContent("Hello thread!")
->setThreadId($threadId)
->send();
$msg = $dw
->newMessage()
->setContent("Hello world!")
->setTitle("Embed title")
->setDescription("Embed description")
->setRandomColor()
->send();
$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();
$msg = $dw
->newMessage()
->setTitle("Embed title")
->setImage("attachment://beautiful_image.jpg")
->addFile("fileId1", "files/image.jpg")
->addAttachment("fileId1", "beautiful_image.jpg")
->send();
$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();
# 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();
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 |
newMessage(?string $content);
newThread(string $name);
setThreadId(snowflake $id);
setMessageId(snowflake $msgId);
$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(?snowflake $msgId): ResponseObject;
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(?snowflake $msgId): ResponseObject;
delete(?snowflake $msgId): ResponseObject;
setUsername(string $username);
setAvatarUrl(string $avatarUrl);
setWebhook(string $webhook);
waitMessage(?bool $wait = true);
setContent(string $content);
prependContent(string $content);
appendContent(string $content);
setTts(?bool $tts = true);
addEmbed(object $embed, ?int $index = null, ?bool $replace = false);
# 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);
$attachmentObject
- Attachment object
# Associative array
$attachment = [
"id" => string,
"filename" => string,
...$attachmentObject,
];
addAttachment(associative array $attachment);
addAttachment(string $id, ?string $filename);
addAttachments(associative array ...$attachments);
$flag
- Message flags
setFlag(int $flag);
$allowedMentionsObject
- Allowed mentions object
setAllowedMentions(array $allowedMentionsObject);
toJSON(): string;
setTitle(string $title, ?string $url);
prependTitle(string $title);
appendTitle(string $title);
setUrl(string $url);
setDescription(string $description);
prependDescription(string $description);
appendDescription(string $description);
$color
can be a hex, decimal, or rgb (comma separated).
setColor(int $color);
setColor(string $color);
setRandomColor();
setTimestamp(?string $ts = date('c'));
# 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);
# Associative array
$footer = [
"text" => string,
"icon_url" => string,
"proxy_icon_url" => ?string,
];
setFooter(string $text, ?string $iconUrl, ?string $proxyIconUrl);
setFooter(associative array $footer);
# 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 = [
# Associative array
"url" => string,
"proxy_url" => ?string,
"height" => ?int,
"width" => ?int,
];
setThumbnail(string $url, ?string $proxyUrl, ?int $height, ?int $width);
setThumbnail(associative array $thumbnail);
# 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);