File Coverage

blib/lib/Bot/IRC/Karma.pm
Criterion Covered Total %
statement 11 37 29.7
branch 0 10 0.0
condition 0 13 0.0
subroutine 3 6 50.0
pod 0 1 0.0
total 14 67 20.9


line stmt bran cond sub pod time code
1             # ABSTRACT: Bot::IRC track karma for things
2              
3             use 5.014;
4 1     1   1833 use exact;
  1         3  
5 1     1   17  
  1         1  
  1         6  
6             our $VERSION = '1.39'; # VERSION
7              
8             my ($bot) = @_;
9             $bot->load('Store');
10 1     1 0 4906  
11 1         5 $bot->hook(
12             {
13             command => 'PRIVMSG',
14             text => qr/^(?<thing>\([^\)]+\)|\S+)\s*(?<type>[+-]{2,})(?:\s*#\s*(?<comment>.+))?/,
15             },
16             sub {
17             my ( $bot, $in, $m ) = @_;
18             my $type = substr( $m->{type}, 0, 2 );
19 0     0   0 my $thing = $bot->store->get( lc( $m->{thing} ) ) || {};
20 0         0  
21 0   0     0 if ( $type eq '++' ) {
22             $thing->{karma} += 1;
23 0 0       0 }
    0          
24 0         0 elsif ( $type eq '--' ) {
25             $thing->{karma} -= 1;
26             }
27 0         0  
28             push( @{ $thing->{comments}{$type} }, $m->{comment} ) if ( $m->{comment} );
29             $bot->store->set( lc( $m->{thing} ) => $thing );
30 0 0       0 },
  0         0  
31 0         0 );
32              
33 1         12 $bot->hook(
34             {
35             to_me => 1,
36             text => qr/^karma\s+(?<thing>\([^\)]+\)|\S+)/i,
37             },
38             sub {
39             my ( $bot, $in, $m ) = @_;
40             my $thing = $bot->store->get( lc( $m->{thing} ) ) || {};
41 0     0   0 my $karma = $thing->{karma} || 0;
42 0   0     0  
43 0   0     0 $bot->reply_to("$m->{thing} has a karma of $karma.");
44             },
45 0         0 );
46              
47 1         18 $bot->hook(
48             {
49             to_me => 1,
50             text => qr/^explain\s+(?<thing>\([^\)]+\)|\S+)/i,
51             },
52             sub {
53             my ( $bot, $in, $m ) = @_;
54             my $thing = $bot->store->get( lc( $m->{thing} ) ) || {};
55 0     0   0 my $karma = $thing->{karma} || 0;
56 0   0     0  
57 0   0     0 my @text;
58             for my $type ( '++', '--' ) {
59 0         0 if ( $thing->{comments}{$type} and @{ $thing->{comments}{$type} } ) {
60 0         0 push( @text,
61 0 0 0     0 "Some say $m->{thing} is " . ( ( $type eq '++' ) ? 'good' : 'bad' ) . " because: " .
  0         0  
62             (
63             map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, rand() ] }
64             @{ $thing->{comments}{$type} }
65 0         0 )[0] . '.'
  0         0  
  0         0  
66 0 0       0 );
  0         0  
67             }
68             }
69              
70             $bot->reply_to( join( ' ', @text ) );
71             },
72 0         0 );
73              
74 1         21 $bot->helps(
75             karma => join(
76 1         16 'Adjust the karma of a word.',
77             'Usage: "word++" or "word--" to increment or decrement the karma of "word".',
78             '"karma word" to see what the karma of "word" is.',
79             '"explain word" to receive one positive and one negative comment (at random)',
80             'about the word if there are comments to share.',
81             ),
82             );
83             }
84              
85             1;
86              
87              
88             =pod
89              
90             =encoding UTF-8
91              
92             =head1 NAME
93              
94             Bot::IRC::Karma - Bot::IRC track karma for things
95              
96             =head1 VERSION
97              
98             version 1.39
99              
100             =head1 SYNOPSIS
101              
102             use Bot::IRC;
103              
104             Bot::IRC->new(
105             connect => { server => 'irc.perl.org' },
106             plugins => ['Karma'],
107             )->run;
108              
109             =head1 DESCRIPTION
110              
111             This L<Bot::IRC> plugin gives the bot the ability to track and report on
112             something as meaningless as the concept of karma for things. Commands include:
113              
114             =head2 <thing>++ # comment
115              
116             Increases karma of "thing" by one and optionally remembers a positive comment
117             about "thing".
118              
119             =head2 <thing>-- # comment
120              
121             Decreases karma of "thing" by one and optionally remembers a negative comment
122             about "thing".
123              
124             =head2 karma <thing>
125              
126             Reports the karma of a "thing".
127              
128             =head2 explain <thing>
129              
130             Tells you one positive and one negative comment (at random) about "thing" if
131             there are comments to share.
132              
133             =head2 SEE ALSO
134              
135             L<Bot::IRC>
136              
137             =for Pod::Coverage init
138              
139             =head1 AUTHOR
140              
141             Gryphon Shafer <gryphon@cpan.org>
142              
143             =head1 COPYRIGHT AND LICENSE
144              
145             This software is Copyright (c) 2016-2050 by Gryphon Shafer.
146              
147             This is free software, licensed under:
148              
149             The Artistic License 2.0 (GPL Compatible)
150              
151             =cut