mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-08 01:05:37 +08:00
add cs fixer and PHPStan and activate it (build 436)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Module;
|
||||
|
||||
@@ -22,60 +23,78 @@ use ZM\Utils\SingletonTrait;
|
||||
|
||||
/**
|
||||
* Class QQBot
|
||||
* @package ZM\Module
|
||||
*/
|
||||
class QQBot
|
||||
{
|
||||
use SingletonTrait;
|
||||
|
||||
public function handleByEvent() {
|
||||
public function handleByEvent()
|
||||
{
|
||||
$data = json_decode(context()->getFrame()->data, true);
|
||||
$this->handle($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param int $level
|
||||
* @param int $level
|
||||
* @throws InterruptException
|
||||
*/
|
||||
public function handle($data, $level = 0) {
|
||||
public function handle($data, $level = 0)
|
||||
{
|
||||
try {
|
||||
if ($level > 10) return;
|
||||
set_coroutine_params(["data" => $data]);
|
||||
if (isset($data["post_type"])) {
|
||||
if ($level > 10) {
|
||||
return;
|
||||
}
|
||||
set_coroutine_params(['data' => $data]);
|
||||
if (isset($data['post_type'])) {
|
||||
//echo TermColor::ITALIC.json_encode($data, 128|256).TermColor::RESET.PHP_EOL;
|
||||
ctx()->setCache("level", $level);
|
||||
ctx()->setCache('level', $level);
|
||||
//Console::debug("Calling CQ Event from fd=" . ctx()->getConnection()->getFd());
|
||||
if ($data["post_type"] != "meta_event") {
|
||||
$r = $this->dispatchBeforeEvents($data, "pre"); // before在这里执行,元事件不执行before为减少不必要的调试日志
|
||||
if ($r->store === "block") EventDispatcher::interrupt();
|
||||
if ($data['post_type'] != 'meta_event') {
|
||||
$r = $this->dispatchBeforeEvents($data, 'pre'); // before在这里执行,元事件不执行before为减少不必要的调试日志
|
||||
if ($r->store === 'block') {
|
||||
EventDispatcher::interrupt();
|
||||
}
|
||||
}
|
||||
//Console::warning("最上数据包:".json_encode($data));
|
||||
}
|
||||
if (isset($data["echo"]) || isset($data["post_type"])) {
|
||||
if (CoMessage::resumeByWS()) EventDispatcher::interrupt();
|
||||
if (isset($data['echo']) || isset($data['post_type'])) {
|
||||
if (CoMessage::resumeByWS()) {
|
||||
EventDispatcher::interrupt();
|
||||
}
|
||||
}
|
||||
if (($data["post_type"] ?? "meta_event") != "meta_event") {
|
||||
$r = $this->dispatchBeforeEvents($data, "post"); // before在这里执行,元事件不执行before为减少不必要的调试日志
|
||||
if ($r->store === "block") EventDispatcher::interrupt();
|
||||
if (($data['post_type'] ?? 'meta_event') != 'meta_event') {
|
||||
$r = $this->dispatchBeforeEvents($data, 'post'); // before在这里执行,元事件不执行before为减少不必要的调试日志
|
||||
if ($r->store === 'block') {
|
||||
EventDispatcher::interrupt();
|
||||
}
|
||||
}
|
||||
if (isset($data['post_type'])) {
|
||||
$this->dispatchEvents($data);
|
||||
} else {
|
||||
$this->dispatchAPIResponse($data);
|
||||
}
|
||||
if (isset($data["post_type"])) $this->dispatchEvents($data);
|
||||
else $this->dispatchAPIResponse($data);
|
||||
|
||||
if (($data["post_type"] ?? "meta_event") != "meta_event") {
|
||||
if (($data['post_type'] ?? 'meta_event') != 'meta_event') {
|
||||
$r = $this->dispatchAfterEvents($data); // before在这里执行,元事件不执行before为减少不必要的调试日志
|
||||
if ($r->store === "block") EventDispatcher::interrupt();
|
||||
if ($r->store === 'block') {
|
||||
EventDispatcher::interrupt();
|
||||
}
|
||||
}
|
||||
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (WaitTimeoutException $e) {
|
||||
if (($data["post_type"] ?? "meta_event") != "meta_event") {
|
||||
} /* @noinspection PhpRedundantCatchClauseInspection */ catch (WaitTimeoutException $e) {
|
||||
if (($data['post_type'] ?? 'meta_event') != 'meta_event') {
|
||||
$r = $this->dispatchAfterEvents($data); // before在这里执行,元事件不执行before为减少不必要的调试日志
|
||||
if ($r->store === "block") EventDispatcher::interrupt();
|
||||
if ($r->store === 'block') {
|
||||
EventDispatcher::interrupt();
|
||||
}
|
||||
}
|
||||
$e->module->finalReply($e->getMessage());
|
||||
} catch (InterruptException $e) {
|
||||
if (($data["post_type"] ?? "meta_event") != "meta_event") {
|
||||
if (($data['post_type'] ?? 'meta_event') != 'meta_event') {
|
||||
$r = $this->dispatchAfterEvents($data); // before在这里执行,元事件不执行before为减少不必要的调试日志
|
||||
if ($r->store === "block") EventDispatcher::interrupt();
|
||||
if ($r->store === 'block') {
|
||||
EventDispatcher::interrupt();
|
||||
}
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
@@ -85,22 +104,24 @@ class QQBot
|
||||
/**
|
||||
* @param $data
|
||||
* @param $time
|
||||
* @return EventDispatcher
|
||||
* @throws Exception
|
||||
*/
|
||||
public function dispatchBeforeEvents($data, $time): EventDispatcher {
|
||||
public function dispatchBeforeEvents($data, $time): EventDispatcher
|
||||
{
|
||||
$before = new EventDispatcher(CQBefore::class);
|
||||
if ($time === "pre") {
|
||||
if ($time === 'pre') {
|
||||
$before->setRuleFunction(function ($v) use ($data) {
|
||||
return $v->level >= 200 && $v->cq_event == $data["post_type"];
|
||||
return $v->level >= 200 && $v->cq_event == $data['post_type'];
|
||||
});
|
||||
} else {
|
||||
$before->setRuleFunction(function ($v) use ($data) {
|
||||
return $v->level < 200 && $v->cq_event == $data["post_type"];
|
||||
return $v->level < 200 && $v->cq_event == $data['post_type'];
|
||||
});
|
||||
}
|
||||
$before->setReturnFunction(function ($result) {
|
||||
if (!$result) EventDispatcher::interrupt("block");
|
||||
if (!$result) {
|
||||
EventDispatcher::interrupt('block');
|
||||
}
|
||||
});
|
||||
$before->dispatchEvents($data);
|
||||
return $before;
|
||||
@@ -111,23 +132,32 @@ class QQBot
|
||||
* @throws InterruptException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function dispatchEvents($data) {
|
||||
private function dispatchEvents($data)
|
||||
{
|
||||
//Console::warning("最xia数据包:".json_encode($data));
|
||||
switch ($data["post_type"]) {
|
||||
case "message":
|
||||
switch ($data['post_type']) {
|
||||
case 'message':
|
||||
//分发CQCommand事件
|
||||
$dispatcher = new EventDispatcher(CQCommand::class);
|
||||
$dispatcher->setReturnFunction(function ($result) {
|
||||
if (is_string($result)) ctx()->reply($result);
|
||||
if (ctx()->getCache("has_reply") === true) EventDispatcher::interrupt();
|
||||
if (is_string($result)) {
|
||||
ctx()->reply($result);
|
||||
}
|
||||
if (ctx()->getCache('has_reply') === true) {
|
||||
EventDispatcher::interrupt();
|
||||
}
|
||||
});
|
||||
$s = MessageUtil::matchCommand(ctx()->getStringMessage(), ctx()->getData());
|
||||
if ($s->status !== false) {
|
||||
if (!empty($s->match)) ctx()->setCache("match", $s->match);
|
||||
if (!empty($s->match)) {
|
||||
ctx()->setCache('match', $s->match);
|
||||
}
|
||||
$dispatcher->dispatchEvent($s->object, null);
|
||||
if (is_string($dispatcher->store)) ctx()->reply($dispatcher->store);
|
||||
if (ctx()->getCache("has_reply") === true) {
|
||||
$policy = ZMConfig::get("global", "onebot")['message_command_policy'] ?? 'interrupt';
|
||||
if (is_string($dispatcher->store)) {
|
||||
ctx()->reply($dispatcher->store);
|
||||
}
|
||||
if (ctx()->getCache('has_reply') === true) {
|
||||
$policy = ZMConfig::get('global', 'onebot')['message_command_policy'] ?? 'interrupt';
|
||||
switch ($policy) {
|
||||
case 'interrupt':
|
||||
EventDispatcher::interrupt();
|
||||
@@ -135,7 +165,7 @@ class QQBot
|
||||
case 'continue':
|
||||
break;
|
||||
default:
|
||||
throw new Exception("未知的消息命令策略:" . $policy);
|
||||
throw new Exception('未知的消息命令策略:' . $policy);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,54 +173,57 @@ class QQBot
|
||||
//分发CQMessage事件
|
||||
$msg_dispatcher = new EventDispatcher(CQMessage::class);
|
||||
$msg_dispatcher->setRuleFunction(function ($v) {
|
||||
return ($v->message == '' || ($v->message == ctx()->getStringMessage())) &&
|
||||
($v->user_id == 0 || ($v->user_id == ctx()->getUserId())) &&
|
||||
($v->group_id == 0 || ($v->group_id == (ctx()->getGroupId() ?? 0))) &&
|
||||
($v->message_type == '' || ($v->message_type == ctx()->getMessageType())) &&
|
||||
($v->raw_message == '' || ($v->raw_message == context()->getData()["raw_message"]));
|
||||
return ($v->message == '' || ($v->message == ctx()->getStringMessage()))
|
||||
&& ($v->user_id == 0 || ($v->user_id == ctx()->getUserId()))
|
||||
&& ($v->group_id == 0 || ($v->group_id == (ctx()->getGroupId() ?? 0)))
|
||||
&& ($v->message_type == '' || ($v->message_type == ctx()->getMessageType()))
|
||||
&& ($v->raw_message == '' || ($v->raw_message == context()->getData()['raw_message']));
|
||||
});
|
||||
$msg_dispatcher->setReturnFunction(function ($result) {
|
||||
if (is_string($result)) ctx()->reply($result);
|
||||
if (is_string($result)) {
|
||||
ctx()->reply($result);
|
||||
}
|
||||
});
|
||||
$msg_dispatcher->dispatchEvents(ctx()->getMessage());
|
||||
return;
|
||||
case "meta_event":
|
||||
case 'meta_event':
|
||||
//Console::success("当前数据包:".json_encode(ctx()->getData()));
|
||||
$dispatcher = new EventDispatcher(CQMetaEvent::class);
|
||||
$dispatcher->setRuleFunction(function (CQMetaEvent $v) {
|
||||
return ($v->meta_event_type == '' || ($v->meta_event_type != '' && $v->meta_event_type == ctx()->getData()["meta_event_type"]));
|
||||
return $v->meta_event_type == '' || ($v->meta_event_type != '' && $v->meta_event_type == ctx()->getData()['meta_event_type']);
|
||||
});
|
||||
//eval(BP);
|
||||
$dispatcher->dispatchEvents(ctx()->getData());
|
||||
return;
|
||||
case "notice":
|
||||
case 'notice':
|
||||
$dispatcher = new EventDispatcher(CQNotice::class);
|
||||
$dispatcher->setRuleFunction(function (CQNotice $v) {
|
||||
return
|
||||
($v->notice_type == '' || ($v->notice_type != '' && $v->notice_type == ctx()->getData()["notice_type"])) &&
|
||||
($v->sub_type == '' || ($v->sub_type != '' && $v->sub_type == ctx()->getData()["sub_type"])) &&
|
||||
($v->group_id == '' || ($v->group_id != '' && $v->group_id == ctx()->getData()["group_id"])) &&
|
||||
($v->operator_id == '' || ($v->operator_id != '' && $v->operator_id == ctx()->getData()["operator_id"]));
|
||||
($v->notice_type == '' || ($v->notice_type != '' && $v->notice_type == ctx()->getData()['notice_type']))
|
||||
&& ($v->sub_type == '' || ($v->sub_type != '' && $v->sub_type == ctx()->getData()['sub_type']))
|
||||
&& ($v->group_id == '' || ($v->group_id != '' && $v->group_id == ctx()->getData()['group_id']))
|
||||
&& ($v->operator_id == '' || ($v->operator_id != '' && $v->operator_id == ctx()->getData()['operator_id']));
|
||||
});
|
||||
$dispatcher->dispatchEvents(ctx()->getData());
|
||||
return;
|
||||
case "request":
|
||||
case 'request':
|
||||
$dispatcher = new EventDispatcher(CQRequest::class);
|
||||
$dispatcher->setRuleFunction(function (CQRequest $v) {
|
||||
return ($v->request_type == '' || ($v->request_type != '' && $v->request_type == ctx()->getData()['request_type'])) &&
|
||||
($v->sub_type == '' || ($v->sub_type != '' && $v->sub_type == ctx()->getData()['sub_type'])) &&
|
||||
($v->user_id == 0 || ($v->user_id != 0 && $v->user_id == ctx()->getData()["user_id"])) &&
|
||||
($v->comment == '' || ($v->comment != '' && $v->comment == ctx()->getData()['comment']));
|
||||
return ($v->request_type == '' || ($v->request_type != '' && $v->request_type == ctx()->getData()['request_type']))
|
||||
&& ($v->sub_type == '' || ($v->sub_type != '' && $v->sub_type == ctx()->getData()['sub_type']))
|
||||
&& ($v->user_id == 0 || ($v->user_id != 0 && $v->user_id == ctx()->getData()['user_id']))
|
||||
&& ($v->comment == '' || ($v->comment != '' && $v->comment == ctx()->getData()['comment']));
|
||||
});
|
||||
$dispatcher->dispatchEvents(ctx()->getData());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private function dispatchAfterEvents($data): EventDispatcher {
|
||||
private function dispatchAfterEvents($data): EventDispatcher
|
||||
{
|
||||
$after = new EventDispatcher(CQAfter::class);
|
||||
$after->setRuleFunction(function ($v) use ($data) {
|
||||
return $v->cq_event == $data["post_type"];
|
||||
return $v->cq_event == $data['post_type'];
|
||||
});
|
||||
$after->dispatchEvents($data);
|
||||
return $after;
|
||||
@@ -200,11 +233,12 @@ class QQBot
|
||||
* @param $req
|
||||
* @throws Exception
|
||||
*/
|
||||
private function dispatchAPIResponse($req) {
|
||||
set_coroutine_params(["cq_response" => $req]);
|
||||
private function dispatchAPIResponse($req)
|
||||
{
|
||||
set_coroutine_params(['cq_response' => $req]);
|
||||
$dispatcher = new EventDispatcher(CQAPIResponse::class);
|
||||
$dispatcher->setRuleFunction(function (CQAPIResponse $response) {
|
||||
return $response->retcode == ctx()->getCQResponse()["retcode"];
|
||||
return $response->retcode == ctx()->getCQResponse()['retcode'];
|
||||
});
|
||||
$dispatcher->dispatchEvents($req);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user