diff --git a/src/DiscordAlert.php b/src/DiscordAlert.php index 0d4d23c..03ec6e4 100644 --- a/src/DiscordAlert.php +++ b/src/DiscordAlert.php @@ -5,6 +5,8 @@ class DiscordAlert { protected string $webhookUrlName = 'default'; + protected $username = null; + protected $avatarUrl = null; public function to(string $webhookUrlName): self { @@ -13,6 +15,20 @@ public function to(string $webhookUrlName): self return $this; } + public function username(string $username): self + { + $this->username = $username; + + return $this; + } + + public function avatar(string $avatarUrl): self + { + $this->avatarUrl = $avatarUrl; + + return $this; + } + public function message(string $text, array $embeds = []): void { $webhookUrl = Config::getWebhookUrl($this->webhookUrlName); @@ -35,6 +51,9 @@ public function message(string $text, array $embeds = []): void 'embeds' => $embeds, ]; + if($this->username != null) { $jobArguments['username'] = $this->username; } + if($this->avatarUrl != null) { $jobArguments['avatarUrl'] = $this->avatarUrl; } + $job = Config::getJob($jobArguments); dispatch($job); diff --git a/src/Jobs/SendToDiscordChannelJob.php b/src/Jobs/SendToDiscordChannelJob.php index 1584ed0..42ff015 100644 --- a/src/Jobs/SendToDiscordChannelJob.php +++ b/src/Jobs/SendToDiscordChannelJob.php @@ -24,16 +24,21 @@ class SendToDiscordChannelJob implements ShouldQueue public function __construct( public string $text, public string $webhookUrl, - public array|null $embeds = null + public array|null $embeds = null, + public string|null $username = null, + public string|null $avatarUrl = null ) { } public function handle(): void { $payload = [ - 'content' => $this->text, + 'content' => $this->text ]; + if($this->username != null) { $payload['username'] = $this->username; } + if($this->avatarUrl != null) { $payload['avatar_url'] = $this->avatarUrl; } + if (! blank($this->embeds)) { $payload['embeds'] = $this->embeds; }