judahnator / discord-http-wrapper
Discord HTTP API的基本封装
Requires
- php: >=7.1
- guzzlehttp/guzzle: ^6.2
- vlucas/phpdotenv: ^2.4
Requires (Dev)
- phpunit/phpunit: ^6.1
README
这个库作为您机器人与Discord HTTP REST API之间的干净简单的接口。
由于这个库正在积极开发中,有些事情可能会出错,但请不要害怕提出问题。我会在有时间的时候处理它们。
安装说明
此软件包可以通过composer安装。
composer require judahnator/discord-http-wrapper
此库依赖于vlucas/phpdotenv库来保存其配置值。其中最重要的是您的机器人令牌。如果您的项目也使用此库,只需在.env文件中包含BOT_TOKEN=your-bots-token
。否则,在/vendor/judahnator/discord-http-wrapper/.env
文件中设置该值。此文件默认不存在,但在同一目录中有一个.env.example
模板。
如果您还没有令牌,可以从Discord开发者网站获取。您的令牌也必须被授权才能进行RESTful API请求。
基本用法
judahnator\DiscordHttpWrapper\Bot::class
- 提供关于机器人一般信息的单例类。例如
$Bot = judahnator\DiscordHttpWrapper\Bot::Instance();
$Bot->id; // 123456
$Bot->username; // TestBot
$Bot->guilds; // An array of guilds the bot has access to
judahnator\DiscordHttpWrapper\Guild::class
- 用于与公会交互的类。
$Guild = judahnator\DiscordHttpWrapper\Guild::find(1234567890);
$Guild->id; // 1234567890
$Guild->name; // TestGuild
$Guild->channels; // An array of channels belonging to this guild
judahnator\DiscordHttpWrapper\Channel::class
- 用于与频道交互的类。
$Channel = judahantor\DiscordHttpWrapper\Channel::find(1234567890);
$Channel->id; // 1234567890
$Channel->name; // TestChannel
$Channel->guild; // The guild the channel belongs to
$Channel->messages; // The 50 most recent messages in this channel
$Channel->sendMessage("Foo bar baz"); // Sends message, returns the message object
judahnator\DiscordHttpWrapper\Message::class
- 用于与消息交互的类
$Message = judahnator\DiscordHttpWrapper\Message::find($ChannelID,$MessageID);
$Message->id; // Message ID
$Message->content; // The content of the message
$Message->author; // The author object
$Message->reply("Foo bar baz"); // Replies to the author of the current message
judahnator\DiscordHttpWrapper\Message::send($ChannelID,"Foo bar baz"); // Sends a message to a channel
judahnator\DiscordHttpWrapper\Author::class
- 用于与作者交互的类
$Author = judahnator\DiscordHttpWrapper\Author::find(1234567890);
$Author->id; // 1234567890
$Author->username; // TestAuthor
一般说明
正如其名所示,此库与Discord的HTTP API交互。这意味着即使在低延迟连接上,此库的运行速度也将远慢于通过websockets与Discord交互的其他库。
同理,此库也尊重Discord提供的速率限制头。撰写本文时,限制约为每秒1.5个请求。这意味着在更受欢迎的服务器中,遍历消息将会很慢。如果您需要速度,我建议使用TeamReflex的DiscordPHP库。
贡献
我欢迎问题、分支、拉取请求等。只是请为了世界上所有美好的事物,编写干净的代码,并留下大量的注释。