Webqq/Plugin/Riddle.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 120 CC 17 SCAR 56.1
CC
17
Cov
0%
call
306.0
BEGIN
2.0
BEGIN
2.0
CRAP 274.0
1 package Mojo::Webqq::Plugin::Riddle;
2 our $PRIORITY = 92;
3 use Encode;
4 use List::Util qw(first);
5 sub call{
6 my $client = shift;
7 my $data = shift;
8 my $flag = 0;
9 my $command = $data->{command} || "猜谜";
10 $client->on(receive_message=>sub{
11 my($client,$msg) = @_;
12 if($msg->type eq "group_message"){
13 return if ref $data->{ban_group} eq "ARRAY" and first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->name eq $_} @{$data->{ban_group}};
14 return if ref $data->{allow_group} eq "ARRAY" and !first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->name eq $_} @{$data->{allow_group}}
15 }
16 return if ref $data->{ban_user} eq "ARRAY" and first {$_=~/^\d+$/?$msg->sender->uid eq $_:$sender_nick eq $_} @{$data->{ban_user}};
17
18 if($flag == 0 and $msg->content eq $command){
19 $msg->allow_plugin(0);
20 $client->steps(
21 sub{
22 my $delay =shift;
23 $client->http_get('http://apis.baidu.com/gushi/grid/p1',{json=>1,apikey=>$data->{apikey}||'20d7db97e337ffa35ae0838439c9db5d'},form=>{count=>1,fmt=>0},$delay->begin(0,1));
24 },
25 sub{
26 my $delay = shift;
27 my $json = shift;
28 return if not defined $json;
29 return if $json->{status} != 0;
30 my $id = $json->{data}[0]{id} if ref $json->{data} eq "ARRAY";
31 return if not $id;
32 $client->http_get('http://apis.baidu.com/gushi/grid/p2',{json=>1,apikey=>$data->{apikey}||'20d7db97e337ffa35ae0838439c9db5d'},form=>{id=>$id,fmt=>0},sub{
33 my $json = shift;
34 return if not defined $json;
35 return if $json->{status} != 0;
36 return if ref $json->{data} ne "ARRAY";
37 my $answer = $json->{data}[0]{body};
38 $flag = 1;
39 $msg->reply("文曲星君题戏三界($json->{data}[0]{id}):\n" . $json->{data}[0]{title},sub{$_[1]->from("bot")});
40  
41 $client->wait(
42 $data->{timeout} || 30,#等待答案超时时间
43 sub{#超时公布答案
44 $flag = 0;
45 $msg->reply("本题答案:$answer\n偌大的三界之中,难道就没有能懂本星君心意之人么.\n吾独徘徊于天地之间,对酒影成双,知己难求,呜呼哉!");
46 },
47 receive_message=>sub{#查看是否有人给出正确答案
48 my($client,$msg,$timer_id) = @_;
49 return if $msg->content !~ /\Q$answer\E/;
50 $flag = 0;
51 $msg->reply("于千万人之中,文曲星君终于找到了有缘人:\n恭喜 \@" . $msg->sender->displayname . " 回答正确"); return 1;
52 }
53 );
54 });
55 },
56 );
57 }
58 });
59 }
60 1;