1POE::Component::IRC::CoUoskebrooCko:n:tBraisbiuctBePodOtE(P:3e:)rClomDpoocnuemnetn:t:aItRiCo:n:Cookbook::BasicBot(3)
2
3
4

NAME

6       POE::Component::IRC::Cookbook::BasicBot - A basic IRC bot
7

SYNOPSIS

9       This a very basic bot that connects to IRC, joins a few channels, and
10       announces its arrival.
11

DESCRIPTION

13       We start off quite simply:
14
15        #!/usr/bin/env perl
16
17        use strict;
18        use warnings;
19
20       Then we "use" the stuff we're going to...well, use. "::State" is a
21       subclass which keeps track of state information related to channels and
22       nicknames. It is needed by the "AutoJoin" plugin which takes care of
23       keeping us on our channels.
24
25        use POE;
26        use POE::Component::IRC::State;
27        use POE::Component::IRC::Plugin::AutoJoin;
28
29       Next up is our POE session. We create it and list our event handlers.
30       We then start the POE kernel.
31
32        POE::Session->create(
33            package_states => [
34                main => [ qw(_start irc_join) ]
35            ]
36        );
37
38        $poe_kernel->run();
39
40       Now all we have to do is write the handlers for "_start" and
41       "irc_join".  In "_start", we create our IRC component, add an
42       "AutoJoin" plugin, register for the "irc_join" event, and connect to
43       the IRC server.
44
45        sub _start {
46            my $irc = POE::Component::IRC::State->spawn(
47                Nick   => 'basic_bot',
48                Server => 'irc.freenode.net',
49            );
50
51            $irc->plugin_add('AutoJoin', POE::Component::IRC::Plugin::AutoJoin->new(
52               Channels => [ '#test_channel1', '#test_channel2' ]
53            ));
54
55            $irc->yield(register => 'join');
56            $irc->yield('connect');
57        }
58
59       Now comes our "irc_join" event handler. We send a message to the
60       channel once we've joined it.
61
62        sub irc_join {
63            my $nick = (split /!/, $_[ARG0])[0];
64            my $channel = $_[ARG1];
65            my $irc = $_[SENDER]->get_heap();
66
67            # only send the message if we were the one joining
68            if ($nick eq $irc->nick_name()) {
69                $irc->yield(privmsg => $channel, 'Hi everybody!');
70            }
71        }
72
73       That's it!
74

AUTHOR

76       Hinrik Örn Sigurðsson, hinrik.sig@gmail.com
77
78
79
80perl v5.38.0                      202P3O-E0:7:-C2o1mponent::IRC::Cookbook::BasicBot(3)
Impressum