hesmatt/discord-hooker

此包最新版本(v1.0.0)没有提供许可证信息。

轻量级的PHP Discord webhook客户端库

v1.0.0 2023-07-15 20:02 UTC

This package is auto-updated.

Last update: 2024-09-15 22:28:30 UTC


README

Discord Hooker是一个轻量级的PHP Discord webhook客户端库(>=7.0)

安装

您只需通过Composer使其项目依赖即可 👀

composer require hesmatt/discord-hooker

用法

使用Discord Hooker非常简单,以下是一个只包含消息的示例。

基本用法

use HesMatt\DiscordHooker\Client\Webhook;

$webhook = new Webhook('Your webhook _URL_');

$webhook->setMessage('I am a Discord Hooker');

$webhook->send();

结果

我们还可以设置用户名和图像,您也可以在Discord的Webhook设置中这样做,但这种方式可以“强制”使用不同的设置。

让我们修改一下代码

use HesMatt\DiscordHooker\Client\Webhook;
use HesMatt\DiscordHooker\Dto\Embed\Embed;

$webhook = new Webhook('Your webhook _URL_');

$webhook->setMessage('I am a Discord Hooker');
$webhook->setUsername('Hooker');
$webhook->setAvatar('The url of your image'); //Note that this always has to be an URL, not a file!

$webhook->send();

结果

使用嵌入

您还可以构建嵌入并发送它们,这可能是您最常用的功能,让您的消息看起来很酷 🌶️

构建和添加嵌入并不困难,而且最好的是您可以添加尽可能多的嵌入(或者Discord允许的那么多)

让我们先构建一个嵌入,嵌入可以像标题一样简单。

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setTitle('I am a test embed');

//The description is optional and you don't have to set it, but I wanted to mention is as well :)
$embed->setDescription('I am  test embed description');

我们现在需要发送它。

use HesMatt\DiscordHooker\Client\Webhook;
use HesMatt\DiscordHooker\Dto\Embed\Embed;

$webhook = new Webhook('Your webhook _URL_');
$embed = new Embed(); //Let's assume we have the different parts we've already built.

//You can use all of the settings for $webhook from earlier, to make the code shorter I won't be typing them again from now on.

$webhook->addEmbed($embed);

$webhook->send();

结果

嵌入有很多设置,我将在这里列出它们,不包含“结果”部分,以免使内容过长。

给嵌入添加页脚

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setFooter('The footer text', 'The footer image URL');

//Setting the time part of footer, we can either use
$embed->setTimestamp(new DateTimeImmutable());
//or (To use current time)
$embed->setTimestampNow();

设置颜色

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setColor('327424'); //Discord is using an int value of the color.

设置缩略图

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setThumbnail('Url of the thumbnail image');

添加作者

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setAuthor('Name of Author','Icon of author','Url of author');

添加字段

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->addField('Text','Value',false); //The last parameter is whether you want the field to be inlined or no.

我们可以将这些功能组合在一起,注意至少需要一个字段是必须的。因此您需要实际的字段或标题/描述。

贡献

贡献总是受欢迎的!

许可证

MIT