stil / phoebe
此包已被 废弃 并不再维护。没有建议的替代包。
关于此包最新版本(v0.2.0)没有可用的许可信息。
一个IRC机器人骨架。可以在单个进程内监听多个IRC网络。
v0.2.0
2013-09-06 08:43 UTC
Requires
- php: >=5.4
- phergie/phergie-irc-client-react: dev-master
- symfony/event-dispatcher: 2.3
Requires (Dev)
- phpunit/phpunit: 3.7.24
This package is not auto-updated.
Last update: 2021-02-05 20:34:08 UTC
README
Phoebe 是一个基于 Phergie 组件的 IRC 机器人骨架。与 Phergie 2 相比,其主要优势在于其灵活性,这可以通过 PHP 命名空间实现。
目录
使用示例
简单的 Phoebe 机器人
<?php require __DIR__.'/vendor/autoload.php'; use Phoebe\ConnectionManager; use Phoebe\Connection; use Phoebe\Event\Event; use Phoebe\Plugin\PingPong\PingPongPlugin; $freenode = new Connection(); $freenode->setServerHostname('irc.freenode.net'); $freenode->setServerPort(6667); $freenode->setNickname('Phoebe2'); $freenode->setUsername('Phoebe'); $freenode->setRealname('Phoebe'); // Create shortcut to EventDispatcher $events = $freenode->getEventDispatcher(); // Add PingPongPlugin to avoid being kicked from server $events->addSubscriber(new PingPongPlugin()); // Join #phoebe channel on startup $events->addListener('irc.received.001', function (Event $event) { $event->getWriteStream()->ircJoin('#phoebe'); }); $phoebe = new ConnectionManager(); $phoebe->addConnection($freenode); $phoebe->run();
多个 IRC 网络
<?php require __DIR__.'/vendor/autoload.php'; use Phoebe\Connection; use Phoebe\ConnectionManager; use Phoebe\Plugin\PingPong\PingPongPlugin; use Phoebe\Plugin\AutoJoin\AutoJoinPlugin; // First connection to Rizon // We enclose connections in functions to prevent namespace collisions $rizon = function () { $conn = new Connection(); $conn->setServerHostname('irc.rizon.net'); $conn->setServerPort(6667); $conn->setNickname('Phoebe4'); $conn->setUsername('Phoebe'); $conn->setRealname('Phoebe'); $events = $conn->getEventDispatcher(); // We'll join several channels on startup $autoJoin = new AutoJoinPlugin(); $autoJoin->addChannels( ['#channel1' => 'key', '#channel2', '#channel3'] ); $events->addSubscriber($autoJoin); // Answer on "hi" with "hello, nick" $events->addListener( 'irc.received.PRIVMSG', function ($event) { $msg = $event->getMessage(); if ($msg['params']['text'] == 'hi') { $event->getWriteStream()->ircPrivmsg( $event->getSource(), 'hello, '.$msg['nick'] ); } } ); return $conn; }; // Second connection to QuakeNet $qn = function () { $conn = new Connection(); $conn->setServerHostname('irc.quakenet.org'); $conn->setServerPort(6667); $conn->setNickname('Phoebe5'); $conn->setUsername('Phoebe'); $conn->setRealname('Phoebe'); return $conn; }; // Now create instance of ConnectionManager and add previously prepared connections $phoebe = new ConnectionManager(); $phoebe->addConnection($rizon()); $phoebe->addConnection($qn()); // You can also listen global events on ConnectionManager $events = $phoebe->getEventDispatcher(); // PingPongPlugin will prevent us from disconnecting from server $events->addSubscriber(new PingPongPlugin()); // We can start the bot now $phoebe->run();
事件对象
以下您可以检查在不同事件中哪些方法可用
方法/事件名称 | irc.received.* |
irc.sent |
connection.error |
---|---|---|---|
getMessage() |
yes | yes | yes |
getConnectionManager() |
yes | yes | yes |
getConnection() |
yes | yes | yes |
getTimers() |
yes | yes | yes |
getLogger() |
yes | yes | yes |
getWriteStream() |
yes | no | no |
插件
插件列表
Phoebe\Plugin\PingPong\PingPongPlugin
- 通过响应服务器的 PING 消息保持连接活跃Phoebe\Plugin\UserInfo\UserInfoPlugin
- 跟踪加入和离开频道的用户信息及其标志(+o, +v 等。)Phoebe\Plugin\AutoJoin\AutoJoinPlugin
- 允许您轻松配置启动时必须加入的频道Phoebe\Plugin\NickServ\NickServPlugin
- 启动时识别 NickServPhoebe\Plugin\Url\YouTubePlugin
- 显示在频道中提到的 YouTube 链接的信息Phoebe\Plugin\Url\SpotifyPlugin
- 显示在频道中提到的 Spotify 链接/URI 的信息
你知道值得分享的插件吗?通过拉取请求将它们添加到上面的列表中!(但保持类似的形式:类名加链接 - 描述)
创建自定义插件
插件类只需实现 getSubscribedEvents()
方法。
以下是一个简单的插件示例
<?php use Phoebe\Event\Event; use Phoebe\Plugin\PluginInterface; class HelloPlugin implements PluginInterface { public static function getSubscribedEvents() { return array( 'irc.received.PRIVMSG' => array('onMessage', 0), 'irc.received.NOTICE' => array('onMessage', 0) ); } public function onMessage(Event $event) { $msg = $event->getMessage(); if ($msg['params']['text'] === 'hello') { $event->getWriteStream()->ircPrivmsg( $msg['nick'], 'Hi!' ); } } }
使用计时器
在某些情况下,您需要延迟执行特定函数。感谢 Timers 类,在 Phoebe 中这非常容易。
下面您可以查看如何以短延迟重新连接到IRC。
$reconnect = function ($event) { $hostname = $event->getConnection()->getServerHostname(); $event->getLogger()->debug( "Connection to $hostname lost, attempting to reconnect in 15 seconds.\n" ); $event->getTimers()->setTimeout( function () use ($event) { // Use $event so we have access to required objects $event->getLogger()->debug("Reconnecting now...\n"); $event->getConnectionManager()->addConnection( $event->getConnection() ); }, 15 // Execute callback after 15 seconds ); }; // Reconnect when there is connection problem $events->addListener('irc.received.ERROR', $reconnect); $events->addListener('connect.error', $reconnect);