Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function request($verb, $endpoint, array $data)
'headers' => [
'Authorization' => 'Bot '.$this->token,
],
'json' => $data,
'multipart' => $data,
]);
} catch (RequestException $exception) {
if ($response = $exception->getResponse()) {
Expand Down
24 changes: 22 additions & 2 deletions src/DiscordChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ public function send($notifiable, Notification $notification)
}

$message = $notification->toDiscord($notifiable);
$formData = $this->toFormData($message);

return $this->discord->send($channel, $formData);
}

private function toFormData(DiscordMessage $message): array
{
$data = [
'content' => $message->body
'content' => $message->body,
];

if (count($message->embed) > 0) {
Expand All @@ -49,6 +55,20 @@ public function send($notifiable, Notification $notification)
$data['components'] = $message->components;
}

return $this->discord->send($channel, $data);
$formData = [
[
'name' => 'payload_json',
'contents' => json_encode($data),
]
];

foreach ($message->files as $i => $file) {
$formData[] = [
'name' => 'files[' . $i . ']',
'contents' => $file,
];
}

return $formData;
}
}
32 changes: 28 additions & 4 deletions src/DiscordMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,36 @@ class DiscordMessage
*/
public $components;

/**
* The files to be attached to the message.
*
* @var array
*/
public $files;

/**
* @param string $body
* @param array|null $embed
*
* @return static
*/
public static function create($body = '', $embed = [], $components = [])
public static function create($body = '', $embed = [], $components = [], $files = [])
{
return new static($body, $embed, $components);
return new static($body, $embed, $components, $files);
}

/**
* @param string $body
* @param array $embed
* @param array $components
* @param array $files
*/
public function __construct($body = '', $embed = [], $components = [])
public function __construct($body = '', $embed = [], $components = [], $files = [])
{
$this->body = $body;
$this->embed = $embed;
$this->components = $components;
$this->files = $files;
}

/**
Expand All @@ -63,7 +73,7 @@ public function body($body)

/**
* Set a single embedded object.
*
*
* TODO: Refactor to enable multiple embeds.
* See https://discord.com/developers/docs/resources/channel#create-message
*
Expand Down Expand Up @@ -91,4 +101,18 @@ public function components($components)

return $this;
}

/**
* Set the files object.
*
* @param array $files
*
* @return $this
*/
public function files($files)
{
$this->files = $files;

return $this;
}
}