coolify/app/Notifications/Dto/DiscordMessage.php

84 lines
1.8 KiB
PHP
Raw Normal View History

2024-09-29 22:43:35 +00:00
<?php
2024-09-30 08:06:50 +00:00
namespace App\Notifications\Dto;
2024-09-29 22:43:35 +00:00
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 static function infoColor(): int
{
return hexdec('4f545c');
}
2024-10-21 20:40:43 +00:00
public function addField(string $name, string $value, bool $inline = false): self
2024-09-29 22:43:35 +00:00
{
$this->fields[] = [
'name' => $name,
'value' => $value,
2024-10-21 20:40:43 +00:00
'inline' => $inline,
2024-09-29 22:43:35 +00:00
];
return $this;
}
public function toPayload(): array
{
$footerText = 'Coolify v'.config('constants.coolify.version');
2024-10-21 20:40:43 +00:00
if (isCloud()) {
$footerText = 'Coolify Cloud';
}
2024-09-29 22:43:35 +00:00
$payload = [
'embeds' => [
[
'title' => $this->title,
'description' => $this->description,
'color' => $this->color,
'fields' => $this->addTimestampToFields($this->fields),
2024-10-21 20:40:43 +00:00
'footer' => [
'text' => $footerText,
],
2024-09-29 22:43:35 +00:00
],
],
];
if ($this->isCritical) {
$payload['content'] = '@here';
}
return $payload;
}
private function addTimestampToFields(array $fields): array
{
$fields[] = [
'name' => 'Time',
'value' => '<t:'.now()->timestamp.':R>',
2024-10-21 20:40:43 +00:00
'inline' => true,
2024-09-29 22:43:35 +00:00
];
return $fields;
}
}