Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Add conditional methods to Message (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Jun 19, 2024
2 parents e490066 + d6e1d2a commit 804ab6c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Commands/SlashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected function option(?string $key = null, mixed $default = null): mixed
/**
* Retrieve the option value.
*/
public function value(?string $option = null, mixed $default = null): mixed
protected function value(?string $option = null, mixed $default = null): mixed
{
$options = $this->flattenOptions($this->getOptions());

Expand Down
60 changes: 50 additions & 10 deletions src/Discord/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,21 @@ public function build(): MessageBuilder
->setContent($this->body)
->setComponents($this->getComponents());

if ($this->content || $this->fields) {
if ($this->hasContent() || $this->hasFields()) {
$message->addEmbed($this->getEmbed());
}

if ($this->selects) {
if ($this->hasSelects()) {
foreach ($this->selects as $select) {
$message->addComponent($select);
}
}

if ($this->buttons) {
if ($this->hasButtons()) {
$message->addComponent($this->getButtons());
}

if ($this->files) {
if ($this->hasFiles()) {
foreach ($this->files as $file) {
$message->addFileFromContent($file['filename'], $file['content']);
}
Expand Down Expand Up @@ -383,7 +383,7 @@ public function getComponents(): array
*/
public function getButtons()
{
if (empty($this->buttons)) {
if (! $this->hasButtons()) {
return;
}

Expand Down Expand Up @@ -450,6 +450,14 @@ public function content(?string $content): self
return $this;
}

/**
* Determine if the message has content.
*/
public function hasContent(): bool
{
return ! empty($this->content);
}

/**
* Set the message avatar.
*/
Expand Down Expand Up @@ -531,6 +539,14 @@ public function filePath(string $path, string $filename = ''): self
return $this;
}

/**
* Determine if the message has files.
*/
public function hasFiles(): bool
{
return ! empty($this->files);
}

/**
* Set the message color.
*/
Expand Down Expand Up @@ -765,6 +781,14 @@ public function clearFields(): self
return $this;
}

/**
* Determine if the message has fields.
*/
public function hasFields(): bool
{
return ! empty($this->fields);
}

/**
* Set the message components.
*/
Expand Down Expand Up @@ -854,6 +878,24 @@ public function select(
return $this;
}

/**
* Clear the select menus from the message.
*/
public function clearSelects(): self
{
$this->selects = [];

return $this;
}

/**
* Determine if the message has select menus.
*/
public function hasSelects(): bool
{
return ! empty($this->selects);
}

/**
* Add a button to the message.
*/
Expand Down Expand Up @@ -950,13 +992,11 @@ public function clearButtons(): self
}

/**
* Clear the select menus from the message.
* Determine if the message has buttons.
*/
public function clearSelects(): self
public function hasButtons(): bool
{
$this->selects = [];

return $this;
return ! empty($this->buttons);
}

/**
Expand Down

0 comments on commit 804ab6c

Please sign in to comment.