-
-
Notifications
You must be signed in to change notification settings - Fork 245
🚧 Initial Discord message components v2 implementation #1294
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
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2f0db2b
🚧 Initial Discord message components v2 implementation
Log1x e1316b6
🚧 Allow passing attachments to file and unfurled media item
Log1x 4ec3f39
🚧 Improve v2 component support
Log1x c2f2bf2
🎨 Merge flags when setting ephemeral
Log1x 4dcfa4f
🔥 Remove unnecessary component allow list
Log1x 84cd998
🎨 Improve v2 flag handling
Log1x 9336de5
🎨 Add a contract for V2 components
Log1x cd5b233
poly_strlen
valzargaming 9dd7539
getFlags(): ?int
valzargaming 9a4154f
Add imports for ActionRow and SelectMenu
valzargaming acefceb
instance to use imports
valzargaming 92cb43e
Update setMedia to accept UnfurledMediaItem as param
valzargaming f9ce1d4
Thumbnail's getMedia returns UnfurledMediaItem
valzargaming e653b20
Remove setSpacingSmall and setSpacingLarge macros
valzargaming 6e0464a
setAccessory throws exception for invalid components
valzargaming 1fe77c1
getAccessory(): Component
valzargaming dd5c67b
FLAG_V2_COMPONENTS => FLAG_IS_V2_COMPONENTS
valzargaming 8f0ee3b
FLAG_V2_COMPONENTS => FLAG_IS_V2_COMPONENTS
valzargaming fb211d5
Validate component types for Containers
valzargaming fe13130
Docs for Section addComponent
valzargaming 0c0244f
Doc primitive last
valzargaming 859ef46
getFlags returns 0 if null
valzargaming 35ee22c
Remove undesired nullsafe
valzargaming a8601c4
Remove loading_state from array_filter
valzargaming ab0f4a0
Add loading_state to array_merge
valzargaming 118a8ba
Don't send content or embeds if FLAG_IS_V2_COMPONENTS
valzargaming 349b249
Update MessageBuilder's addComponents to work with both v1 and v2 com…
valzargaming 927e6a4
Merge branch 'master' into components-v2
Log1x 2316e24
Bump @since from 10.4.0 to 10.5.0
valzargaming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
<?php | ||
|
||
/* | ||
* This file is a part of the DiscordPHP project. | ||
* | ||
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com> | ||
* | ||
* This file is subject to the MIT license that is bundled | ||
* with this source code in the LICENSE.md file. | ||
*/ | ||
|
||
namespace Discord\Builders\Components; | ||
|
||
use Discord\Builders\Components\ActionRow; | ||
use Discord\Builders\Components\Section; | ||
use Discord\Builders\Components\TextDisplay; | ||
use Discord\Builders\Components\MediaGallery; | ||
use Discord\Builders\Components\File; | ||
use Discord\Builders\Components\Separator; | ||
|
||
/** | ||
* Containers are a new way to group components together. | ||
* You can also specify an accent color (similar to embeds) and spoiler it. | ||
* | ||
* @link https://discord.com/developers/docs/interactions/message-components#container | ||
* | ||
* @since 10.5.0 | ||
*/ | ||
class Container extends Component implements Contracts\ComponentV2 | ||
{ | ||
/** | ||
* Array of components. | ||
* | ||
* @var Component[] | ||
*/ | ||
private $components = []; | ||
|
||
/** | ||
* Accent color for the container. | ||
* | ||
* @var int|null | ||
*/ | ||
private $accent_color; | ||
|
||
/** | ||
* Whether the container is a spoiler. | ||
* | ||
* @var bool | ||
*/ | ||
private $spoiler = false; | ||
|
||
/** | ||
* Creates a new container. | ||
* | ||
* @return self | ||
*/ | ||
public static function new(): self | ||
{ | ||
return new self(); | ||
} | ||
|
||
/** | ||
* Adds a component to the container. | ||
* | ||
* @param ActionRow|Section|TextDisplay|MediaGallery|File|Separator $component Component to add. | ||
* | ||
* @throws \InvalidArgumentException Component is not a valid type. | ||
* @throws \OverflowException Container exceeds 10 components. | ||
* | ||
* @return $this | ||
*/ | ||
public function addComponent(Component $component): self | ||
{ | ||
if (count($this->components) >= 10) { | ||
throw new \OverflowException('You can only have 10 components per container.'); | ||
} | ||
|
||
if (! ( $component instanceof ActionRow || $component instanceof Section || $component instanceof TextDisplay || $component instanceof MediaGallery || $component instanceof File || $component instanceof Separator )) { | ||
throw new \InvalidArgumentException('Invalid component type.'); | ||
} | ||
|
||
$this->components[] = $component; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add a group of components to the container. | ||
* | ||
* @param Component[] $components Components to add. | ||
* | ||
* @throws \InvalidArgumentException Component is not a valid type. | ||
* | ||
* @return $this | ||
*/ | ||
public function addComponents(array $components): self | ||
{ | ||
foreach ($components as $component) { | ||
$this->addComponent($component); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the components for the container. | ||
* | ||
* @param Component[] $components Components to set. | ||
* | ||
* @return $this | ||
*/ | ||
public function setComponents(array $components): self | ||
{ | ||
$this->components = $components; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the accent color for the container. | ||
* | ||
* @param int|null $color Color code for the container. | ||
* | ||
* @return $this | ||
*/ | ||
public function setAccentColor(?int $color): self | ||
{ | ||
$this->accent_color = $color; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets whether the container is a spoiler. | ||
* | ||
* @param bool $spoiler Whether the container is a spoiler. | ||
* | ||
* @return $this | ||
*/ | ||
public function setSpoiler(bool $spoiler = true): self | ||
{ | ||
$this->spoiler = $spoiler; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns all the components in the container. | ||
* | ||
* @return Component[] | ||
*/ | ||
public function getComponents(): array | ||
{ | ||
return $this->components; | ||
} | ||
|
||
/** | ||
* Returns the accent color for the container. | ||
* | ||
* @return int|null | ||
*/ | ||
public function getAccentColor(): ?int | ||
{ | ||
return $this->accent_color; | ||
} | ||
|
||
/** | ||
* Returns whether the container is a spoiler. | ||
* | ||
* @return bool | ||
*/ | ||
public function isSpoiler(): bool | ||
{ | ||
return $this->spoiler; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
$data = [ | ||
'type' => Component::TYPE_CONTAINER, | ||
'components' => $this->components, | ||
]; | ||
|
||
if (isset($this->accent_color)) { | ||
$data['accent_color'] = $this->accent_color; | ||
} | ||
|
||
if ($this->spoiler) { | ||
$data['spoiler'] = true; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is a part of the DiscordPHP project. | ||
* | ||
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com> | ||
* | ||
* This file is subject to the MIT license that is bundled | ||
* with this source code in the LICENSE.md file. | ||
*/ | ||
|
||
namespace Discord\Builders\Components\Contracts; | ||
|
||
/** | ||
* This interface is a contract for V2 components. | ||
* | ||
* @since 10.5.0 | ||
*/ | ||
interface ComponentV2 | ||
{ | ||
// | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
/* | ||
* This file is a part of the DiscordPHP project. | ||
* | ||
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com> | ||
* | ||
* This file is subject to the MIT license that is bundled | ||
* with this source code in the LICENSE.md file. | ||
*/ | ||
|
||
namespace Discord\Builders\Components; | ||
|
||
use Discord\Parts\Channel\Attachment; | ||
|
||
/** | ||
* File components allow you to send a file. You can also spoiler it. | ||
* | ||
* @link https://discord.com/developers/docs/interactions/message-components#file | ||
* | ||
* @since 10.5.0 | ||
*/ | ||
class File extends Component implements Contracts\ComponentV2 | ||
{ | ||
/** | ||
* The file to be displayed. | ||
* | ||
* @var array | ||
*/ | ||
private $file; | ||
|
||
/** | ||
* Whether the file is a spoiler. | ||
* | ||
* @var bool | ||
*/ | ||
private $spoiler = false; | ||
|
||
/** | ||
* Creates a new file component. | ||
* | ||
* @param string|Attachment|null $filename The filename or attachment to reference. | ||
* | ||
* @return self | ||
*/ | ||
public static function new(string|Attachment|null $filename = null): self | ||
{ | ||
$component = new self(); | ||
|
||
if ($filename !== null) { | ||
$component->setFile($filename); | ||
} | ||
|
||
return $component; | ||
} | ||
|
||
/** | ||
* Sets the file to be displayed. | ||
* | ||
* @param string|Attachment $filename The filename or attachment to reference. | ||
* | ||
* @return $this | ||
*/ | ||
public function setFile(string|Attachment $filename): self | ||
{ | ||
if ($filename instanceof Attachment) { | ||
$filename = $filename->filename; | ||
} | ||
|
||
$this->file = ['url' => "attachment://{$filename}"]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets whether the file is a spoiler. | ||
* | ||
* @param bool $spoiler Whether the file is a spoiler. | ||
* | ||
* @return $this | ||
*/ | ||
public function setSpoiler(bool $spoiler = true): self | ||
{ | ||
$this->spoiler = $spoiler; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the file reference. | ||
* | ||
* @return array | ||
*/ | ||
public function getFile(): array | ||
{ | ||
return $this->file; | ||
} | ||
|
||
/** | ||
* Returns whether the file is a spoiler. | ||
* | ||
* @return bool | ||
*/ | ||
public function isSpoiler(): bool | ||
{ | ||
return $this->spoiler; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
$data = [ | ||
'type' => Component::TYPE_FILE, | ||
'file' => $this->file, | ||
]; | ||
|
||
if ($this->spoiler) { | ||
$data['spoiler'] = true; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.