php-channels/discord-webhook

通过 Webhook 快速直接与 Discord 频道进行通信的接口。

v1.0.6 2024-04-17 18:48 UTC

This package is auto-updated.

Last update: 2024-09-28 16:57:08 UTC


README

php workflow Latest Stable Version Total Downloads License PHP Version Require

这是一个 PHP 包,包含了一套通过 Webhook 以简单、直接和优雅的方式与 Discord 频道进行通信的方法。

Composer

安装

composer require php-channels/discord-webhook

更新

composer update php-channels/discord-webhook

质量保证

单元测试

使用 PHPUnit 运行单元测试

./vendor/bin/phpunit ./tests

PHP Stan

使用 PHP Stan 运行代码静态分析

./vendor/bin/phpstan analyse

通用

本包的主要目标是允许通过 Webhook 以简单和语义化的方式发送高度可定制的消息到 Discord 频道。构建代码所使用的基负载可以在 ./documentation/payload.json 处访问。

基本用法

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setUsername('My Bot')
    ->setContent('Hello World!')
    ->send();

image

1. 定义 Webhook

有两种方法可以定义消息的目标 Webhook。第一种是将 Webhook 的 URL 作为参数传递给 message (?string $webhook = null) 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path');
// ...

或者,可以通过 setWebhook (string $webhook) 方法设置 Webhook,如下面的第二个示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message()
    ->setWebhook('https://discord.com/api/webhooks/your-webhook-path');
// ...

2. 用户名

可以使用 setUsername (string $username) 方法设置消息发送者的名称,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setUsername('Your Name')
    ->setContent('Hello World!')
    ->send();

3. 头像

可以使用 setAvatar (string $avatar_url) 方法设置消息发送者的头像,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setUsername('My Bot')
    ->setContent('Hello World!')
    ->setAvatar('https://fake-avatar.com/avatar.png')
    ->send();

4. 内容

可以使用 setContent (string $content) 方法设置消息的简单文本内容,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setContent('A simple text message you want to send!')
    ->send();

5. 嵌入

然而,可以通过 setEmbeds (array $embeds) 方法丰富消息内容,该方法接受一个数组作为参数,允许广泛的定制,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setEmbeds(['color' => '123456', 'title' => 'Embed content!'])
    ->send();

处理嵌入

通过单个数组完全处理此属性的单个数组处理嵌入内容非常强大,并且在许多情况下肯定非常有用。但是,当我们在处理大量设置的大数组时,这会变得非常复杂,使得代码难以阅读和维护。为了解决这个问题,包中添加了用于操作嵌入的方法,如下所述。

1. 作者

要处理 embeds[0]['author'] 属性,可以使用 setAuthor (string $name, ?string $url = '', ?string $icon_url = '') 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setAuthor('Author Name')
    ->send();

// optional param $url
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setAuthor('Author Name', 'https://fake-author.com')
    ->send();

// optional param $icon_url
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setAuthor('Author Name', 'https://fake-author.com', 'https://fake-icon.com/icon.png')
    ->send();

2. 标题

要处理 embeds[0]['title'] 属性,可以使用 setTitle (string $title) 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setTitle('Your title here!')
    ->send();

3. URL

要处理 embeds[0]['url'] 属性,可以使用 setUrl (string $url) 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setUrl('https://fake-url.com')
    ->send();

4. 描述

要处理 embeds[0]['description'] 属性,可以使用 setDescription (string $description) 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setDescription('Your description here!')
    ->send();

5. 颜色

要处理 embeds[0]['color'] 属性,可以使用 setColor (string $color) 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setColor('2450411')
    ->send();

注意:颜色值应为十进制数字。我们建议使用 此转换器 将十六进制值转换为十进制。

或者,您可以使用 setColor 方法传递一个字符串 参考颜色,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setColor('info')
    ->send();

可能的颜色引用包括:'info', 'error', 'notice', 'warning', 'success'

6. 字段

要将字段添加到 embeds[0]['fields'] 属性,可以使用 setField (string $name, string $value, bool $inline = null) 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setField('Name', 'Value')
    ->send();

// optional param $inline
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setField('Name', 'Value', true)
    ->send();

// multiple fields
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setField('Name 1', 'Value 1')
    ->setField('Name 2', 'Value 2', true)
    ->send();

注意:此方法可以多次调用,每次都会向数组中添加一个新字段。

7. 缩略图

要将字段添加到 embeds[0]['thumbnail'] 属性,可以使用 setThumbnail (string $thumbnail) 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setThumbnail('https://fake-thumb.com/thumb.png')
    ->send();

8. 图片

要将字段添加到 embeds[0]['image'] 属性,可以使用 setImage (string $image) 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setImage('https://fake-image.com/image.png')
    ->send();

9. 页脚

要将字段添加到 embeds[0]['footer'] 属性,可以使用 setFooter (string $text, ?string $icon_url = '') 方法,如下面的示例所示

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setFooter('Text to footer here!')
    ->send();

// optional param $icon_url
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setFooter('Text to footer here!', 'https://fake-icon.com/icon.png')
    ->send();

使用示例

use PhpChannels\DiscordWebhook\Discord;
...
Discord::message('https://discord.com/api/webhooks/your-webhook-path')
    ->setUsername('Application Bot')
    ->setAvatarUrl('https://fake-avatar.com/avatar.png')
    ->setContent('Content here!')
    ->setColor('2450411')
    ->setTitle('Title here!')
    ->setDescription('Description here!')
    ->send();

注意:上面的示例仅用于文档目的,并且在此文件中提供的所有方法都可以组合起来,以发送您需要的消息。