File Coverage

blib/lib/Bot/BasicBot/CommandBot.pm
Criterion Covered Total %
statement 42 69 60.8
branch 4 16 25.0
condition 2 13 15.3
subroutine 10 17 58.8
pod 8 8 100.0
total 66 123 53.6


line stmt bran cond sub pod time code
1             package Bot::BasicBot::CommandBot;
2              
3 1     1   5 use strict;
  1         2  
  1         24  
4 1     1   3 use warnings;
  1         1  
  1         16  
5 1     1   12 use 5.014;
  1         2  
  1         29  
6              
7             our $VERSION = 0.05;
8              
9 1     1   366 use Tie::RegexpHash 0.16;
  1         8178  
  1         37  
10 1     1   7 use List::Util qw(any);
  1         1  
  1         96  
11              
12 1     1   4 use Exporter 'import';
  1         1  
  1         20  
13 1     1   3 use base qw/Bot::BasicBot/;
  1         1  
  1         528  
14              
15             our @EXPORT_OK = qw(command autocommand);
16              
17             my %command;
18             my %autocommand;
19              
20             sub command {
21 2     2 1 9 (caller)[0]->declare_command(@_);
22             }
23              
24             sub autocommand {
25 0     0 1 0 (caller)[0]->declare_autocommand(@_);
26             }
27              
28             sub declare_autocommand {
29 0     0 1 0 my ($package, $sub) = @_;
30 0         0 $autocommand{$package} = $sub;
31             }
32              
33             sub declare_command {
34 2     2 1 2 my $package = shift;
35 2         2 my $sub = pop;
36 2         1 my $command = pop;
37              
38 2         3 my %options = @_;
39              
40 2   50     10 $options{events} //= ['said'];
41              
42 2 100       5 if (not exists $command{$package}) {
43 1         1 $command{$package} = {};
44 1         1 tie %{$command{$package}}, 'Tie::RegexpHash';
  1         8  
45             }
46              
47 2         18 $command{$package}{$command} = {
48             sub => $sub,
49             %options,
50             };
51             }
52              
53              
54             sub new {
55 1     1 1 13 my $class = shift;
56 1         5 my %opts = @_;
57              
58 1         3 my $address = delete $opts{address};
59 1         2 my $trigger = delete $opts{trigger};
60 1   50     5 my $bark = delete $opts{bark} // 1;
61              
62 1         9 my $self = $class->SUPER::new(%opts);
63 1 50       94 $self->{trigger} = $trigger if defined $trigger;
64 1 50       3 $self->{address} = $address if defined $address;
65 1         2 $self->{bark} = $bark;
66              
67 1           return $self;
68             }
69              
70             sub said {
71 0     0 1   my $self = shift;
72 0           my ($data) = @_;
73              
74 0           $self->{command} = $data;
75 0           my $package = ref $self;
76              
77 0           my ($cmd, $message) = split ' ', $data->{body}, 2;
78              
79 0           my $autosay = $self->_auto($data->{body});
80              
81 0 0 0       if ($self->{address} and not $data->{address}) {
82 0           return $autosay;
83             }
84              
85 0 0 0       if ($self->{trigger} and $data->{body} !~ s/^\Q$self->{trigger}//) {
86 0           return $autosay;
87             }
88              
89 0           my $found = $command{$package}{$cmd};
90              
91 0 0         if (!$found) {
92 0 0         return "What is $cmd?" if $self->{bark};
93 0           return $autosay;
94             }
95              
96 0           my $say;
97 0 0   0     if (any { $_ eq 'said' } @{$found->{events}}) {
  0            
  0            
98 0           $say = $found->{sub}->($self, $cmd, $message);
99             }
100              
101 0   0       return $say // $autosay;
102             }
103              
104 0     0 1   sub emoted {
105             }
106              
107 0     0 1   sub noticed {
108             }
109              
110             sub _auto {
111 0     0     my ($self, $message) = @_;
112 0           join ' ', map $_->($self, $message), values %autocommand;
113             }
114              
115             1;
116              
117             __END__