diff --git a/src/Message/AbstractDiscordMessage.php b/src/Message/AbstractDiscordMessage.php new file mode 100644 index 0000000..31a7cd9 --- /dev/null +++ b/src/Message/AbstractDiscordMessage.php @@ -0,0 +1,12 @@ + $this->username, 'content' => $this->content, 'avatar_url' => $this->avatar, @@ -124,8 +124,6 @@ public function toArray(): array ], ]], ]; - - return $data; } public function getContent(): ?string @@ -374,7 +372,7 @@ public function setColorWithHexValue(string $hexValue): self return $this; } - public function formatForDiscord(): array + public function jsonSerialize() { return $this->toArray(); } diff --git a/src/Message/DiscordMessageInterface.php b/src/Message/DiscordMessageInterface.php deleted file mode 100644 index ccbdb59..0000000 --- a/src/Message/DiscordMessageInterface.php +++ /dev/null @@ -1,14 +0,0 @@ -toArray(); - } - public function toArray(): array { return [ @@ -89,8 +84,8 @@ public function toArray(): array ]; } - public function toJson(): string + public function jsonSerialize() { - return json_encode($this->toArray()); + return $this->toArray(); } } diff --git a/src/Webhook/DiscordWebhook.php b/src/Webhook/DiscordWebhook.php index 406f69a..036a5db 100644 --- a/src/Webhook/DiscordWebhook.php +++ b/src/Webhook/DiscordWebhook.php @@ -5,7 +5,7 @@ namespace Woeler\DiscordPhp\Webhook; use Woeler\DiscordPhp\Exception\DiscordInvalidResponseException; -use Woeler\DiscordPhp\Message\DiscordMessageInterface; +use Woeler\DiscordPhp\Message\AbstractDiscordMessage; class DiscordWebhook { @@ -17,7 +17,7 @@ class DiscordWebhook /** * @var string */ - protected $webhookUrl; + private $webhookUrl; public function __construct(string $webhookUrl) { @@ -27,20 +27,29 @@ public function __construct(string $webhookUrl) /** * @throws DiscordInvalidResponseException */ - public function send(DiscordMessageInterface $message): int + public function send(AbstractDiscordMessage $message): int { - $ch = curl_init($this->webhookUrl); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); - curl_setopt($ch, CURLOPT_POSTFIELDS, $message->toJson()); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, ['content-type: application/json']); - curl_exec($ch); - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - - if ($code < 200 || $code >= 400) { - throw new DiscordInvalidResponseException('Discord Webhook returned invalid response: '.$code.'.', $code); + $sent = false; + + while (!$sent) { + $ch = curl_init($this->webhookUrl); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, ['content-type: application/json']); + $response = curl_exec($ch); + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if (429 === $code) { + $response = json_decode($response, false); + usleep($response->retry_after * 1000); + } else { + $sent = true; + if ($code < 200 || $code >= 400) { + throw new DiscordInvalidResponseException('Discord Webhook returned invalid response: '.$code.'.', $code); + } + } } return $code;