Skip to content
Browse files

Powiedz adopted, small fix in WebTitle

  • Loading branch information...
1 parent 4ece89e commit 581980bdbb61552ddf9a906d4f9484e7c2b72125 @tadzik committed
Showing with 40 additions and 20 deletions.
  1. +38 −20 a8bot/Plugin/Powiedz.pm
  2. +2 −0 a8bot/Plugin/WebTitle.pm
View
58 a8bot/Plugin/Powiedz.pm
@@ -1,36 +1,54 @@
-package Powiedz;
-use strict;
-use warnings;
+package a8bot::Plugin::Powiedz;
+use Moose;
use Hash::MultiValue;
+with 'a8bot::Plugin';
-my $db = Hash::MultiValue->new;
+has 'db' => (
+ is => 'rw',
+ isa => 'Hash::MultiValue',
+ default => sub { Hash::MultiValue->new },
+);
-sub init {
- return { publicmsg => \&pubmsg };
+sub BUILD {
+ my $self = shift;
+ $self->keyword('powiedz');
+ $self->keyword_cb( sub { active($self, @_) } );
+ $self->passive_cb( sub { passive($self, @_) } );
}
-sub pubmsg {
- my ($bot, $data) = @_;
- my $resp = '';
- if ($data->{msg} =~ /^$bot->{nick},?:? powiedz ([^,: ]+)[:,]? (.+)$/) {
- $db->add($1 => [$data->{nick}, $2]);
- $resp = "$data->{nick}: ok, powiem mu jak się pojawi.";
+sub active {
+ my ($self, %data) = @_;
+ #number of elements in $data{args} arrayref
+ my @args = @{$data{args} // []};
+ if ($#args < 1) {
+ return "$data{nick}: Usage: powiedz <nick> <wiadomosc>";
+ } else {
+ $self->db->add($args[0] => [
+ $data{nick},
+ join(' ', @args[1..$#args]),
+ ]
+ );
+ return "$data{nick}: ok, powiem mu jak się pojawi.";
}
- if ($db->get($data->{nick})) {
- my @msgs = $db->get_all($data->{nick});
+}
+
+sub passive {
+ my ($self, %data) = @_;
+ if ($self->db->get($data{nick})) {
+ my @msgs = $self->db->get_all($data{nick});
+ my $resp = '';
for my $msg (@msgs) {
if ($resp ne '') {
$resp .= ' Poza tym, ';
}
$resp .= "$msg->[0] kazał Ci powiedzieć: $msg->[1].";
}
- $db->remove($data->{nick});
- }
- if ($resp ne '') {
- return $resp;
- } else {
- return undef;
+ $self->db->remove($data{nick});
+ return "$data{nick}: $resp";
}
+ return undef;
}
+__PACKAGE__->meta->make_immutable;
+
1;
View
2 a8bot/Plugin/WebTitle.pm
@@ -25,4 +25,6 @@ sub pubmsg {
return undef;
}
+__PACKAGE__->meta->make_immutable;
+
1;

0 comments on commit 581980b

Please sign in to comment.
Something went wrong with that request. Please try again.