Skip to content
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

Added ability to customize username and avatar of webhook #36

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/DiscordAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
class DiscordAlert
{
protected string $webhookUrlName = 'default';
protected $username = null;
protected $avatarUrl = null;

public function to(string $webhookUrlName): self
{
Expand All @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/Jobs/SendToDiscordChannelJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down