jbdabes/discord-webhooks

轻松使用Discord钩子。

1.1.0 2021-04-01 18:18 UTC

This package is auto-updated.

Last update: 2024-09-29 05:22:23 UTC


README

轻松使用Discord的钩子功能!

什么是钩子?

来自Discord的文档

Discord内置的钩子功能是一种将自动化消息和数据更新发送到您的服务器文本频道的方法。想象一下,它们就像那些你曾经喜欢用来在银行存钱并看着消失的时髦气动管,但是你实际上是将消息从另一个平台发送到Discord,而不是永远看不到你的钱。

这个库的功能是什么?

以前,为了将一些数据发送到Discord钩子,您可能需要手动编写JSON代码,或自行构建数据结构,然后用curl或其他方法发送,等等。这样的代码对开发者不友好,并且可能让初学者感到困惑。

这个库的目的是简化钩子的使用,让每个人都能利用它们的强大功能。

以下是一个示例

$webhookURL = "https://discordapp.com/api/webhooks/my-webhook-url";

$jsonData = [
    "embeds" => [
        [
            "title" => "My Webhook Title",
            "description" => "My Webhook Description",
            "url" => "https://playersquared.com",
            "color" => 4433631,
            "timestamp" => date("Y-m-d\\TH:i:s.u\\Z"),
            "thumbnail" => [
                "url" => "https://cdn.discordapp.com/embed/avatars/0.png",
            ],
            "author" => [
                "name" => "JB",
                "url" => "https://playersquared.com/forums/members/jb/",
                "icon_url" => "https://cdn.discordapp.com/embed/avatars/4.png",
            ]
        ]
    ]
];

$ch = curl_init($webhookURL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonData));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);

现在,使用Discord-Webhooks的相同代码

$webhook = new \DiscordWebhooks\Webhook("https://discordapp.com/api/webhooks/my-webhook-url");
$embed   = new \DiscordWebhooks\Embeds\Embed();

$embed->setTitle("My Webhook Title")
    ->setDescription("My Webhook Description")
    ->setUrl("https://playersquared.com")
    ->setColor("#43a6df")
    ->setTimestamp($webhook->currentTimestamp());

$embed->thumbnail()->setUrl("https://cdn.discordapp.com/embed/avatars/0.png");
$embed->author()->setName("JB")
    ->setUrl("https://playersquared.com/forums/members/jb/")
    ->setIconUrl("https://cdn.discordapp.com/embed/avatars/4.png");

$webhook->embeds()->add($embed);
$webhook->send();

使用Discord的钩子从未如此简单。

我该如何使用这个库?

您可以使用composer将Discord-Webhooks安装到PHP项目中

composer require jbdabes/discord-webhooks