axl1232/php-discord-webhook

一个用于使用 Discord webhook 的 PHP 库。

1.0.2 2021-10-29 00:02 UTC

This package is auto-updated.

Last update: 2024-09-29 06:03:10 UTC


README

一个使发送 Discord webhook 更容易的 PHP 库。

安装

composer require axl1232/php-discord-webhook

用法

发送文本消息

$message = (new Axl1232\PhpDiscordWebhook\Message())
    ->setContent('Hello world')
    ->setUsername('Test hook')
    ->setAvatarUrl('https://example.com/avatar.png');

$hook = new Axl1232\PhpDiscordWebhook\Webhook('https://discordapp.com/api/webhooks/test/hook');
$hook->send($message);

带附件的文本消息

$message = (new Axl1232\PhpDiscordWebhook\Message())
    ->setContent('Hello world')
    ->setUsername('Test hook')
    ->setAvatarUrl('https://example.com/avatar.png')
    ->setFile('/path/to/file.png');

$hook = new Axl1232\PhpDiscordWebhook\Webhook('https://discordapp.com/api/webhooks/test/hook');
$hook->send($message);

发送嵌入消息

$message = (new Axl1232\PhpDiscordWebhook\Message())
    ->setContent('Hello world')
    ->setUsername('Test hook')
    ->setAvatarUrl('https://example.com/avatar.png')
    ->addEmbed(
        (new Axl1232\PhpDiscordWebhook\Embed())
        ->setTitle('Test embed')
        ->setUrl('https://example.com/')
        ->setDescription('Some embed description')
        ->setImage(
            (new Axl1232\PhpDiscordWebhook\Embed\Image())
            ->setUrl('https://example.com/image.jpg')
        )
        ->setAuthor(
            (new Axl1232\PhpDiscordWebhook\Embed\Author())
            ->setName('Author')
            ->setUrl('https://example.com')
            ->setIconUrl('https://example.com/author_icon.png')
        )
        ->setColor(Axl1232\PhpDiscordWebhook\Tools\ColorHelper::FUSCHIA)
        ->setFooter(
            (new Axl1232\PhpDiscordWebhook\Embed\Footer())
            ->setText('Footer text')
            ->setIconUrl('https://example.com/footer_icon.png')
        )
        ->setTimestamp(new DateTime('-1 week'))
        ->setThumbnail(
            (new Axl1232\PhpDiscordWebhook\Embed\Thumbnail())
            ->setUrl('https://example.com/thumbnail.png')
        )
        ->addField(
            (new Axl1232\PhpDiscordWebhook\Embed\Field())
            ->setName('Field 1')
            ->setValue('Value 1')
        )
        ->addField(
            (new Axl1232\PhpDiscordWebhook\Embed\Field())
            ->setName('Field 2')
            ->setValue('Value 2')
        )
    );

$hook = new Axl1232\PhpDiscordWebhook\Webhook('https://discordapp.com/api/webhooks/test/hook');
$hook->send($message);

Axl1232\PhpDiscordWebhook\Tools\ColorHelper 中的预定义颜色