diff --git a/SDK/API.php b/SDK/API.php index f41a31d..efb2f01 100644 --- a/SDK/API.php +++ b/SDK/API.php @@ -41,6 +41,7 @@ class API{ const clean_plugin_log = '/clean_plugin_log'; const _get_friend_list = '/_get_friend_list'; const _get_group_info = '/_get_group_info'; + const _get_vip_info = '/_get_vip_info'; const __check_update = '/.check_update'; const __handle_quick_operation = '/.handle_quick_operation'; } diff --git a/SDK/CoolQ.php b/SDK/CoolQ.php index 818ceca..09de1c5 100644 --- a/SDK/CoolQ.php +++ b/SDK/CoolQ.php @@ -306,10 +306,12 @@ public function getVersionInfo(){ return $this->query($api, $param); } - public function setRestart($clean_cache = false){ + public function setRestart($clean_log = false, $clean_cache = false, $clean_event = false){ $api = API::set_restart; $param = [ + 'clean_log' => $clean_log, 'clean_cache' => $clean_cache, + 'clean_event'=> $clean_event, ]; return $this->query($api, $param); } @@ -336,16 +338,26 @@ public function cleanPluginLog(){ return $this->query($api, $param); } - public function _getFriendList(){ + public function _getFriendList($flat = false){ $api = API::_get_friend_list; - $param = []; + $param = [ + 'flat' => $flat, + ]; return $this->query($api, $param); } - public function _getGroupInfo($flat = false){ + public function _getGroupInfo($group_id){ $api = API::_get_group_info; $param = [ - 'flat' => $flat, + 'group_id' => $group_id, + ]; + return $this->query($api, $param); + } + + public function _get_vip_info($user_id){ + $api = API::_get_vip_info; + $param = [ + 'user_id' => $user_id, ]; return $this->query($api, $param); } @@ -381,7 +393,7 @@ private function query($api, $param){ case 1: return NULL; default: - throw new \Exception("Query Failed", $result['retcode']); + throw new \Exception("Query Failed", $result->retcode); } } diff --git a/config.ini.example b/config.ini.example index 1a5457b..8be7da2 100644 --- a/config.ini.example +++ b/config.ini.example @@ -1,11 +1,11 @@ -API=127.0.0.1:5700 +API="127.0.0.1:5700" token= prefix="!" master=919815238 bot=2839098896 allowFriends=true allowGroups=true -WelcomeMsg="感谢您添加 kjBot 为好友,发送 !help 查看帮助列表" +WelcomeMsg="感谢您添加 kjBot 为好友,发送 !help 查看帮助列表。用户协议:https://github.com/kjBot-Dev/TOS/blob/master/README.md" ;DEBUG=true DebugListen=919815238 ;Listen= diff --git a/module/AD/main.php b/module/AD/main.php new file mode 100644 index 0000000..ba9db28 --- /dev/null +++ b/module/AD/main.php @@ -0,0 +1,59 @@ +getGroupList(); +$whiteList = file_get_contents('https://raw.githubusercontent.com/kjBot-Dev/ADwhitelist/master/whitelist.json'); +if(false === $whiteList)leave('打开白名单失败,终止'); + +$prefix=<<list; +$expireDay = []; +$success = 0; +$silence = 0; +$error = 0; +$now = new \DateTime(); + +foreach($whiteList as $group){ + $expireDay[$group->group] = \DateTime::createFromFormat('Y-m-d H:i:s', $group->expire_day); +} + +foreach($groupList as $group){ + if(isset($expireDay[$group->group_id])){ + if($now < $expireDay[$group->group_id])continue; + } + try{ + $CQ->sendGroupMsg($group->group_id, $Text); + $success++; + }catch(\Exception $e){ + if(-34 === $e->getCode()){ + $silence++; + }else{ + $error++; + } + $Queue[]= sendMaster("Query {$group->group_id} failed: ".$e->getCode()); + } + if($error>5)leave('错误次数过多,终止'); + sleep(10); //10秒延迟 +} +$groupCount = count($groupList); +$whiteCount = $groupCount-$success-$error-$silence; +$Queue[]= sendMaster("目前共有 {$groupCount} 个群,有 {$whiteCount} 个群白名单生效中。\n已投放 {$success} 条广告,异常原因失败 {$error} 个,被 {$silence} 个群禁言中"); + +?> diff --git a/module/announce/main.php b/module/announce/main.php index d530edb..d76f77a 100644 --- a/module/announce/main.php +++ b/module/announce/main.php @@ -3,11 +3,28 @@ global $Queue, $Text, $CQ; use kjBot\Frame\Message; requireMaster(); +set_time_limit(0); $groupList = $CQ->getGroupList(); +$success = 0; +$silence = 0; +$error = 0; foreach($groupList as $group){ - $Queue[]= new Message($Text, $group->group_id, true); + try{ + $CQ->sendGroupMsg($group->group_id, $Text); + $success++; + }catch(\Exception $e){ + if(-34 === $e->getCode()){ + $silence++; + }else{ + $error++; + } + $Queue[]= sendMaster("Query {$group->group_id} failed: ".$e->getCode()); + } + if($error>5)leave('错误次数过多,终止'); + sleep(10); //10秒延迟 } - +$groupCount = count($groupList); +$Queue[]= sendMaster("目前共有 {$groupCount} 个群,成功 {$success} 个,异常原因失败 {$error} 个,被 {$silence} 个群禁言中"); ?> \ No newline at end of file diff --git a/module/credit/check.php b/module/credit/check.php index 7d6aa81..017d04e 100644 --- a/module/credit/check.php +++ b/module/credit/check.php @@ -3,7 +3,11 @@ global $Queue, $Event; use kjBot\SDK\CQCode; -$QQ = parseQQ(nextArg())??$Event['user_id']; +$QQ = nextArg(); +if(!(preg_match('/\d+/', $QQ, $match) && $match[0] == $QQ)){ + $QQ = parseQQ($QQ); +} +$QQ = $QQ??$Event['user_id']; $Queue[]= sendBack(CQCode::At($QQ).' 的余额为 '.getData("credit/{$QQ}")); ?> diff --git a/module/credit/set.php b/module/credit/set.php index f8ac7c8..e381e8a 100644 --- a/module/credit/set.php +++ b/module/credit/set.php @@ -6,7 +6,10 @@ loadModule('credit.tools'); -$QQ = parseQQ(nextArg()); +$QQ = nextArg(); +if(!(preg_match('/\d+/', $QQ, $match) && $match[0] == $QQ)){ + $QQ = parseQQ($QQ); +} $credit = (int)nextArg(); setCredit($QQ, $credit); diff --git a/module/credit/tools.php b/module/credit/tools.php index 8a24201..ae2ae72 100644 --- a/module/credit/tools.php +++ b/module/credit/tools.php @@ -4,18 +4,21 @@ function getCredit($QQ){ return (int)getData("credit/{$QQ}"); } -function setCredit($QQ, $credit){ +function setCredit($QQ, $credit, $set = false){ + if($set)setData('credit.history', "* {$QQ} {$credit}\n", true); return setData("credit/{$QQ}", (int)$credit); } function addCredit($QQ, $income){ - return setCredit($QQ, getCredit($QQ)+(int)$income); + setData('credit.history', "+ {$QQ} {$income}\n", true); + return setCredit($QQ, getCredit($QQ)+(int)$income, true); } function decCredit($QQ, $pay){ $balance = getCredit($QQ); if($balance >= $pay){ - return setCredit($QQ, (int)($balance-$pay)); + setData('credit.history', "- {$QQ} {$pay}\n"); + return setCredit($QQ, (int)($balance-$pay), true); }else{ throw new \Exception('余额不足,还需要 '.($pay-$balance).' 个金币'); } diff --git a/module/credit/transfer.php b/module/credit/transfer.php index c52268a..4fc6ace 100644 --- a/module/credit/transfer.php +++ b/module/credit/transfer.php @@ -4,7 +4,10 @@ loadModule('credit.tools'); use kjBot\SDK\CQCode; -$QQ = parseQQ(nextArg()); +$QQ = nextArg(); +if(!(preg_match('/\d+/', $QQ, $match) && $match[0] == $QQ)){ + $QQ = parseQQ($QQ); +} $transfer = abs((int)nextArg()); transferCredit($Event['user_id'], $QQ, $transfer); diff --git a/module/osu/bp.php b/module/osu/bp.php index b0e113a..9928e2e 100644 --- a/module/osu/bp.php +++ b/module/osu/bp.php @@ -44,7 +44,7 @@ $u = $user??$osuUser; }else{ if($user == NULL){ - throw new \Exception('未绑定 osu!,且未指定用户'); + throw new \Exception("未绑定 osu!,请使用\n!osu.bind 用户名\n进行绑定"); }else{ $u = $user; } diff --git a/module/osu/profile.php b/module/osu/profile.php index e50aa03..a8a3e0b 100644 --- a/module/osu/profile.php +++ b/module/osu/profile.php @@ -48,7 +48,7 @@ $osuid = $osuid??getOsuID($qq); if($osuid == ''){ - throw new \Exception('未绑定 osu!'); + throw new \Exception("未绑定 osu!,请使用\n!osu.bind 用户名\n进行绑定"); } $osuid = OsuUsernameEscape($osuid); diff --git a/module/osu/recent.php b/module/osu/recent.php index 94d6ff2..e3e7640 100644 --- a/module/osu/recent.php +++ b/module/osu/recent.php @@ -40,7 +40,7 @@ $u = $user??$osuUser; }else{ if($user == NULL){ - throw new \Exception('未绑定 osu!,且未指定用户'); + throw new \Exception("未绑定 osu!,请使用\n!osu.bind 用户名\n进行绑定"); }else{ $u = $user; } diff --git a/module/pixiv/IID.php b/module/pixiv/IID.php index f53f229..1a6d3ee 100644 --- a/module/pixiv/IID.php +++ b/module/pixiv/IID.php @@ -16,24 +16,21 @@ $pixiv = getIllustInfoByID($iID); $img = getIllustImgstr($pixiv); } - $tags = implode(' ', $pixiv->tags); - + $tags = getIllustTagsFromPixivJSON($pixiv); $msg=<<userId} 标签:{$tags} {$pixiv->illustTitle} +{$pixiv->illustComment} EOT; - $msg.=sendImg($img); - if($pixiv->xRestrict === 1){ $Queue[]= sendPM($msg, false, true); //异步发送加快处理速度 }else{ $Queue[]= sendBack($msg, false, true); } - }while($iID!==NULL); diff --git a/module/pixiv/search.php b/module/pixiv/search.php index d84aceb..e63fc40 100644 --- a/module/pixiv/search.php +++ b/module/pixiv/search.php @@ -52,15 +52,18 @@ } $pixiv = $result[$index++]; -$tags = implode(' ', $pixiv->tags); +$pixiv = getIllustInfoByID($pixiv->illustId); +$tags = getIllustTagsFromPixivJSON($pixiv); $img = getIllustImgstr($pixiv); $msg=<<illustId} 画师ID:{$pixiv->userId} +标签:{$tags} {$pixiv->illustTitle} +{$pixiv->illustComment} EOT; diff --git a/module/pixiv/tools.php b/module/pixiv/tools.php index 201af85..14f3eea 100644 --- a/module/pixiv/tools.php +++ b/module/pixiv/tools.php @@ -10,28 +10,10 @@ function getIllustImgstr($pixiv, $page = NULL){ global $pixivCookieHeader; - $img = $pixiv->url; - $img = str_replace('_master1200', '', $img); //消去尾缀 - $img = str_replace('.jpg', '.png', $img); //优先尝试以 png 取得原图 - if($page !== NULL){ //如果是在获取 manga ID - $img = str_replace('_p0.png', "_p{$page}.png", $img); - $img = str_replace('/img-master', '/img-original', $img); - $imgHeader['http']['header']=$pixivCookieHeader['http']['header'].'referer: https://www.pixiv.net/member_illust.php?mode=manga&illust_id='.$pixiv->illustId."\n"; - }else{ - $img = str_replace('/c/240x240/img-master', '/img-original', $img); //转换为原图路径 - $imgHeader['http']['header']=$pixivCookieHeader['http']['header'].'referer: https://www.pixiv.net/member_illust.php?mode=medium&illust_id='.$pixiv->illustId."\n"; //伪造上级页面来源 - } - + $img = $pixiv->urls->original; + $imgHeader['http']['header']=$pixivCookieHeader['http']['header'].'referer: https://www.pixiv.net/member_illust.php?mode=medium&illust_id='.$pixiv->illustId."\n"; //伪造上级页面来源 $imgStr = file_get_contents($img, false, stream_context_create($imgHeader)); - if($imgStr === false){ //如果 png 没取到图 - $img = str_replace('.png', '.jpg', $img); - $imgStr = file_get_contents($img, false, stream_context_create($imgHeader)); //改用 jpg 取图 - if($imgStr === false){ - leave('未知图片类型'); - } - } - return $imgStr; } @@ -40,12 +22,22 @@ function getIllustInfoByID($iID){ $web = file_get_contents('https://www.pixiv.net/member_illust.php?mode=medium&illust_id='.$iID, false, stream_context_create($pixivCookieHeader)); if($web===false)leave('无法打开 Pixiv'); - if(!preg_match('/"'.$iID.'":({[^}]*})/', $web, $result)){ + if(!preg_match('/illust:\s?\{\s?'.$iID.':\s?({[\S\s]*}\})/', $web, $result)){ leave('没有这张插画'); } $pixiv = json_decode($result[1]); + setData('test.txt', var_export($pixiv, true)); return $pixiv; } +function getIllustTagsFromPixivJSON($pixiv){ + $tags = $pixiv->tags->tags; + $tagString = ''; + foreach($tags as $tag){ + $tagString.= $tag->tag.' '; + } + return rtrim($tagString); +} + ?> \ No newline at end of file diff --git a/module/recordStat/main.php b/module/recordStat/main.php index 3bdaddf..f9d346c 100644 --- a/module/recordStat/main.php +++ b/module/recordStat/main.php @@ -13,6 +13,7 @@ !recordStat.verify EOT; +if(trim(getData('recordStat/'.$User_id))=='') setData('recordStat/'.$User_id, 'read'); $Queue[]= sendPM($agreement); //仅在私聊中发送用户协议 diff --git a/module/status/main.php b/module/status/main.php index 50f4751..a8b6175 100644 --- a/module/status/main.php +++ b/module/status/main.php @@ -21,12 +21,29 @@ $usedDiskPercent = sprintf('%.2f%%', $disk['used']/$disk['size']*100); $msg=<< \ No newline at end of file +?> diff --git a/module/version/main.php b/module/version/main.php index ebab274..7d123ce 100644 --- a/module/version/main.php +++ b/module/version/main.php @@ -1,9 +1,17 @@ getVersionInfo(); +$coolQVersion->coolq_edition = ucfirst($coolQVersion->coolq_edition); $Github = new \Github\Client(); $result = $Github->api('repo')->releases()->latest('kj415j45', 'kjBot'); -$Queue[]= sendBack("kjBot {$result['tag_name']} {$result['name']}\n项目地址:https://github.com/kj415j45/kjBot\n{$result['body']}"); +$Queue[]= sendBack(<<coolq_edition} (HTTP API {$coolQVersion->plugin_version}) +项目地址:https://github.com/kj415j45/kjBot +{$result['body']} +EOT +); ?> diff --git a/public/index.php b/public/index.php index cb9e7a9..3831e54 100644 --- a/public/index.php +++ b/public/index.php @@ -24,7 +24,7 @@ //调试 if($Debug && $Event['user_id'] == $DebugListen){ - $Queue[]= sendMaster(var_export($Queue, true)); + $Queue[]= sendMaster(var_export($Event, true)."\n\n".var_export($Queue, true)); } }catch(\Exception $e){ diff --git a/public/init.php b/public/init.php index 975d933..d9c9df2 100644 --- a/public/init.php +++ b/public/init.php @@ -13,7 +13,7 @@ $CQ = new CoolQ(config('API', '127.0.0.1:5700'), config('token', '')); $Queue = []; $MsgSender = new MessageSender($CQ); -$Debug = ('true'===config('DEBUG', 'false'))?true:false; +$Debug = config('DEBUG', false); $DebugListen = config('DebugListen', config('master')); $Command = []; $Text = ''; diff --git a/public/noticeProcessor.php b/public/noticeProcessor.php index 935acd9..36f3872 100644 --- a/public/noticeProcessor.php +++ b/public/noticeProcessor.php @@ -8,7 +8,22 @@ if($Event['user_id'] != config('bot')){ $Queue[]= sendBack(CQCode::At($Event['user_id']).' 欢迎加入本群,请阅读群公告!'); }else{ - $Queue[]= sendBack('kjBot 已入驻本群,发送 '.config('prefix', '!').'help 查看帮助'); + $Queue[]= sendBack('kjBot 已入驻本群,发送 '.config('prefix', '!').'help 查看帮助'."\nkjBot 用户协议:https://github.com/kjBot-Dev/TOS/blob/master/README.md"); + } + break; + case 'group_decrease': + if($Event['sub_type']=='kick_me'){ + $Queue[]= sendMaster('Being kicked from group '.$Event['group_id'].' by '.$Event['operator_id']); + } + break; + case 'group_admin': + if($Event['user_id'] == config('bot')){ + if($Event['sub_type']=='set'){ + $prefix = 'Get '; + }elseif($Event['sub_type']=='unset'){ + $prefix = 'Lost '; + } + $Queue[]= sendMaster($prefix.'admin in group '.$Event['group_id']); } break; default: diff --git a/public/tools/frame.php b/public/tools/frame.php index 0e4a334..8d1b306 100644 --- a/public/tools/frame.php +++ b/public/tools/frame.php @@ -252,13 +252,15 @@ function leave($msg = '', $code = 0){ * @return bool */ function inBlackList($qq):bool{ - $blackList = getData('black.txt'); + $blackList = getData('blacklist.json'); if($blackList === false)return false; //无法打开黑名单时不再抛异常 - if(strpos($blackList, ''.$qq) !== false){ - return true; - }else{ - return false; + $blackList = json_decode($blackList)->list; + foreach($blackList as $person){ + if($qq == $person->id){ + return true; + } } + return false; } function block($qq){