Skip to content
Extraits de code Groupes Projets
Valider 4069ffbe rédigé par Alexandre Morignot's avatar Alexandre Morignot
Parcourir les fichiers

clever !get

!get iterates on the results of a repeated query instead of just doing a
stupid random.
parent 119b65d8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -6,11 +6,15 @@ use Scalar::Util qw(looks_like_number);
use lib "$FindBin::Bin/lib/";
use utils::print;
use utils::db;
our $dbh;
our $irc;
our $log;
my $last_req;
my $sth;
sub exec {
my ($kernel, $nick, $chan, $msg) = @_;
......@@ -21,10 +25,13 @@ sub exec {
my @tags = ($msg =~ /#([a-zA-Z0-9_-]+)/g);
my $content;
my $req;
my $sth;
my $rows;
my @words = ($msg =~ /(?:^| )([a-zA-Z0-9_-]+)/g);
if (not defined $last_req or $msg ne $last_req) {
my $dbh = utils::db::get_session;
my @words_param;
while ($msg =~ /(?:^| )([a-zA-Z0-9_-]+)/g) {
unshift @words_param, '%'.$1.'%';
......@@ -97,30 +104,37 @@ sub exec {
$sth->execute($chan->[0], @words_param);
}
}
}
$content = $sth->fetch;
if (!$content) {
if (@words or @tags) {
if ($last_req eq $msg) {
# the request was already executed, there is nothing more
$irc->yield(privmsg => $chan => "Tu tournes en rond, Jack !");
}
elsif (@words or @tags) {
$irc->yield(privmsg => $chan => "Je n'ai rien dans ce registre.");
}
else {
$irc->yield(privmsg => $chan => "Poste d'abord du contenu, n00b.");
}
$last_req = undef;
return
}
# this is specific to the mysql driver
$rows = $sth->rows;
$sth = $dbh->prepare("select tag
my $sth2 = utils::db::get_session()->prepare("select tag
from playbot_tags
where id = ?
");
$sth->execute($content->[0]);
$sth2->execute($content->[0]);
my @tags;
while (my $data = $sth->fetch) {
while (my $data = $sth2->fetch) {
my $tag = $data->[0];
$tag =~ s/([a-zA-Z0-9_-]+)/#$1/;
push @tags, $tag;
......@@ -134,14 +148,14 @@ sub exec {
$content_h{'duration'} = $content->[4];
$content_h{'tags'} = \@tags;
my $msg = utils::print::print(\%content_h);
$msg .= ' [' . $rows . ' résultat';
$msg .= 's' if ($rows > 1);
$msg .= ']';
$irc->yield(privmsg => $chan => $msg);
my $irc_msg = utils::print::print(\%content_h);
$irc_msg .= ' [' . $rows . ' résultat';
$irc_msg .= 's' if ($rows > 1);
$irc_msg .= ']';
$irc->yield(privmsg => $chan => $irc_msg);
# we save the get like a post
my $sth2 = $dbh->prepare_cached('
$sth2 = utils::db::get_session()->prepare_cached('
INSERT INTO playbot_chan (content, chan, sender_irc)
VALUES (?,?,?)');
$log->error("Couldn't prepare querie; aborting") unless (defined $sth2);
......@@ -149,6 +163,9 @@ sub exec {
$sth2->execute($content->[0], $chan->[0], "PlayBot")
or $log->error("Couldn't finish transaction: " . $dbh->errstr);
# we save the request
$last_req = $msg;
return $content->[0];
}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter