From 6d88e58565332d3cfb1d6acbd066cb9b86ff4f4a Mon Sep 17 00:00:00 2001 From: m7rcin Date: Mon, 23 Sep 2019 15:06:03 +0200 Subject: [PATCH] Update for PHP7; Add Embed support Updated for PHP v7 support. Added Discord Embed for PHP v7 and v7.3. --- lib/DiscordEmbed-v7.0.php | 178 +++++++++++++++++++++++++++ lib/DiscordEmbed.php | 178 +++++++++++++++++++++++++++ lib/DiscordWebhook-v7.0.php | 234 +++++++++++++++++++++++++++++++----- lib/DiscordWebhook.php | 155 +++++++++++++++++++----- 4 files changed, 684 insertions(+), 61 deletions(-) create mode 100644 lib/DiscordEmbed-v7.0.php create mode 100644 lib/DiscordEmbed.php diff --git a/lib/DiscordEmbed-v7.0.php b/lib/DiscordEmbed-v7.0.php new file mode 100644 index 0000000..85edf31 --- /dev/null +++ b/lib/DiscordEmbed-v7.0.php @@ -0,0 +1,178 @@ +title = $title; + $this->url = $url; + + return $this; + } + + /** + * Set description + * + * @param string $description + * @return self + */ + function setDescription($description) { + $this->description = $description; + + return $this; + } + /** + * Set timestamp of the embed + * + * @param [type] $timestamp + * @return self + */ + function setTimestamp($timestamp) { + $this->timestamp = $timestamp; + + return $this; + } + /** + * Set border color + * + * @param string $color + * @return self + */ + function setColor($color) { + $this->color = is_int($color) ? $color : hexdec($color); + + return $this; + } + /** + * Set url + * + * @param string $url + * @return self + */ + function setUrl($url) { + $this->url = $url; + + return $this; + } + /** + * Set footer + * + * @param string $text + * @param string $icon_url + * @return self + */ + function setFooter($text, $icon_url = "") { + $this->footer = [ + "text" => $text, + "icon_url" => $icon_url, + ]; + + return $this; + } + /** + * Set image + * + * @param string $url + * @return self + */ + function setImage($url) { + $this->image = [ + "url" => $url, + ]; + + return $this; + } + /** + * Set thumbnail + * + * @param string $url + * @return self + */ + function setThumbnail($url) { + $this->thumbnail = [ + "url" => $url, + ]; + + return $this; + } + /** + * Set author + * + * @param string $name + * @param string $url + * @param string $icon_url + * @return self + */ + function setAuthor($name, $url = "", $icon_url = "") { + $this->author = [ + "name" => $name, + "url" => $url, + "icon_url" => $icon_url, + ]; + + return $this; + } + /** + * Set field + * + * @param string $name + * @param string $value + * @param boolean $inline + * @return self + */ + function setField($name, $value = "", $inline = false) { + $this->fields[] = [ + 'name' => $name, + 'value' => $value, + 'inline' => boolval($inline), + ]; + + return $this; + } + /** + * Get fields as an array + * + * @return array + */ + function toArray() { + return [ + 'title' => $this->title, + 'type' => $this->type, + 'description' => $this->description, + 'url' => $this->url, + 'color' => $this->color, + 'footer' => $this->footer, + 'image' => $this->image, + 'thumbnail' => $this->thumbnail, + 'timestamp' => $this->timestamp, + 'author' => $this->author, + 'fields' => $this->fields + ]; + } + + +} + ?> \ No newline at end of file diff --git a/lib/DiscordEmbed.php b/lib/DiscordEmbed.php new file mode 100644 index 0000000..84c1ecf --- /dev/null +++ b/lib/DiscordEmbed.php @@ -0,0 +1,178 @@ +title = $title; + $this->url = $url; + + return $this; + } + + /** + * Set description + * + * @param string $description + * @return self + */ + function setDescription(string $description): self { + $this->description = $description; + + return $this; + } + /** + * Set timestamp of the embed + * + * @param [type] $timestamp + * @return self + */ + function setTimestamp($timestamp): self { + $this->timestamp = $timestamp; + + return $this; + } + /** + * Set border color + * + * @param string $color + * @return self + */ + function setColor(string $color): self { + $this->color = is_int($color) ? $color : hexdec($color); + + return $this; + } + /** + * Set url + * + * @param string $url + * @return self + */ + function setUrl(string $url): self { + $this->url = $url; + + return $this; + } + /** + * Set footer + * + * @param string $text + * @param string $icon_url + * @return self + */ + function setFooter(string $text, string $icon_url = ""): self { + $this->footer = [ + "text" => $text, + "icon_url" => $icon_url, + ]; + + return $this; + } + /** + * Set image + * + * @param string $url + * @return self + */ + function setImage(string $url): self { + $this->image = [ + "url" => $url, + ]; + + return $this; + } + /** + * Set thumbnail + * + * @param string $url + * @return self + */ + function setThumbnail(string $url): self { + $this->thumbnail = [ + "url" => $url, + ]; + + return $this; + } + /** + * Set author + * + * @param string $name + * @param string $url + * @param string $icon_url + * @return self + */ + function setAuthor(string $name, string $url = "", string $icon_url = ""): self { + $this->author = [ + "name" => $name, + "url" => $url, + "icon_url" => $icon_url, + ]; + + return $this; + } + /** + * Set field + * + * @param string $name + * @param string $value + * @param boolean $inline + * @return self + */ + function setField(string $name, string $value = "", bool $inline = false): self { + $this->fields[] = [ + 'name' => $name, + 'value' => $value, + 'inline' => boolval($inline), + ]; + + return $this; + } + /** + * Get fields as an array + * + * @return array + */ + function toArray(): array { + return [ + 'title' => $this->title, + 'type' => $this->type, + 'description' => $this->description, + 'url' => $this->url, + 'color' => $this->color, + 'footer' => $this->footer, + 'image' => $this->image, + 'thumbnail' => $this->thumbnail, + 'timestamp' => $this->timestamp, + 'author' => $this->author, + 'fields' => $this->fields + ]; + } + + +} + ?> \ No newline at end of file diff --git a/lib/DiscordWebhook-v7.0.php b/lib/DiscordWebhook-v7.0.php index 5e4f8e2..0c2e958 100644 --- a/lib/DiscordWebhook-v7.0.php +++ b/lib/DiscordWebhook-v7.0.php @@ -1,52 +1,52 @@ msg = $msg; - $this->url = $url ?? - 'https://discordapp.com/api/webhooks/536576089582075936/QkY4y6xDDlrPXXtnSXLiAOnIo961w6BXl1SLTE0bj-t0--A-P3thhos5tskW_lIhiRfG'; - $this->username = $username ?? 'www.magictm.com'; - $this->avatar = $avatar ?? - 'https://i.ytimg.com/vi/JrQkgLLL9XQ/hqdefault.jpg'; - $this->tts = $tts ?? false; - } - - public function tts( - bool $tts - ) - { - $this->tts = $tts; + $this->url = $url; } + /** + * Send Discord Webhook to the Discord server + * + * @return self + */ public function send() { + if (!$this->validate()) { + $errors = ""; + foreach ($this->errors as $key => $value) { + $errors .= "$value"; + } + throw new Exception("Zanim wyślesz wiadomość na Discorda popraw błędy: ". $errors); + } + $curl = curl_init(); //timeouts - 5 seconds curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 5 seconds @@ -58,10 +58,12 @@ public function send() curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([ - 'content' => $this->msg, + 'content' => $this->message, 'username' => $this->username, 'avatar_url' => $this->avatar, - 'tts' => $this->tts + 'tts' => $this->tts, + 'file' => $this->file, + 'embeds' => $this->embeds ])); $output = json_decode( curl_exec($curl), @@ -71,12 +73,180 @@ public function send() if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 204) { curl_close($curl); $message = ""; - foreach ($output as $key => $value) { - $message .= ucfirst($key) . " - " .$value[0]; + + if (!isset($output["message"])) { + foreach ($output as $key => $value) { + $message .= ucfirst($key) . " - " .$value[0]; + } + } else { + $message = $output["message"]; } + throw new Exception("Wystąpił błąd podczas przesyłania wiadomości na Discorda: " . $message); } curl_close($curl); + + return $this; + } + + /** + * Set message to send + * + * @param [type] $message + * @return self + */ + function setMessage($message) { + $this->message = $message; + return $this; + } + /** + * Get message + * + * @return string + */ + function getMessage() { + return $this->message; + } + /** + * Set URL + * + * @param string $url + * @return self + */ + function setURL($url) { + $this->url = $url; + return $this; + } + /** + * Get URL + * + * @return string + */ + function getURL() { + return $this->url; + } + /** + * Set username + * + * @param string $username + * @return self + */ + function setUsername($username) { + $this->username = $username; + return $this; + } + /** + * Get username + * + * @return string + */ + function getUsername() { + return $this->username; + } + /** + * Set avatar + * + * @param string $avatar + * @return self + */ + function setAvatar($avatar) { + $this->avatar = $avatar; + return $this; + } + /** + * Get avatar + * + * @return string + */ + function getAvatar() { + return $this->avatar; + } + /** + * Set + * + * @param object $file + * @return self + */ + function setFile($file) { + $this->file = curl_file_create($file->getFile(), null, $file->getFileName()); + return $this; + } + + /** + * Get File that should be send to Discord + * + * @return void + */ + function getFile() { + return $this->file; + } + + /** + * Set TTS + * + * @param [type] $tts + * @return void + */ + function setTTS($tts) { + $this->tts = $tts; + return $this; + } + + /** + * Get TTS + * + * @return void + */ + function getTTS() { + return $this->tts; + } + + /** + * Reset all variables to its default values. + * + * @return self + */ + function reset() { + return $this; + } + + /** + * Set embed + * + * @param [type] $embed + * @return void + */ + function setEmbed($embed) { + $this->embeds[] = $embed->toArray(); + + return $this; + } + + /** + * Validate inputs before sending the webhook. + * + * @return void + */ + private function validate() { + + if (!$this->url || $this->url === "") { + $this->errors["url"] = "URL is empty."; + } + + if (count($this->embeds) == 0) { + if (!$this->message || $this->message === "") { + $this->errors["message"] = "Message is empty."; + } else if (strlen($this->message) >= 2000) { + $this->errors["message"] = "Message is too long (max. 2000 characters)."; + } + } else if ($this->message && strlen($this->message) >= 2000) { + $this->errors["message"] = "Message is too long (max. 2000 characters)."; + } + + if (empty($this->errors)) { + return true; + } + return false; } } diff --git a/lib/DiscordWebhook.php b/lib/DiscordWebhook.php index 7971cbb..a3dbaa4 100644 --- a/lib/DiscordWebhook.php +++ b/lib/DiscordWebhook.php @@ -1,21 +1,23 @@ url = $url; } - public function send(): ?self + /** + * Send Discord Webhook to the Discord server + * + * @return self + */ + public function send(): self { if (!$this->validate()) { @@ -86,75 +95,163 @@ public function send(): ?self } - - function setMessage($message): ?self { + /** + * Set message to send + * + * @param [type] $message + * @return self + */ + function setMessage($message): self { $this->message = $message; return $this; } - - function getMessage() { + /** + * Get message + * + * @return string + */ + function getMessage(): string { return $this->message; } - - function setURL(?string $url): ?self { + /** + * Set URL + * + * @param string $url + * @return self + */ + function setURL(string $url): self { $this->url = $url; return $this; } - - function getURL(): ?string { + /** + * Get URL + * + * @return string + */ + function getURL(): string { return $this->url; } - - function setUsername(?string $username): ?self { + /** + * Set username + * + * @param string $username + * @return self + */ + function setUsername(string $username): self { $this->username = $username; return $this; } - - function getUsername(): ?string { + /** + * Get username + * + * @return string + */ + function getUsername(): string { return $this->username; } - - function setAvatar(?string $avatar): ?self { + /** + * Set avatar + * + * @param string $avatar + * @return self + */ + function setAvatar(string $avatar): self { $this->avatar = $avatar; return $this; } - - function getAvatar(): ?string { + /** + * Get avatar + * + * @return string + */ + function getAvatar(): string { return $this->avatar; } - function setFile(?object $file): ?self { + /** + * Set + * + * @param object $file + * @return self + */ + function setFile(object $file): self { $this->file = curl_file_create($file->getFile(), null, $file->getFileName()); return $this; } - function getFile(): ?string { + /** + * Get File that should be send to Discord + * + * @return object + */ + function getFile(): object { return $this->file; } - function setTts(?bool $tts): ?self { + /** + * Set TTS + * + * @param [type] $tts + * @return void + */ + function setTTS(bool $tts): self { $this->tts = $tts; return $this; } - function getTts(): ?bool { + /** + * Get TTS + * + * @return void + */ + function getTTS(): bool { return $this->tts; } - function reset(): ?self { + /** + * Reset all variables to its default values. + * + * @return self + */ + function reset(): self { return $this; } + /** + * Set embed + * + * @param DiscordEmbed $embed + * @return self + */ + function setEmbed(DiscordEmbed $embed): self { + $this->embeds[] = $embed->toArray(); + + return $this; + } + + /** + * Validate inputs before sending the webhook. + * + * @return boolean + */ private function validate(): bool { if (!$this->url || $this->url === "") { $this->errors["url"] = "URL is empty."; } - if (!$this->message || $this->message === "") { - $this->errors["message"] = "Message is empty."; - } else if (strlen($this->message) >= 2000) { + + if (count($this->embeds) == 0) { + if (!$this->message || $this->message === "") { + $this->errors["message"] = "Message is empty."; + } else if (strlen($this->message) >= 2000) { + $this->errors["message"] = "Message is too long (max. 2000 characters)."; + } + } else if ($this->message && strlen($this->message) >= 2000) { $this->errors["message"] = "Message is too long (max. 2000 characters)."; } + + + if (empty($this->errors)) { return true; }