korbeil / discord-php
与语音和文字服务 Discord 交互的非官方 API。
Requires
- php: ^5.6|^7.0
- cache/array-adapter: >=0.4.0
- guzzlehttp/guzzle: ~5.3|>=6.0
- illuminate/support: ^4.0|^5.0|^6.0|^7.0|^8.0
- monolog/monolog: >=1.19
- nesbot/carbon: >=1.18
- ratchet/pawl: 0.*
- react/datagram: 1.*
- react/partial: >=2.0
- react/socket: >=0.4
- symfony/options-resolver: ^2.7|>=3.0
- trafficcophp/bytebuffer: >=0.3
- wyrihaximus/react-guzzle-psr7: >=1.0
Requires (Dev)
- symfony/var-dumper: ^2.7|^3.0
Suggests
- ext-event: For a faster, and more performant loop
- ext-libev: For a faster, and more performant loop
- ext-libevent: For a faster, and more performant loop. Preferred
- cache/apc-adapter: Required to use the APC cache driver
- cache/memcache-adapter: Required to use the Memcache cache driver
- cache/memcached-adapter: Required to use the Memcached cache driver
- cache/redis-adapter: Required to use the Redis cache driver
This package is auto-updated.
Last update: 2024-09-08 16:11:11 UTC
README
官方 Discord REST、网关和语音 API 的包装器。文档可在此处找到 http://discord-php.github.io/DiscordPHP,尽管目前有限,还有类参考。欢迎在上述 Discord 服务器中提问。
为了测试和稳定性,如果您能将我们的测试机器人添加到您的服务器,我们将不胜感激。我们不会存储任何数据 - 机器人只是闲置且不与任何人互动,用于测试与大量公会的大规模稳定性。您可以通过 这里 邀请机器人。
缓存接口(实验性)
警告 此分支包含实验性功能,请勿在生产环境中使用!有关如何设置的更多信息,请参阅 Wiki 页面。
常见问题解答
- 我能否在 web 服务器(例如 Apache、nginx)上运行 DiscordPHP?
- 不,DiscordPHP 只能在 CLI 上运行。如果您想为您的机器人提供一个界面,可以将 react/http 集成到您的机器人中,并通过 CLI 运行。
- PHP 内存不足?
- 尝试使用
ini_set('memory_limit', '-1');
无限制 PHP 内存。
- 尝试使用
入门指南
在开始使用此库之前,您必须了解 PHP 的工作原理,您需要了解事件循环和承诺的工作原理。这是开始前的基本要求。没有这些知识,您只会感到痛苦。
要求
推荐扩展
- 其中一个
ext-uv
(推荐)、ext-ev
或ext-event
以获得更快、更高效的循环。 - 如果处理非拉丁字符,需要
ext-mbstring
。
语音要求
- 64 位 PHP
ext-sodium
- FFmpeg
Windows 和 SSL
遗憾的是,Windows 上的 PHP 没有访问 Windows 证书存储库。这是一个问题,因为 TLS 被使用,因此证书验证被应用(关闭此选项不是一种选择)。
您将通过脚本在没有任何错误的情况下立即退出一个循环来注意到这个问题。
因此,使用此库的用户需要从 cURL 网站下载一个 证书颁发机构提取。
必须将 caextract 的路径设置为 php.ini
中的 openssl.cafile
。
安装 DiscordPHP
DiscordPHP 使用 Composer 安装。
- 运行
composer require team-reflex/discord-php
。这将安装最新稳定版本。- 如果您愿意,也可以通过运行
composer require team-reflex/discord-php dev-master
来安装开发分支。
- 如果您愿意,也可以通过运行
- 在主文件顶部包含 Composer 自动加载文件
include __DIR__.'/vendor/autoload.php';
- 制作一个机器人!
基本示例
<?php include __DIR__.'/vendor/autoload.php'; use Discord\Discord; use Discord\Parts\Channel\Message; use Discord\WebSockets\Intents; use Discord\WebSockets\Event; $discord = new Discord([ 'token' => 'bot-token', 'intents' => Intents::getDefaultIntents() // | Intents::MESSAGE_CONTENT, // Note: MESSAGE_CONTENT is privileged, see https://dis.gd/mcfaq ]); $discord->on('ready', function (Discord $discord) { echo "Bot is ready!", PHP_EOL; // Listen for messages. $discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) { echo "{$message->author->username}: {$message->content}", PHP_EOL; // Note: MESSAGE_CONTENT intent must be enabled to get the content if the bot is not mentioned/DMed. }); }); $discord->run();
更多示例请查看示例文件夹。
文档
最新版本的文档可以在这里找到。社区贡献的教程可以在维基上找到。
贡献
我们欢迎贡献。但是,请确保您遵循我们的编码标准(PSR-4 自动加载和自定义样式)。请在提交拉取请求之前运行 php-cs-fixer
,通过运行 composer run-script cs
。
许可证
MIT 许可证,© David Cole 及其他贡献者 2016 年至今。