updated namespace for DiscordMessage

This commit is contained in:
Jakub Novák
2024-09-30 10:06:50 +02:00
parent a2bca3d5b8
commit 9e2f0fb894
3 changed files with 3 additions and 3 deletions

View File

@@ -0,0 +1,70 @@
<?php
namespace App\Notifications\Dto;
class DiscordMessage
{
private array $fields = [];
public function __construct(
public string $title,
public string $description,
public int $color,
public bool $isCritical = false,
) {}
public static function successColor(): int
{
return hexdec('a1ffa5');
}
public static function warningColor(): int
{
return hexdec('ffa743');
}
public static function errorColor(): int
{
return hexdec('ff705f');
}
public function addField(string $name, string $value): self
{
$this->fields[] = [
'name' => $name,
'value' => $value,
];
return $this;
}
public function toPayload(): array
{
$payload = [
'embeds' => [
[
'title' => $this->title,
'description' => $this->description,
'color' => $this->color,
'fields' => $this->addTimestampToFields($this->fields),
],
],
];
if ($this->isCritical) {
$payload['content'] = '@here';
}
return $payload;
}
private function addTimestampToFields(array $fields): array
{
$fields[] = [
'name' => 'Time',
'value' => '<t:'.now()->timestamp.':R>',
];
return $fields;
}
}