Skip to content

Commit

Permalink
Implemented ability to add attachments to message (#6)
Browse files Browse the repository at this point in the history
* Implemented ability to add attachments to message

* Fix styling

* Fix phpstan issue

---------

Co-authored-by: Plytas <[email protected]>
  • Loading branch information
Plytas and Plytas authored Jul 16, 2024
1 parent 5ddab09 commit 4bfbc97
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/Data/DiscordMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public function __construct(
public Collection $embeds = new Collection(),
/** @var Collection<int, DiscordComponent> */
public Collection $components = new Collection(),
/** @var array<int, mixed> */
public ?array $attachments = null,
/** @var Collection<int, DiscordMessageFile> */
private Collection $files = new Collection(),
) {}

public static function new(): self
Expand Down Expand Up @@ -53,4 +57,31 @@ public function addComponent(DiscordComponent $component): self

return $this;
}

public function removeAttachments(): self
{
$this->attachments = [];

return $this;
}

public function addFile(DiscordMessageFile $file): self
{
$this->files->push($file);

return $this;
}

/**
* @return Collection<int, DiscordMessageFile>
*/
public function getFiles(): Collection
{
return $this->files;
}

public function isMultipart(): bool
{
return $this->files->isNotEmpty();
}
}
8 changes: 8 additions & 0 deletions src/Data/DiscordMessageEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function __construct(
public int|EmbedColor $color = EmbedColor::Default,
/** @var Collection<int, DiscordMessageEmbedField> */
public Collection $fields = new Collection(),
public ?DiscordMessageEmbedImage $image = null,
) {}

public static function new(): self
Expand Down Expand Up @@ -52,4 +53,11 @@ public function addField(?DiscordMessageEmbedField $field): self

return $this;
}

public function setImage(?DiscordMessageEmbedImage $image): self
{
$this->image = $image;

return $this;
}
}
15 changes: 15 additions & 0 deletions src/Data/DiscordMessageEmbedImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Plytas\Discord\Data;

use Spatie\LaravelData\Data;

class DiscordMessageEmbedImage extends Data
{
public function __construct(
public string $url,
public ?string $proxy_url = null,
public ?int $height = null,
public ?int $width = null,
) {}
}
18 changes: 18 additions & 0 deletions src/Data/DiscordMessageFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Plytas\Discord\Data;

use Spatie\LaravelData\Data;

class DiscordMessageFile extends Data
{
public function __construct(
public string $filename,
public string $content,
) {}

public static function new(string $filename, string $content): self
{
return new self($filename, $content);
}
}
25 changes: 22 additions & 3 deletions src/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public function __construct()
$this->client = Http::baseUrl('https://discord.com/api/v10')
->withUserAgent("DiscordBot ($appUrl, 1)")
->acceptJson()
->asJson()
->withToken($botToken, 'Bot');
}

Expand All @@ -64,12 +63,32 @@ public function deleteInteractionMessage(DiscordInteraction $interaction): Respo

public function createMessage(string $channelId, DiscordMessage $message): Response
{
return $this->client->post("/channels/{$channelId}/messages", $message->toArray());
if ($message->isMultipart()) {
$this->client->attach('payload_json', $message->toJson());

foreach ($message->getFiles() as $index => $file) {
$this->client->attach("files[{$index}]", $file->content, $file->filename);
}

return $this->client->post("/channels/{$channelId}/messages");
}

return $this->client->asJson()->post("/channels/{$channelId}/messages", $message->toArray());
}

public function updateMessage(string $channelId, string $messageId, DiscordMessage $message): Response
{
return $this->client->patch("/channels/{$channelId}/messages/{$messageId}", $message->toArray());
if ($message->isMultipart()) {
$this->client->attach('payload_json', $message->toJson());

foreach ($message->getFiles() as $index => $file) {
$this->client->attach("files[{$index}]", $file->content, $file->filename);
}

return $this->client->patch("/channels/{$channelId}/messages/{$messageId}");
}

return $this->client->asJson()->patch("/channels/{$channelId}/messages/{$messageId}", $message->toArray());
}

public function deleteMessage(string $channelId, string $messageId): Response
Expand Down

0 comments on commit 4bfbc97

Please sign in to comment.