Skip to content

Commit

Permalink
Implemented ability to add attachments to message
Browse files Browse the repository at this point in the history
  • Loading branch information
Plytas committed Jul 16, 2024
1 parent 5ddab09 commit 278eba0
Show file tree
Hide file tree
Showing 30 changed files with 151 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/Components/ActionRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ActionRow extends Data implements DiscordComponent
public function __construct(
/** @var Collection<int, DiscordComponent> */
public Collection $components = new Collection(),
) {}
) {
}

public static function new(): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function __construct(
public ?bool $disabled = null,
public ?string $url = null,
public ButtonStyle $style = ButtonStyle::Primary,
) {}
) {
}

public static function new(): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Components/SelectOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public function __construct(
public ?string $description = null,
public ?DiscordEmoji $emoji = null,
public ?bool $default = null,
) {}
) {
}

public static function new(string $label, string $value): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Components/StringSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function __construct(
public ?int $min_values = 1,
public ?int $max_values = 1,
public ?bool $disabled = false,
) {}
) {
}

public static function new(string $custom_id): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Components/TextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function __construct(
public ?int $min_length = null,
public ?int $max_length = null,
public TextInputStyle $style = TextInputStyle::Short,
) {}
) {
}

public static function new(string $custom_id, string $label): self
{
Expand Down
4 changes: 3 additions & 1 deletion src/Contracts/DiscordComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

namespace Plytas\Discord\Contracts;

interface DiscordComponent {}
interface DiscordComponent
{
}
3 changes: 2 additions & 1 deletion src/Data/DiscordApplicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public function __construct(
public ?string $description = null,
/** @var Collection<int, DiscordApplicationCommandOption> */
public ?Collection $options = null,
) {}
) {
}

public static function new(string $name, CommandType $type): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Data/DiscordApplicationCommandOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public function __construct(
public ?string $description = null,
/** @var Collection<int, DiscordApplicationCommandOption> */
public ?Collection $options = null,
) {}
) {
}

public static function new(string $name, CommandOptionType $type): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Data/DiscordChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public function __construct(
public string $guild_id,
public ?string $parent_id,
public int $position,
) {}
) {
}
}
3 changes: 2 additions & 1 deletion src/Data/DiscordEmoji.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class DiscordEmoji extends Data
{
public function __construct(
public string $name,
) {}
) {
}

public static function new(string $name): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Data/DiscordGuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public function __construct(
public string $id,
public string $name,
public ?string $icon = null,
) {}
) {
}
}
3 changes: 2 additions & 1 deletion src/Data/DiscordInteraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function __construct(
private ?DiscordApplicationCommand $applicationCommand = null,
private ?DiscordMessageComponent $messageComponent = null,
private ?DiscordModalComponent $modalComponent = null,
) {}
) {
}

public function pong(): DiscordResponse
{
Expand Down
3 changes: 2 additions & 1 deletion src/Data/DiscordMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class DiscordMember extends Data
{
public function __construct(
public ?DiscordUser $user = null,
) {}
) {
}
}
34 changes: 33 additions & 1 deletion src/Data/DiscordMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public function __construct(
public Collection $embeds = new Collection(),
/** @var Collection<int, DiscordComponent> */
public Collection $components = new Collection(),
) {}

public ?array $attachments = null,
/** @var Collection<int, DiscordMessageFile> */
private Collection $files = new Collection(),
) {
}

public static function new(): self
{
Expand Down Expand Up @@ -53,4 +58,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();
}
}
3 changes: 2 additions & 1 deletion src/Data/DiscordMessageComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public function __construct(
* @var array<int, string>
*/
public ?array $values,
) {}
) {
}
}
11 changes: 10 additions & 1 deletion src/Data/DiscordMessageEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ 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 +54,11 @@ public function addField(?DiscordMessageEmbedField $field): self

return $this;
}

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

return $this;
}
}
3 changes: 2 additions & 1 deletion src/Data/DiscordMessageEmbedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public function __construct(
public string $name,
public string $value,
public bool $inline = false,
) {}
) {
}
}
16 changes: 16 additions & 0 deletions src/Data/DiscordMessageEmbedImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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,
) {
}
}
19 changes: 19 additions & 0 deletions src/Data/DiscordMessageFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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);
}
}
3 changes: 2 additions & 1 deletion src/Data/DiscordModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public function __construct(
public string $title,
/** @var Collection<int, DiscordComponent> */
public Collection $components = new Collection(),
) {}
) {
}

public static function new(string $custom_id, string $title): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Data/DiscordModalActionRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public function __construct(
public ComponentType $type,
/** @var Collection<int, DiscordModalTextInput> */
public Collection $components,
) {}
) {
}
}
3 changes: 2 additions & 1 deletion src/Data/DiscordModalComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public function __construct(
public string $custom_id,
/** @var Collection<int, DiscordModalActionRow> */
public Collection $components,
) {}
) {
}

/**
* @return Collection<string, DiscordModalTextInput>
Expand Down
3 changes: 2 additions & 1 deletion src/Data/DiscordModalTextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public function __construct(
public string $custom_id,
public string $value,
public ComponentType $type,
) {}
) {
}
}
3 changes: 2 additions & 1 deletion src/Data/DiscordResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class DiscordResponse extends Data
public function __construct(
public ResponseType $type,
public Data|Optional $data,
) {}
) {
}

public static function pong(): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Data/DiscordRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public function __construct(
public string $name,
public ?string $description,
public int $color,
) {}
) {
}

public function colorToHex(): string
{
Expand Down
3 changes: 2 additions & 1 deletion src/Data/DiscordUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public function __construct(
public ?string $avatar = null,
public ?bool $bot = false,
public ?bool $system = false,
) {}
) {
}
}
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
3 changes: 2 additions & 1 deletion src/Events/DiscordCommandInteractionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class DiscordCommandInteractionEvent

public function __construct(
public DiscordInteraction $interaction
) {}
) {
}
}
3 changes: 2 additions & 1 deletion src/Events/DiscordMessageComponentInteractionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class DiscordMessageComponentInteractionEvent

public function __construct(
public DiscordInteraction $interaction
) {}
) {
}
}
3 changes: 2 additions & 1 deletion src/Events/DiscordModalInteractionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class DiscordModalInteractionEvent

public function __construct(
public DiscordInteraction $interaction
) {}
) {
}
}

0 comments on commit 278eba0

Please sign in to comment.