From 4b1f1713c010ad1097fd3e70a84ee7324a3b00a8 Mon Sep 17 00:00:00 2001 From: Alexandre Morignot <erdnaxeli@gmail.com> Date: Tue, 5 Aug 2014 01:00:42 +0200 Subject: [PATCH] !get work with an ID --- lib/commands/get.pm | 57 +++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/lib/commands/get.pm b/lib/commands/get.pm index 0284c3e..1f1006a 100644 --- a/lib/commands/get.pm +++ b/lib/commands/get.pm @@ -6,6 +6,7 @@ our @EXPORT_OK = qw(exec); use strict; use warnings; +use Scalar::Util qw(looks_like_number); our $dbh; our $irc; @@ -19,34 +20,42 @@ sub exec { my @tags = ($msg =~ /#?([a-zA-Z0-9_-]+)/g); my $content; + my $sth; if (@tags) { - my $params = join ', ' => ('?') x @tags; - my $sth; - - if ($all) { + if (looks_like_number($tags[0])) { $sth = $dbh->prepare('select id, sender, title, url from playbot - natural join playbot_tags - where tag in ('.$params.') - group by id - having count(*) >= ? - order by rand() - limit 1'); - $sth->execute(@tags, scalar @tags); + where id = ?'); + $sth->execute($tags[0]); } else { - $sth = $dbh->prepare('select p.id, p.sender, p.title, p.url - from playbot p - natural join playbot_tags pt - join playbot_chan pc on p.id = pc.content - where pt.tag in ('.$params.') - and pc.chan = ? - group by p.id - having count(*) >= ? - order by rand() - limit 1'); - $sth->execute(@tags, $chan->[0], scalar @tags); + my $params = join ', ' => ('?') x @tags; + + if ($all) { + $sth = $dbh->prepare('select id, sender, title, url + from playbot + natural join playbot_tags + where tag in ('.$params.') + group by id + having count(*) >= ? + order by rand() + limit 1'); + $sth->execute(@tags, scalar @tags); + } + else { + $sth = $dbh->prepare('select p.id, p.sender, p.title, p.url + from playbot p + natural join playbot_tags pt + join playbot_chan pc on p.id = pc.content + where pt.tag in ('.$params.') + and pc.chan = ? + group by p.id + having count(*) >= ? + order by rand() + limit 1'); + $sth->execute(@tags, $chan->[0], scalar @tags); + } } $content = $sth->fetch; @@ -57,8 +66,6 @@ sub exec { } } else { - my $sth; - if ($all) { $sth = $dbh->prepare('select id, sender, title, url from playbot order by rand() @@ -83,7 +90,7 @@ sub exec { } } - my $sth = $dbh->prepare("select group_concat(tag separator ' ') + $sth = $dbh->prepare("select group_concat(tag separator ' ') from playbot_tags where id = ? and context = 0 group by id"); -- GitLab