File Coverage

blib/lib/Bot/IRC/Infobot.pm
Criterion Covered Total %
statement 19 57 33.3
branch 0 16 0.0
condition 0 6 0.0
subroutine 5 10 50.0
pod 0 1 0.0
total 24 90 26.6


line stmt bran cond sub pod time code
1             # ABSTRACT: Bot::IRC add classic "infobot" functionality to the bot
2              
3             use 5.014;
4 1     1   1897 use exact;
  1         3  
5 1     1   16  
  1         2  
  1         5  
6             use DateTime;
7 1     1   1223 use DateTime::Format::Human::Duration;
  1         338994  
  1         39  
8 1     1   451  
  1         1524  
  1         1026  
9             our $VERSION = '1.39'; # VERSION
10              
11             my ($bot) = @_;
12             $bot->load('Store');
13 1     1 0 4596  
14 1         5 $bot->hook(
15             {
16             command => 'PRIVMSG',
17             text => qr/^(?<term>\([^\)]+\)|\S+)\s+(?:is|=)\s+(?<fact>.+?)$/i,
18             },
19             sub {
20             my ( $bot, $in, $m ) = @_;
21              
22 0     0   0 ( my $term = lc( $m->{term} ) ) =~ s/[\(\)]+//g;
23             my @facts = @{ $bot->store->get($term) || [] };
24 0         0  
25 0 0       0 push( @facts, $m->{fact} );
  0         0  
26              
27 0         0 $bot->store->set( $term => \@facts );
28             return;
29 0         0 },
30 0         0 );
31              
32 1         11 $bot->hook(
33             {
34             command => 'PRIVMSG',
35             text => qr/^(?<term>\([^\)]+\)|\S+)\?/i,
36             },
37             sub {
38             my ( $bot, $in, $m ) = @_;
39              
40 0     0   0 ( my $display_term = $m->{term} ) =~ s/[\(\)]+//g;
41             my $term = lc($display_term);
42 0         0  
43 0         0 my @facts = map {
44             if ( /\|/ ) {
45             my @terms = split(/\|/);
46 0 0       0 $terms[ int( rand() * @terms ) ];
47 0         0 }
48 0         0 else {
49             $_;
50             }
51 0         0 } @{ $bot->store->get($term) || [] };
52              
53 0 0       0 return unless (@facts);
  0         0  
54              
55 0 0       0 my $text;
56             if ( grep { /^<(?:action|reply)>/i } @facts ) {
57 0         0 my $fact = $facts[ int( rand() * @facts ) ];
58 0 0       0 $text = $fact if ( $fact =~ s|^<\s*action\s*>\s*|/me |i or $fact =~ s|^<\s*reply\s*>\s*||i );
  0         0  
59 0         0 }
60 0 0 0     0 $bot->reply(
61             $text //
62             "$display_term is " . $bot->list( ', ', 'and', map { s/[.,;:?!]+$//; $_ } @facts ) . '.'
63             );
64 0   0     0 },
  0         0  
  0         0  
65             );
66              
67 1         19 $bot->hook(
68             {
69             to_me => 1,
70             text => qr/^no\W+(?<term>\([^\)]+\)|\S+)\s+(?:is|=)\s+(?<fact>.+?)$/i,
71             },
72             sub {
73             my ( $bot, $in, $m ) = @_;
74              
75 0     0   0 ( my $term = lc( $m->{term} ) ) =~ s/[\(\)]+//g;
76              
77 0         0 $bot->store->set( $term => [ $m->{fact} ] );
78             $bot->reply_to('OK.');
79 0         0 },
80 0         0 );
81              
82 1         18 $bot->hook(
83             {
84             to_me => 1,
85             text => qr/^forget\W+(?<term>\([^\)]+\)|[^.,:;?!\s]+)/i,
86             },
87             sub {
88             my ( $bot, $in, $m ) = @_;
89              
90 0     0   0 ( my $term = lc( $m->{term} ) ) =~ s/[\(\)]+//g;
91              
92 0         0 $bot->store->set( $term => [] );
93             $bot->reply_to('OK.');
94 0         0 },
95 0         0 );
96              
97 1         19 $bot->hook(
98             {
99             to_me => 1,
100             text => qr/^info\s+on\s+(?<term>\([^\)]+\)|[^.,:;?!\s]+)/i,
101             },
102             sub {
103             my ( $bot, $in, $m ) = @_;
104              
105 0     0   0 ( my $term = lc( $m->{term} ) ) =~ s/[\(\)]+//g;
106             my @facts = @{ $bot->store->get($term) || [] };
107 0         0  
108 0 0       0 $bot->reply_to(
  0         0  
109             (@facts)
110 0 0       0 ? "\"$m->{term}\" is " . $bot->list( ', ', 'and', @facts )
111             : "I have no information on \"$m->{term}\"."
112             );
113             },
114             );
115              
116 1         14 $bot->helps( infobot =>
117             'Mimics the factoid functionality of the classic infobot. ' .
118 1         11 'Usage: https://metacpan.org/pod/Bot::IRC::Infobot'
119             );
120             }
121              
122             1;
123              
124              
125             =pod
126              
127             =encoding UTF-8
128              
129             =head1 NAME
130              
131             Bot::IRC::Infobot - Bot::IRC add classic "infobot" functionality to the bot
132              
133             =head1 VERSION
134              
135             version 1.39
136              
137             =head1 SYNOPSIS
138              
139             use Bot::IRC;
140              
141             Bot::IRC->new(
142             connect => { server => 'irc.perl.org' },
143             plugins => ['Infobot'],
144             )->run;
145              
146             =head1 DESCRIPTION
147              
148             This L<Bot::IRC> plugin adds classic "infobot" functionality to the bot.
149              
150             =head2 Remembering Factoids
151              
152             <user> thing is great
153             <user> thing is awesome
154             <user> thing is wonderful
155              
156             =head2 Recalling Factoids
157              
158             <user> thing?
159             <bot> thing is great, awesome, and wondeful.
160              
161             =head2 Overwriting Factoids
162              
163             <user> bot no thing is terrible
164             <bot> user: OK.
165             <user> thing?
166             <bot> thing is terrible.
167              
168             =head2 Forgetting Factoids
169              
170             <user> bot forget thing
171             <bot> user: OK.
172              
173             =head2 Reply Factoids
174              
175             A factiod that begins with "<reply>" will have the "<noun> is" missing in the
176             reply.
177              
178             <user> stuff is <reply> What stuff?
179             <user> stuff?
180             <bot> What stuff?
181              
182             =head2 Action Factoids
183              
184             A factoid that begins "<action>" will be emoted as a response.
185              
186             <user> sad is <action> cries in a corner.
187             <user> sad?
188             <bot> * bot cries in a corner.
189              
190             =head2 Multiple Answers
191              
192             Pipes ("|") indicate different possible answers selected at random.
193              
194             <user> d6 is 1|2|3|4|5|6
195             <user> d6?
196             <bot> 5
197             <user> d6?
198             <bot> 3
199              
200             =head2 Learning What the Bot Knows
201              
202             <user> bot info on d6
203             <bot>user: d6 is 1|2|3|4|5|6
204              
205             =head2 SEE ALSO
206              
207             L<Bot::IRC>
208              
209             =for Pod::Coverage init
210              
211             =head1 AUTHOR
212              
213             Gryphon Shafer <gryphon@cpan.org>
214              
215             =head1 COPYRIGHT AND LICENSE
216              
217             This software is Copyright (c) 2016-2050 by Gryphon Shafer.
218              
219             This is free software, licensed under:
220              
221             The Artistic License 2.0 (GPL Compatible)
222              
223             =cut