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

🚧 Initial Discord message components v2 implementation #1294

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
Prev Previous commit
Next Next commit
🚧 Allow passing attachments to file and unfurled media item
  • Loading branch information
Log1x committed Feb 21, 2025
commit e1316b6c733f43267c14fa4fd5ebc67604743361
19 changes: 14 additions & 5 deletions src/Discord/Builders/Components/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Discord\Builders\Components;

use Discord\Parts\Channel\Attachment;

/**
* File components allow you to send a file. You can also spoiler it.
*
Expand All @@ -37,27 +39,34 @@ class File extends Component
/**
* Creates a new file component.
*
* @param string $filename The filename to reference.
* @param string|Attachment|null $filename The filename or attachment to reference.
*
* @return self
*/
public static function new(string $filename): self
public static function new(string|Attachment|null $filename = null): self
{
$component = new self();
$component->setFile($filename);

if ($filename !== null) {
$component->setFile($filename);
}

return $component;
}

/**
* Sets the file to be displayed.
*
* @param string $filename The filename to reference.
* @param string|Attachment $filename The filename or attachment to reference.
*
* @return $this
*/
public function setFile(string $filename): self
public function setFile(string|Attachment $filename): self
{
if ($filename instanceof Attachment) {
$filename = $filename->filename;
}

$this->file = ['url' => "attachment://{$filename}"];

return $this;
Expand Down
9 changes: 7 additions & 2 deletions src/Discord/Builders/Components/UnfurledMediaItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Discord\Builders\Components;

use Discord\Parts\Channel\Attachment;
use JsonSerializable;

/**
Expand Down Expand Up @@ -91,12 +92,16 @@ public static function new(string $url): self
/**
* Creates a new unfurled media item from an attachment.
*
* @param string $filename Name of the attachment file.
* @param string|Attachment $filename Name of the attachment file, or null.
*
* @return self
*/
public static function fromAttachment(string $filename): self
public static function fromAttachment(string|Attachment $filename): self
{
if ($filename instanceof Attachment) {
$filename = $filename->filename;
}

return self::new("attachment://{$filename}");
}

Expand Down