supercrafter333/discordwebhooksx

1.2.0 2023-09-03 14:35 UTC

This package is not auto-updated.

Last update: 2024-09-30 22:36:06 UTC


README


这是 CortexPE 的 DiscordWebhookAPI 库的更好分支。

A PocketMine-MP 的 Virion,可以轻松通过 Discord Webhooks 发送消息

用法

安装很简单,您可能可以在这里找到编译好的 phar,或者将 virion 本身集成到您的插件中。

这个 virion 完全是面向对象的。所以,要使用它,您必须导入 Webhook 对象,Message 对象和可选的 Embed 对象(如果需要)

基本用法

导入类

您需要导入这些类,以便在代码中轻松使用。

<?php

use supercrafter333\DiscordWebhooksX\Message;
use supercrafter333\DiscordWebhooksX\Webhook;
use supercrafter333\DiscordWebhooksX\Embed; // optional

构建 Discord Webhook 对象

您需要 Webhook 的 URL。有关如何在 Discord 文本通道上创建 Discord Webhook 的更多信息,请点击此处

$webHook = new Webhook("YOUR WEBHOOK URL");

构建 Discord Message 对象

您需要为每条要发送的消息创建一个新的 Message 对象... 您可以使用不同的消息对象来分别使用 Webhook,而这个对象 不依赖于 Webhook 对象。它是独立的,并且可以单独工作。

$msg = new Message();
$msg->setUsername("USERNAME"); // optional
$msg->setAvatarURL("https://cortexpe.xyz/utils/kitsu.png"); // optional
$msg->setContent("INSERT TEXT HERE"); // optional. Maximum length is 2000 characters, the limit is set by discord, therefore it is not hardcoded within this API

发送消息

现在您可以轻松地将消息发送到 Webhook!🎉 这将在服务器的 AsyncPool 上安排一个新的 AsyncTask,以防止阻塞主线程。但是请注意,您不能发送空白消息。这样做只会产生来自 Discord 的错误。

$webHook->send($msg);

这有多简单? ^-^ 现在让我们来谈谈更高级和酷的东西,即 Embeds!

Embeds

在发送消息之前,您可能想添加一个 embed。一条消息可以有多个 embeds!您只需构建一个 embed,然后使用 Message->addEmbed() 方法将其添加到消息对象中。

$embed = new Embed();

现在,embed 必须包含一些内容才能正常工作,因此我们将添加一个标题(可选)和一个描述(可选)。所有字段都是可选的,但它至少应该包含一个字段,否则它将拒绝将其添加到消息中

$embed->setTitle("Embed Title Here");
$embed->setDescription("A very awesome description");

我们甚至可以设置页脚文本!embed 底部部分的文本...

$embed->setFooter("Erin is kawaii UwU");

或者甚至,向页脚添加一个图标...

$embed->setFooter("Erin is kawaii UwU","https://cortexpe.xyz/utils/kitsu.png");

现在 embed 已经构建并且有有效的内容,我们需要将其添加到 Message 对象中... 我们将使用 Message->addEmbed() 方法来做这件事。

$msg->addEmbed($embed);

您甚至可以使用以下语句启用特定提及:Message->getAllowedMentions。您需要的只是 discord 雪花/ids。

$msg->getAllowedMentions()->addUser($userId1, $userId2); // Only the two users corresponding with these two ids will be mentionend
$msg->getAllowedMentions()->addRole($roleId1, $roleId2); // Now also all the people with $roleId1 and $roleId2 will be mentioned

但是,如果您想从该消息中抑制所有提及,则可以使用以下方法。

$msg->getAllowedMentions()->suppressAll();

这就是 API 的基本用法。要了解更多信息,您可以自己阅读 API 的源代码(代码简单且具有说明性)或使用您喜欢的 IDE 来索引它。 :3

用于测试此 API 的示例代码

// Construct a discord webhook with its URL
$webHook = new Webhook("YOUR WEBHOOK URL");

// Construct a new Message object
$msg = new Message();

$msg->setUsername("USERNAME");
$msg->setAvatarURL("https://cortexpe.xyz/utils/kitsu.png");
$msg->setContent("INSERT TEXT HERE");
$msg->suppressAll();

// Create an embed object with #FF0000 (RED) as the embed's color and "EMBED 1" as the title
$embed = new Embed();
$embed->setTitle("EMBED 1");
$embed->setColor(0xFF0000);
$msg->addEmbed($embed);

$embed = new Embed();
$embed->setTitle("EMBED 2");
$embed->setColor(0x00FF00);
$embed->setAuthor("AUTHOR", "https://supercrafter333.xyz", "https://cortexpe.xyz/utils/kitsu.png");
$embed->setDescription("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
$msg->addEmbed($embed);

$embed = new Embed();
$embed->setTitle("EMBED 3");
$embed->setColor(0x0000FF);
$embed->addField("FIELD ONE", "Some text here");
$embed->addField("FIELD TWO", "Some text here", true);
$embed->addField("FIELD THREE", "Some text here", true);
$embed->setThumbnail("https://cortexpe.xyz/utils/kitsu.png");
$embed->setImage("https://cortexpe.xyz/utils/kitsu.png");
$embed->setFooter("Erin is kawaii UwU","https://cortexpe.xyz/utils/kitsu.png");
$msg->addEmbed($embed);

$webHook->send($msg);

智能示例

$webhook = new \supercrafter333\DiscordWebhooksX\Webhook("url");
$webhook->send(
\supercrafter333\DiscordWebhooksX\Message::create([
    \supercrafter333\DiscordWebhooksX\Embed::create()
    ->setTitle("Smart Embed")
    ->setColor(0x0000FF)
    ->addField("FIELD ONE", "Some text here")
    ->addField("FIELD TWO", "Some text here", true)
    ->addField("FIELD THREE", "Some text here", true)
    ->setThumbnail("https://cortexpe.xyz/utils/kitsu.png")
    ->setImage("https://cortexpe.xyz/utils/kitsu.png")
    ->setFooter("Erin is kawaii UwU","https://cortexpe.xyz/utils/kitsu.png");
])
);

这个 API 是由 CortexPE 用 ❤️ 制作的,祝您享受!~ :3