diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..c9ac4d5 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,12 @@ +| Q | A +| ---------------- | ----- +| Bug report? | yes/no +| Feature request? | yes/no +| BC Break report? | yes/no +| RFC? | yes/no + + diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..d6cb8ba --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,12 @@ +| Q | A +| ------------- | --- +| Bug fix? | yes/no +| New feature? | yes/no +| BC breaks? | no +| Deprecations? | yes/no +| Fixed tickets | #... + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85462d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.idea/ +/tmp/ +/src/vendor/ +/composer.lock diff --git a/README.md b/README.md new file mode 100644 index 0000000..ba3e62d --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Discord Webhook +Send Discord messages directly from your PHP application. Even with embeds & files! + +## Installation + +**Composer installation** +```bash +composer require labymod/discord-webhook +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a40fcf0 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "labymod/discord-webhook", + "description": "A lightweight library for Discordâ„¢ Webhooks", + "type": "library", + "license": "GPL-3.0", + "authors": [ + { + "name": "LabyMod Scrummer", + "email": "scrummer@gmx.ch", + "homepage": "https://labymod.net", + "role": "Developer" + } + ], + "require": { + "php": ">=7.0", + "guzzlehttp/guzzle": "~6.3.3" + }, + "config": { + "vendor-dir": "src/vendor" + }, + "autoload": { + "psr-4": { + "DiscordWebhook\\": "src/DiscordWebhook/" + } + } +} diff --git a/src/DiscordWebhook/Webhook.php b/src/DiscordWebhook/Webhook.php new file mode 100644 index 0000000..d4f1326 --- /dev/null +++ b/src/DiscordWebhook/Webhook.php @@ -0,0 +1,174 @@ + + * @package DiscordWebhook + */ +class Webhook +{ + /** + * @var Client + */ + private $client; + + /** + * @var string + */ + private $username; + + /** + * @var string + */ + private $avatar; + + /** + * @var string + */ + private $message; + + /** + * @var bool + */ + private $tts; + + /** + * @var array + */ + private $file; + + /** + * Constructor. + * + * @param string $url + */ + public function __construct(string $url) + { + $this->client = new Client([ + 'base_uri' => $url + ]); + } + + /** + * Send the Webhook + * + * @return bool + * @throws GuzzleException + */ + public function send(): bool + { + $response = $this->client->request( + 'POST', + $this->client->getConfig('base_uri')->getPath(), + $this->buildPayload() + ); + + return $response->getStatusCode() === 200; + } + + private function buildPayload(): array + { + $fields = [ + 'username' => 'username', + 'avatar' => 'avatar_url', + 'message' => 'content', + 'tts' => 'tts', + 'file' => 'file', + 'embeds' => 'embeds' + ]; + $payload = [ + 'multipart' => [] + ]; + + foreach ($fields as $field => $payloadField) { + if (!property_exists($this, $field) || null === $this->$field) { + continue; + } + + if (is_string($this->$field) || is_bool($this->$field)) { // add string and booloan values + $payload['multipart'][] = [ + 'name' => $payloadField, + 'contents' => $this->$field + ]; + } elseif ($this->$field instanceof \SplFileInfo) { // add file + /** @var \SplFileInfo $file */ + $file = $this->$field; + + $payload['multipart'][] = [ + 'name' => $payloadField, + 'contents' => $file->openFile()->fread($file->getSize()), + 'filename' => $file->getFilename() + ]; + } + } + + return $payload; + } + + /** + * @param bool $tts + * + * @return Webhook + */ + public function setTts(bool $tts = false): self + { + $this->tts = $tts; + + return $this; + } + + /** + * @param string $username + * + * @return Webhook + */ + public function setUsername(string $username): self + { + $this->username = $username; + + return $this; + } + + /** + * @param string $url + * + * @return Webhook + */ + public function setAvatar(string $url): self + { + $this->avatar = $url; + + return $this; + } + + /** + * @param string $message + * + * @return Webhook + */ + public function setMessage(string $message): self + { + $this->message = $message; + + return $this; + } + + /** + * @param \SplFileInfo $file + * + * @return Webhook + */ + public function setFile(\SplFileInfo $file): self + { + $this->file = $file; + + return $this; + } +}