Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for issue #68: send embed and components only if filled #69

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/DiscordChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,24 @@ public function __construct(Discord $discord)
*/
public function send($notifiable, Notification $notification)
{
if (! $channel = $notifiable->routeNotificationFor('discord', $notification)) {
if (!$channel = $notifiable->routeNotificationFor('discord', $notification)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suspect this change will fail StyleCI check since the default in Laravel is with the space

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed it with another commit. I personally don't use StyleCI nor I have an integration in my IDE for that.

return;
}

$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