diff --git a/src/DiscordChannel.php b/src/DiscordChannel.php index 5692f49..7364192 100644 --- a/src/DiscordChannel.php +++ b/src/DiscordChannel.php @@ -37,10 +37,18 @@ public function send($notifiable, Notification $notification) $message = $notification->toDiscord($notifiable); - return $this->discord->send($channel, [ - 'content' => $message->body, - 'embed' => $message->embed, - 'components' => $message->components - ]); + $data = [ + 'content' => $message->body + ]; + + if (count($message->embed) > 0) { + $data['embeds'] = [$message->embed]; + } + + if (count($message->components) > 0) { + $data['components'] = $message->components; + } + + return $this->discord->send($channel, $data); } } diff --git a/src/DiscordMessage.php b/src/DiscordMessage.php index 9cde221..1985993 100644 --- a/src/DiscordMessage.php +++ b/src/DiscordMessage.php @@ -62,9 +62,12 @@ public function body($body) } /** - * Set the embedded object. + * Set a single embedded object. + * + * TODO: Refactor to enable multiple embeds. + * See https://discord.com/developers/docs/resources/channel#create-message * - * @param $embed + * @param array $embed * * @return $this */ @@ -78,7 +81,7 @@ public function embed($embed) /** * Set the components object. * - * @param $components + * @param array $components * * @return $this */ diff --git a/tests/DiscordChannelTest.php b/tests/DiscordChannelTest.php index 565fcea..58dfc15 100644 --- a/tests/DiscordChannelTest.php +++ b/tests/DiscordChannelTest.php @@ -22,18 +22,22 @@ public function it_can_send_a_notification() 'Authorization' => 'Bot super-secret', ], 'json' => [ - 'content' => 'Hello, Discord!', 'embed' => [ - 'title' => 'Object Title', - 'url' => 'https://discord.com', - ], "components" => [ + 'content' => 'Hello, Discord!', + 'embeds' => [ + 0 => [ + 'title' => 'Object Title', + 'url' => 'https://discord.com', + ] + ], + 'components' => [ [ - "type" => 1, - "components" => [ + 'type' => 1, + 'components' => [ [ - "type" => 2, - "label" => "Test", - "style" => 1, - "custom_id" => "primary" + 'type' => 2, + 'label' => 'Test', + 'style' => 1, + 'custom_id' => 'primary' ] ] ] @@ -84,7 +88,8 @@ public function toDiscord() ->embed([ 'title' => 'Object Title', 'url' => 'https://discord.com', - ])->components([ + ]) + ->components([ [ "type" => 1, "components" => [