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