whisller/irc-bot-bundle

此包已被弃用,不再维护。没有推荐替代包。

Symfony IrcBotBundle

安装: 480

依赖者: 0

建议者: 0

安全: 0

星标: 24

关注者: 7

分支: 3

公开问题: 1

类型:symfony-bundle

1.0 2012-10-31 21:04 UTC

This package is not auto-updated.

Last update: 2022-09-17 04:01:02 UTC


README

Build Status

安装

  1. 下载IrcBotBundle
  2. 启用该包
  3. 配置服务器、用户
  4. 启动IrcBot!

步骤 1: 下载IrcBotBundle

{
    "require": {
        "whisller/irc-bot-bundle": "*"
    }
}

现在让composer运行以下命令下载该包:

$ php composer.phar update whisller/irc-bot-bundle

Composer会将包安装到您的项目的vendor/whisller目录。

步骤 2: 启用包

在内核中启用该包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Whisnet\IrcBotBundle\WhisnetIrcBotBundle(),
    );
}

步骤 3: 配置服务器、用户

基本配置

whisnet_irc_bot:
  user: ~
  channels: ["#test-irc"]

高级配置

whisnet_irc_bot:
    connection_class: Whisnet\IrcBotBundle\Connection\Socket
    host: irc.freenode.net
    port: 6667
    command_prefix: !bot
    user:
        username: IrcBotBunle
        mode: 8
        servername: IrcBotBundle
    channels: ["#test-irc", "#test-other-irc"]

步骤 4: 启动IrcBot!

$ php app/console irc:launch

编写自己的机器人命令。

  1. 编写监听器
  2. 注册您的监听器
  3. 使用您的命令

当您想编写自己的命令时,非常简单,因为IrcBotBundle基于事件调度器。

所以您需要做的一件事是捕获命令事件,并处理它。

最好的学习方法就是看它是如何工作的。所以让我们编写一个简单的命令,它会说“Hi {user}!”

步骤 1: 编写监听器

<?php
namespace Acme\EventListener;

use Whisnet\IrcBotBundle\EventListener\Plugins\Commands\CommandListener;
use Whisnet\IrcBotBundle\Event\BotCommandFoundEvent;

class HelloListener extends CommandListener
{
    public function onCommand(BotCommandFoundEvent $event)
    {
        // get list of arguments passed after command
        $args = $event->getArguments();

        $msg = 'Hi, '.(isset($args[0]) ? $args[0] : 'nobody').' !';

        // write to the current channel
        $this->sendMessage(array($event->getChannel()), $msg);
    }
}

步骤 2: 注册您的监听器

<service id="whisnet_irc_bot.bot_command_hello_listener" class="Acme\EventListener\HelloListener">
    <tag name="whisnet_irc_bot.bot_command" command="hello" help="Say hello to user" arguments="(username)"/>
    <tag name="kernel.event_listener" event="whisnet_irc_bot.bot_command_hello" method="onCommand"/>
</service>

如你所见,事件名称是 "whisnet_irc_bot.bot_command_hello",包正在监听来自服务器的 PRIVMSG 消息,然后在其中搜索定义在 "whisnet_irc_bot.command_prefix" 中的字符串。

如果找到了 "whisnet_irc_bot.command_prefix" 字符串,则包会尝试解析其后的所有内容以读取命令名称和参数,并将它们传递给 "BotCommandFoundEvent"。

例如,"!bot hello whisller" 将被解析为:

  • command: "hello"
  • arg_0: "whisller"

然后它将触发一个事件 "whisnet_irc_bot.bot_command_hello"。

步骤 3: 使用您的命令

!bot hello whisller

事件列表

IrcBotBundle 事件

whisnet_irc_bot.post_connection

此事件在建立与服务器的连接后触发。你可以通过 Whisnet\IrcBotBundle\EventListener\Plugins\Core\LoadUserCoreListener 类中的示例来了解如何使用此事件。

服务器事件

包基于服务器消息触发事件。例如:

whisnet_irc_bot.irc_command_PRIVMSG
whisnet_irc_bot.irc_command_MODE
whisnet_irc_bot.irc_command_372
whisnet_irc_bot.irc_command_NOTICE
whisnet_irc_bot.irc_command_391

等等。

事件名称非常简单,它基于前缀 "whisnet_irc_bot.irc_command_" 和服务器发送的消息类型,例如 "PRIVMSG"。因此,您可以监听服务器发送的所有事件,以扩展自己的包。

所有命令列表都可以在 http://tools.ietf.org/html/rfc2812 上找到

机器人命令

如果Whisnet\IrcBotBundle\EventListener\Irc\Messages\PrivMsgListener::onData判断在频道上写入的消息是irc机器人命令,则它会触发一个事件 "whisnet_irc_bot.bot_command_COMMANDNAME",例如 whisnet_irc_bot.bot_command_time,whisnet_irc_bot.bot_command_seen。