Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Change the Message send and sendTo return type to a promise (#45
Browse files Browse the repository at this point in the history
)

🎨 Log a console error instead of exception when passed an invalid user
  • Loading branch information
Log1x authored Feb 22, 2024
1 parent 457973f commit 6942711
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Discord/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Exception;
use Illuminate\Support\Str;
use Laracord\Laracord;
use React\Promise\ExtendedPromiseInterface;
use Throwable;

class Message
Expand Down Expand Up @@ -189,27 +190,27 @@ public function build(): MessageBuilder
/**
* Send the message.
*/
public function send(mixed $destination = null): void
public function send(mixed $destination = null): ?ExtendedPromiseInterface
{
if ($destination) {
$this->channel($destination);
}

$this->getChannel()->sendMessage($this->build());
return $this->getChannel()->sendMessage($this->build());
}

/**
* Send the message to a user.
*/
public function sendTo(mixed $user): void
public function sendTo(mixed $user): ?ExtendedPromiseInterface
{
if (is_numeric($user)) {
$member = $this->bot->discord()->users->get('id', $user);

if (! $member) {
$this->bot->console()->error("Could not find user <fg=red>{$user}</> to send message");

return;
return null;
}

$user = $member;
Expand All @@ -220,10 +221,12 @@ public function sendTo(mixed $user): void
}

if (! $user instanceof User) {
throw new Exception('You must provide a valid Discord user.');
$this->bot->console()->error('You must provide a valid Discord user.');

return null;
}

$user->sendMessage($this->build());
return $user->sendMessage($this->build());
}

/**
Expand Down

0 comments on commit 6942711

Please sign in to comment.