此包已被弃用且不再维护。未建议替代包。

Discord斜杠命令的HTTP服务器

v2.0.0-beta 2022-07-19 18:22 UTC

This package is auto-updated.

Last update: 2023-02-19 19:43:47 UTC


README

PHP服务器和客户端,用于Discord斜杠命令。请在使用此库之前阅读Discord斜杠命令文档

如果您已经使用DiscordPHP v7+,则不需要此DiscordPHP-Slash库。更多信息请参阅:[https://github.com/discord-php/DiscordPHP/wiki/Slash-Command](https://github.com/discord-php/DiscordPHP/wiki/Slash-Command)

警告

Discord斜杠命令仍处于测试阶段。这些命令的工作方式可能会随时更改,恕不另行通知。同样,也适用于此库。

要求

  • PHP >=7.3
  • Composer

安装

$ composer require discord-php/slash

使用方法

库中有两个“客户端”

  • Discord\Slash\RegisterClient 用于向Discord注册命令。
  • Discord\Slash\Client 用于监听HTTP请求并响应。

Discord\Slash\RegisterClient

您应该阅读有关如何在Discord开发者文档中注册命令的说明,特别是创建和更新命令时的 options 数组。

<?php

include 'vendor/autoload.php';

use Discord\Slash\RegisterClient;

$client = new RegisterClient('your-bot-token-here');

/// GETTING COMMANDS

// gets a list of all GLOBAL comamnds (not guild-specific)
$commands = $client->getCommands();
// gets a list of all guild-specific commands to the given guild
$guildCommands = $client->getCommands('guild_id_here');
// gets a specific command with command id - if you are getting a guild-specific command you must provide a guild id
$command = $client->getCommand('command_id', 'optionally_guild_id');

/// CREATING COMMANDS

// creates a global command
$command = $client->createGlobalCommand('command_name', 'command_description', [
    // optional array of options
]);

// creates a guild specific command
$command = $client->createGuildSpecificCommand('guild_id', 'command_name', 'command_description', [
    // optional array of options
]);

/// UPDATING COMMANDS

// change the command name etc.....
$command->name = 'newcommandname';
$client->updateCommand($command);

/// DELETING COMMANDS

$client->deleteCommand($command);

Discord\Slash\Client

有两种方法可以设置slash客户端

  • Webhook方法
  • 网关方法(已弃用)

请阅读这两部分,因为它们都包含重要信息,并且都有优点/缺点。

Webhook方法

您已注册命令后,可以设置一个HTTP服务器以监听来自Discord的请求。

有几种方法可以设置一个HTTP服务器以监听请求

  • 内置的ReactPHP HTTP服务器。
  • 使用内置的ReactPHP HTTP服务器(无HTTPS)并使用Apache或nginx作为反向代理(推荐)。
  • 使用Apache或nginx等外部HTTP服务器。

无论您选择哪个路径,服务器都必须使用HTTPS进行保护 - Discord不接受普通HTTP。

目前为了测试,我正在运行内置的ReactPHP HTTP服务器在端口8080上,没有HTTPS。然后我有一个带有HTTPS的Apache2 Web服务器,充当ReactPHP服务器的反向代理。以下是在Linux上设置此示例。

设置基本的Client

<?php

include 'vendor/autoload.php';

use Discord\Slash\Client;
use Discord\Slash\Parts\Interaction;
use Discord\Slash\Parts\Choices;

$client = new Client([
    // required options
    'public_key' => 'your_public_key_from_discord_here',

    // optional options, defaults are shown
    'uri' => '0.0.0.0:80', // if you want the client to listen on a different URI
    'logger' => $logger, // different logger, default will write to stdout
    'loop' => $loop, // reactphp event loop, default creates a new loop
    'socket_options' => [], // options to pass to the react/socket instance, default empty array
]);

// register a command `/hello`
$client->registerCommand('hello', function (Interaction $interaction, Choices $choices) {
    // do some cool stuff here
    // good idea to var_dump interaction and choices to see what they contain

    // once finished, you MUST either acknowledge or reply to a message
    $interaction->acknowledge(); // acknowledges the message, doesn't show source message
    $interaction->acknowledge(true); // acknowledges the message and shows the source message

    // to reply to the message
    $interaction->reply('Hello, world!'); // replies to the message, doesn't show source message
    $interaction->replyWithSource('Hello, world!'); // replies to the message and shows the source message

    // the `reply` methods take 4 parameters: content, tts, embed and allowed_mentions
    // all but content are optional.
    // read the discord developer documentation to see what to pass to these options:
    // https://discord.com/developers/docs/resources/channel#create-message
});

// starts the ReactPHP event loop
$client->run();

请注意,您必须在3秒内确认交互,否则Discord将取消交互。如果您要进行耗时操作,请在准备好后调用acknowledge()函数,并使用sendFollowUpMessage()发送后续消息。

此库仅处理slash命令,不支持与Discord的其他交互,例如创建频道、发送其他消息等。您可以将DiscordPHP库与此库结合使用,以拥有更多工具集。您只需确保两个客户端共享相同的ReactPHP事件循环。以下是一个示例:

<?php

include 'vendor/autoload.php';

// make sure you have included DiscordPHP into your project - `composer require team-reflex/discord-php`

use Discord\Discord;
use Discord\Slash\Client;
use Discord\Slash\Parts\Interaction;
use Discord\Slash\Parts\Choices;

$discord = new Discord([
    'token' => '##################',
]);

$client = new Client([
    'public_key' => '???????????????',
    'loop' =>  $discord->getLoop(),
]);

$client->linkDiscord($discord, false); // false signifies that we still want to use the HTTP server - default is true, which will use gateway

$discord->on('ready', function (Discord $discord) {
    // DiscordPHP is ready
});

$client->registerCommand('my_cool_command', function (Interaction $interaction, Choices $choices) use ($discord) {
    // there are a couple fields in $interaction that will return DiscordPHP parts:
    $interaction->guild;
    $interaction->channel;
    $interaction->member;

    // if you don't link DiscordPHP, it will simply return raw arrays

    $discord->guilds->get('id', 'coolguild')->members->ban(); // do something ?
    $interaction->acknowledge();
});

$discord->run();

在PHP-CGI/PHP-FPM之后运行

要在CGI/FPM和web服务器之后运行,需要kambo/httpmessage包。

$ composer require kambo/httpmessage

语法与使用ReactPHP http服务器运行时完全相同,只是最后一行不同。

<?php

include 'vendor/autoload.php';

use Discord\Slash\Client;

$client = new Client([
    'public_key' => '???????',
    'uri' => null, // note the null uri - signifies to not register the socket
]);

// register your commands like normal
$client->registerCommand(...);

// note the difference here - runCgi instead of run
$client->runCgi();

请注意,常规的DiscordPHP客户端在CGI或FPM上无法运行,因此效果可能会有所不同。

配置Apache2作为反向代理

假设您已经安装了Apache2,并且服务器上已有SSL证书。

  1. 启用所需的Apache模块。
$ sudo a2enmod proxy
$ sudo a2enmod proxy_http
$ sudo a2enmod ssl
  1. 创建一个新的站点或修改现有的默认站点以监听端口443
$ vim /etc/apache2/sites-available/000-default.conf # default site

# change contents to the following
<VirtualHost *:443> # listen on 443
        ProxyPreserveHost       On                              # preserve the host header from Discord
        ProxyPass               /       http://127.0.0.1:8080/  # pass-through to the HTTP server on port 8080
        ProxyPassReverse        /       http://127.0.0.1:8080/

        SSLEngine               On                              # enable SSL
        SSLCertificateFile      /path/to/ssl/cert.crt           # change to your cert path
        SSLCertificateKeyFile   /path/to/ssl/cert.key           # change to your key path
</VirtualHost>
  1. 重启Apache - 下面的代码适用于基于Debian的系统。
$ sudo service apache2 restart

网关方法(已弃用)

从DiscordPHP v7.0.0版本开始,slash命令已集成到主库中。您不再需要此DiscordPHP-Slash库!请在此处阅读更多信息:https://github.com/discord-php/DiscordPHP/blob/master/V7_CONVERSION.md#slash-commands

客户端可以使用常规的DiscordPHP客户端连接并监听网关上的交互。要使用此方法,请确保您的Discord开发者应用程序中没有设置交互端点。

请确保您的项目中已包含DiscordPHP(截至撰写本文时,仅支持DiscordPHP 6.x)。

$ composer require team-reflex/discord-php

然后您可以创建客户端并将它们链接起来。

<?php

include 'vendor/autoload.php';

use Discord\Discord;
use Discord\Slash\Client;

$discord = new Discord([
    'token' => 'abcd.efdgh.asdas',
]);

$client = new Client([
    'loop' => $discord->getLoop(), // Discord and Client MUST share event loops
]);

$client->linkDiscord($discord);

$client->registerCommand(...);

$discord->run();

网关方法设置起来更容易,您无需担心SSL证书。

许可证

本软件根据MIT许可证授权,可在LICENSE.md文件中查看。

致谢