Skip to content

Commit

Permalink
fix for issue #68: send embed and components only if filled (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
deto1986 authored Jan 13, 2024
1 parent a4ef4fc commit 11ab1bf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
18 changes: 13 additions & 5 deletions src/DiscordChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 6 additions & 3 deletions src/DiscordMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -78,7 +81,7 @@ public function embed($embed)
/**
* Set the components object.
*
* @param $components
* @param array $components
*
* @return $this
*/
Expand Down
27 changes: 16 additions & 11 deletions tests/DiscordChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
]
]
Expand Down Expand Up @@ -84,7 +88,8 @@ public function toDiscord()
->embed([
'title' => 'Object Title',
'url' => 'https://discord.com',
])->components([
])
->components([
[
"type" => 1,
"components" => [
Expand Down

0 comments on commit 11ab1bf

Please sign in to comment.