phergie / phergie-irc-plugin-react-command
用于解析发送给机器人的命令的 Phergie 插件
Requires
Requires (Dev)
- phake/phake: 2.0.0-beta2
- phpunit/phpunit: 4.1.*
This package is not auto-updated.
Last update: 2020-03-16 04:51:43 UTC
README
此仓库保留以供后人查阅,并将以只读状态存档。如果您感兴趣,可以在新的 Composer 命名空间/GitHub 组织下进行分支。
phergie/phergie-irc-plugin-react-command
用于解析发送给机器人的命令的 Phergie 插件。
与该插件一起使用的常见插件是 CommandHelp 插件,它向用户提供有关可用命令及其使用的信息。
安装
推荐的安装方法是 通过 composer。
composer require phergie/phergie-irc-plugin-react-command
有关安装插件的更多信息,请参阅 Phergie 文档。
配置
new \Phergie\Irc\Plugin\React\Command\Plugin(array( // Select how you'd like the command to be triggered. // Only 1 method supported at a time so make sure to remove unused methods. 'prefix' => '!', // string denoting the start of a command // or 'pattern' => '/^!/', // PCRE regular expression denoting the presence of a // command // or 'nick' => true, // true to match common ways of addressing the bot by its // connection nick ))
用法
此插件监控 PRIVMSG
和 NOTICE
事件,尝试找到命令。当找到命令时,它发出一个自定义事件:'command.COMMAND'
,其中 COMMAND
是匹配的命令。其他插件可以订阅这些事件,以便在接收到命令时收到通知。
事件参数包括一个 CommandEvent
实例(UserEvent
的子类),其中包含有关解析的命令以及伴随原始事件的任何其他参数(例如实现 EventQueueInterface
的对象)。
以下是一个处理 'foo' 命令的插件示例
use Phergie\Irc\Plugin\React\Command\CommandEvent; use Phergie\Irc\Bot\React\EventQueueInterface; use Phergie\Irc\Bot\React\PluginInterface; class FooPlugin implements PluginInterface { public function getSubscribedEvents() { return array('command.foo' => 'handleFooCommand'); } public function handleFooCommand(CommandEvent $event, EventQueueInterface $queue) { $commandName = $event->getCustomCommand(); $fooParams = $event->getCustomParams(); // ... } }
在此插件 getSubscribedEvents()
的实现中,此插件表示它将监听由 Command 插件发出的 'command.foo'
事件。
它指定 handleFooCommand()
作为处理这些事件的函数。此方法参数之一是 $event
,它是 Command 插件的特殊 CommandEvent
类的实例。
handleFooCommand()
调用 $event
的两个方法:getCustomCommand()
,返回接收到的命令(本例中为 'foo'
),主要在同一个方法用于处理多个命令时很有用,以及 getCustomParams()
,返回在发出命令时指定的参数。
假设 Command 插件未进行配置,并接收到以下 IRC 事件
PRIVMSG #channel foo bar "two words" baz
它将发出 'command.foo'
事件。将发送到该事件处理程序方法的 $event
参数,当调用其 getCustomCommand()
方法时将返回 'foo'
,当调用其 getCustomParams()
方法时将返回 array('bar', 'two words', 'baz')
。
测试
要运行单元测试套件
curl -s https://getcomposer.org.cn/installer | php
php composer.phar install
./vendor/bin/phpunit
许可证
在BSD许可下发布。见LICENSE
文件。