Skip to content

Commit

Permalink
Update MessageBuilder's addComponents to work with both v1 and v2 com…
Browse files Browse the repository at this point in the history
…ponents
  • Loading branch information
valzargaming authored Feb 25, 2025
1 parent 118a8ba commit 349b249
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/Discord/Builders/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,23 @@ public function addComponent(Component $component): self
if (! ($this->flags & Message::FLAG_IS_V2_COMPONENTS)) {
$this->flags |= Message::FLAG_IS_V2_COMPONENTS;
}

if (isset($this->components) && count($this->components) >= 10) {
throw new \OverflowException('You can only add 10 components to a v2 message');
}

if (! ($this->flags & Message::FLAG_IS_V2_COMPONENTS)) {
if (isset($this->components)) {
if (count($this->components) >= 5) {
throw new \OverflowException('You can only add 5 components to a v1 message');
}
}
if (! ($component instanceof ActionRow || $component instanceof SelectMenu)) {
throw new \InvalidArgumentException('You can only add action rows and select menus as components to v1 messages. Put your other components inside an action row.');
}
} else {
if (isset($this->components)) {
if (count($this->components) >= 10) {
throw new \OverflowException('You can only add 10 components to a v2 message');
}
}
} elseif (! ($component instanceof ActionRow || $component instanceof SelectMenu)) {
throw new \InvalidArgumentException('You can only add action rows and select menus as components to v1 messages. Put your other components inside an action row.');
} elseif (isset($this->components) && count($this->components) >= 5) {
throw new \OverflowException('You can only add 5 components to a v1 message');
}

$this->components[] = $component;
Expand Down

0 comments on commit 349b249

Please sign in to comment.