florianrambur/discord-webhook

1.2.1 2023-05-11 14:21 UTC

This package is auto-updated.

Last update: 2024-09-11 17:10:15 UTC


README

安装

composer require florianrambur/discord-webhook

用法

以下是一个简单的示例

<?php

use FlorianRambur\DiscordWebhook\Client;

$config = [
    'url' => 'https://your-discord-webhook-url',
    'author' => [
        'username' => 'Florian',
        'avatar_url' => 'https://i.imgur.com/ZGPxFN2.jpg',
    ],
];

$client = new Client($config);

$response = $client->send([
    'content' => 'This is the main content',
]);

Simple Example

与Embed一起工作

此包为您提供使用简单的Embed类来创建高级消息的机会。要实现这一点,您可以将数据传递给构造函数或使用一些方法,如name($value)

<?php

$embed = (new Embed())
    ->add(new Author([
        'name' => 'This is the author',
        'url' => 'https://github.com/florianrambur/discord-webhook',
        'icon_url' => 'https://github.com/florianrambur/discord-webhook/icon.jpg',
    ]))
    ->add(new Body([
        'title' => 'Webhook Title',
        'description' => 'Webhook Description',
        'color' => 5814783,
    ]))
    ->add(new Image([
        'images' => ['https://i.imgur.com/ZGPxFN2.jpg'],
    ]))
    ->add(new Footer([
        'text' => 'This is the footer',
        'timestamp' => '2021-07-06T22:00:00.000Z',
    ]));
<?php

$author = (new Author())
    ->name('This is the author')
    ->url('https://github.com/florianrambur/discord-webhook')
    ->iconUrl('https://github.com/florianrambur/discord-webhook/icon.jpg');

$body = (new Body())
    ->title('Webhook Title')
    ->description('Webhook Description')
    ->color(5814783);

$image = (new Image())->image('https://i.imgur.com/ZGPxFN2.jpg');

$footer = (new Footer())
    ->text('This is the footer')
    ->timestamp('2021-07-06T22:00:00.000Z');

$embed = (new Embed())->addMany([$author, $body, $image, $footer]);

Embed提供了两种方法来添加属性类

<?php

(new Embed())
    ->add($body)
    ->add($footer);

(new Embed())->addMany([$body, $footer]);

然后,将您的数据和配置传递给Client

<?php

$config = [
    'url' => 'https://your-discord-webhook-url',
    'author' => [
        'username' => 'Florian',
        'avatar_url' => 'https://i.imgur.com/ZGPxFN2.jpg',
    ],
];

$client = new Client($config);

$response = $client->send([
    'embeds' => [
        $embed->toArray(),
    ],
]);

Embed Example

接下来是什么

此包处于开发中,有些功能尚未实现

  • 缺少一些属性上的约束
  • 将创建一些模板,以便您可以更轻松、更快速地创建消息