2021-06-04 22:51:59 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Notifications\Channels\Discord;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Original is from https://gist.github.com/freekmurze/e4415090f650e070d3de8b905875cf78
|
|
|
|
*
|
|
|
|
* Markdown guide: https://birdie0.github.io/discord-webhooks-guide/other/discord_markdown.html
|
|
|
|
*/
|
|
|
|
class DiscordMessage
|
|
|
|
{
|
2022-02-15 05:24:22 +08:00
|
|
|
const COLOR_SUCCESS = '0B6623';
|
|
|
|
const COLOR_WARNING = 'FD6A02';
|
|
|
|
const COLOR_ERROR = 'ED2939';
|
2021-06-04 22:51:59 +08:00
|
|
|
|
|
|
|
public $webhook_url;
|
|
|
|
|
|
|
|
protected $title;
|
|
|
|
protected $url;
|
2022-02-15 05:24:22 +08:00
|
|
|
protected $thumbnail = [];
|
|
|
|
protected $image = [];
|
2021-06-04 22:51:59 +08:00
|
|
|
protected $description;
|
|
|
|
protected $timestamp;
|
|
|
|
protected $footer;
|
|
|
|
protected $color;
|
|
|
|
protected $author = [];
|
|
|
|
protected $fields = [];
|
|
|
|
|
2022-02-15 05:24:22 +08:00
|
|
|
// Supply the webhook URL that this should be going to
|
2021-06-04 22:51:59 +08:00
|
|
|
public function webhook(string $url): self
|
|
|
|
{
|
|
|
|
$this->webhook_url = $url;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:24:22 +08:00
|
|
|
// Title of the embed
|
2021-06-04 22:51:59 +08:00
|
|
|
public function title(string $title): self
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:24:22 +08:00
|
|
|
// URL of the Title
|
2021-06-04 22:51:59 +08:00
|
|
|
public function url(string $url): self
|
|
|
|
{
|
|
|
|
$this->url = $url;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:24:22 +08:00
|
|
|
// Thumbnail image (placed right side of embed)
|
|
|
|
// ['url' => '']
|
|
|
|
public function thumbnail(array $thumbnail): self
|
|
|
|
{
|
|
|
|
$this->thumbnail = $thumbnail;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main image (placed bottom of embed)
|
|
|
|
// ['url' => '']
|
|
|
|
public function image(array $image): self
|
|
|
|
{
|
|
|
|
$this->image = $image;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:51:59 +08:00
|
|
|
/**
|
|
|
|
* @param array|string $descriptionLines
|
|
|
|
*/
|
|
|
|
public function description($descriptionLines): self
|
|
|
|
{
|
|
|
|
if (!is_array($descriptionLines)) {
|
|
|
|
$descriptionLines = [$descriptionLines];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->description = implode(PHP_EOL, $descriptionLines);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:24:22 +08:00
|
|
|
// Author details
|
|
|
|
// ['name' => '', 'url' => '', 'icon_url' => '']
|
2021-06-04 22:51:59 +08:00
|
|
|
public function author(array $author): self
|
|
|
|
{
|
|
|
|
$this->author = $author;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:24:22 +08:00
|
|
|
// Fields
|
2021-06-04 22:51:59 +08:00
|
|
|
public function fields(array $fields): self
|
|
|
|
{
|
|
|
|
$this->fields = [];
|
|
|
|
foreach ($fields as $name => $value) {
|
|
|
|
$this->fields[] = [
|
|
|
|
'name' => '**'.$name.'**', // bold
|
|
|
|
'value' => $value,
|
|
|
|
'inline' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function footer(string $footer): self
|
|
|
|
{
|
|
|
|
$this->footer = $footer;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:24:22 +08:00
|
|
|
// Fixed Success Color
|
2021-06-04 22:51:59 +08:00
|
|
|
public function success(): self
|
|
|
|
{
|
2022-02-15 05:24:22 +08:00
|
|
|
$this->color = hexdec('0B6623'); // static::COLOR_SUCCESS;
|
2021-06-04 22:51:59 +08:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:24:22 +08:00
|
|
|
// Fixed Warning
|
2021-06-04 22:51:59 +08:00
|
|
|
public function warning(): self
|
|
|
|
{
|
2022-02-15 05:24:22 +08:00
|
|
|
$this->color = hexdec('FD6A02'); // static::COLOR_WARNING;
|
2021-06-04 22:51:59 +08:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:24:22 +08:00
|
|
|
// Fixed Error
|
2021-06-04 22:51:59 +08:00
|
|
|
public function error(): self
|
|
|
|
{
|
2022-02-15 05:24:22 +08:00
|
|
|
$this->color = hexdec('ED2939'); // static::COLOR_ERROR;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom Color
|
|
|
|
public function color(string $embed_color): self
|
|
|
|
{
|
|
|
|
$this->color = hexdec($embed_color);
|
2021-06-04 22:51:59 +08:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
2021-06-05 03:16:36 +08:00
|
|
|
$embeds = [
|
2022-02-15 05:24:22 +08:00
|
|
|
'type' => 'rich',
|
|
|
|
'color' => $this->color,
|
2021-06-05 03:16:36 +08:00
|
|
|
'title' => $this->title,
|
|
|
|
'url' => $this->url,
|
2022-02-15 05:24:22 +08:00
|
|
|
'thumbnail' => $this->thumbnail,
|
2021-06-05 03:16:36 +08:00
|
|
|
'description' => $this->description,
|
|
|
|
'author' => $this->author,
|
2022-02-15 05:24:22 +08:00
|
|
|
'image' => $this->image,
|
2021-06-05 03:16:36 +08:00
|
|
|
'timestamp' => Carbon::now('UTC'),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!empty($this->fields)) {
|
|
|
|
$embeds['fields'] = $this->fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->footer)) {
|
|
|
|
$embeds['footer'] = [
|
|
|
|
'text' => $this->footer,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:51:59 +08:00
|
|
|
return [
|
2021-06-05 03:16:36 +08:00
|
|
|
'embeds' => [$embeds],
|
2021-06-04 22:51:59 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|