Skip to content

Commit 11ab1bf

Browse files
authored
fix for issue #68: send embed and components only if filled (#69)
1 parent a4ef4fc commit 11ab1bf

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

src/DiscordChannel.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,18 @@ public function send($notifiable, Notification $notification)
3737

3838
$message = $notification->toDiscord($notifiable);
3939

40-
return $this->discord->send($channel, [
41-
'content' => $message->body,
42-
'embed' => $message->embed,
43-
'components' => $message->components
44-
]);
40+
$data = [
41+
'content' => $message->body
42+
];
43+
44+
if (count($message->embed) > 0) {
45+
$data['embeds'] = [$message->embed];
46+
}
47+
48+
if (count($message->components) > 0) {
49+
$data['components'] = $message->components;
50+
}
51+
52+
return $this->discord->send($channel, $data);
4553
}
4654
}

src/DiscordMessage.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ public function body($body)
6262
}
6363

6464
/**
65-
* Set the embedded object.
65+
* Set a single embedded object.
66+
*
67+
* TODO: Refactor to enable multiple embeds.
68+
* See https://discord.com/developers/docs/resources/channel#create-message
6669
*
67-
* @param $embed
70+
* @param array $embed
6871
*
6972
* @return $this
7073
*/
@@ -78,7 +81,7 @@ public function embed($embed)
7881
/**
7982
* Set the components object.
8083
*
81-
* @param $components
84+
* @param array $components
8285
*
8386
* @return $this
8487
*/

tests/DiscordChannelTest.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,22 @@ public function it_can_send_a_notification()
2222
'Authorization' => 'Bot super-secret',
2323
],
2424
'json' => [
25-
'content' => 'Hello, Discord!', 'embed' => [
26-
'title' => 'Object Title',
27-
'url' => 'https://discord.com',
28-
], "components" => [
25+
'content' => 'Hello, Discord!',
26+
'embeds' => [
27+
0 => [
28+
'title' => 'Object Title',
29+
'url' => 'https://discord.com',
30+
]
31+
],
32+
'components' => [
2933
[
30-
"type" => 1,
31-
"components" => [
34+
'type' => 1,
35+
'components' => [
3236
[
33-
"type" => 2,
34-
"label" => "Test",
35-
"style" => 1,
36-
"custom_id" => "primary"
37+
'type' => 2,
38+
'label' => 'Test',
39+
'style' => 1,
40+
'custom_id' => 'primary'
3741
]
3842
]
3943
]
@@ -84,7 +88,8 @@ public function toDiscord()
8488
->embed([
8589
'title' => 'Object Title',
8690
'url' => 'https://discord.com',
87-
])->components([
91+
])
92+
->components([
8893
[
8994
"type" => 1,
9095
"components" => [

0 commit comments

Comments
 (0)