mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-12 11:15:34 +08:00
add cs fixer and PHPStan and activate it (build 436)
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
<?php /** @noinspection PhpUnused */
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Utils;
|
||||
|
||||
|
||||
use Exception;
|
||||
use ZM\Annotation\CQ\CQCommand;
|
||||
use ZM\API\CQ;
|
||||
use ZM\Config\ZMConfig;
|
||||
@@ -11,34 +12,37 @@ use ZM\Console\Console;
|
||||
use ZM\Entity\MatchResult;
|
||||
use ZM\Event\EventManager;
|
||||
use ZM\Requests\ZMRequest;
|
||||
use ZM\Utils\Manager\ProcessManager;
|
||||
use ZM\Utils\Manager\WorkerManager;
|
||||
|
||||
class MessageUtil
|
||||
{
|
||||
/**
|
||||
* 下载消息中 CQ 码的所有图片,通过 url
|
||||
* @param $msg
|
||||
* @param null $path
|
||||
* @param null $path
|
||||
* @return array|false
|
||||
*/
|
||||
public static function downloadCQImage($msg, $path = null) {
|
||||
$path = $path ?? DataProvider::getDataFolder() . "images/";
|
||||
if (!is_dir($path)) @mkdir($path);
|
||||
public static function downloadCQImage($msg, $path = null)
|
||||
{
|
||||
$path = $path ?? DataProvider::getDataFolder() . 'images/';
|
||||
if (!is_dir($path)) {
|
||||
@mkdir($path);
|
||||
}
|
||||
$path = realpath($path);
|
||||
if ($path === false) {
|
||||
Console::warning(zm_internal_errcode("E00059") . "指定的路径错误不存在!");
|
||||
Console::warning(zm_internal_errcode('E00059') . '指定的路径错误不存在!');
|
||||
return false;
|
||||
}
|
||||
$files = [];
|
||||
$cq = CQ::getAllCQ($msg, true);
|
||||
foreach ($cq as $v) {
|
||||
if ($v->type == "image") {
|
||||
$result = ZMRequest::downloadFile($v->params["url"], $path . "/" . $v->params["file"]);
|
||||
if ($v->type == 'image') {
|
||||
$result = ZMRequest::downloadFile($v->params['url'], $path . '/' . $v->params['file']);
|
||||
if ($result === false) {
|
||||
Console::warning(zm_internal_errcode("E00060") . "图片 " . $v->params["url"] . " 下载失败!");
|
||||
Console::warning(zm_internal_errcode('E00060') . '图片 ' . $v->params['url'] . ' 下载失败!');
|
||||
return false;
|
||||
}
|
||||
$files[] = $path . "/" . $v->params["file"];
|
||||
$files[] = $path . '/' . $v->params['file'];
|
||||
}
|
||||
}
|
||||
return $files;
|
||||
@@ -47,19 +51,20 @@ class MessageUtil
|
||||
/**
|
||||
* 检查消息中是否含有图片 CQ 码
|
||||
* @param $msg
|
||||
* @return bool
|
||||
*/
|
||||
public static function containsImage($msg): bool {
|
||||
public static function containsImage($msg): bool
|
||||
{
|
||||
$cq = CQ::getAllCQ($msg, true);
|
||||
foreach ($cq as $v) {
|
||||
if ($v->type == "image") {
|
||||
if ($v->type == 'image') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isAtMe($msg, $me_id): bool {
|
||||
public static function isAtMe($msg, $me_id): bool
|
||||
{
|
||||
return strpos($msg, CQ::at($me_id)) !== false;
|
||||
}
|
||||
|
||||
@@ -69,20 +74,19 @@ class MessageUtil
|
||||
* type == 1 : 返回图片的 file://路径 CQ 码(路径必须为绝对路径)
|
||||
* type == 2 : 返回图片的 http://xxx CQ 码(默认为 /images/ 路径就是文件对应所在的目录)
|
||||
* @param $file
|
||||
* @param int $type
|
||||
* @return string
|
||||
*/
|
||||
public static function getImageCQFromLocal($file, int $type = 0): string {
|
||||
public static function getImageCQFromLocal($file, int $type = 0): string
|
||||
{
|
||||
switch ($type) {
|
||||
case 0:
|
||||
return CQ::image("base64://" . base64_encode(file_get_contents($file)));
|
||||
return CQ::image('base64://' . base64_encode(file_get_contents($file)));
|
||||
case 1:
|
||||
return CQ::image("file://" . $file);
|
||||
return CQ::image('file://' . $file);
|
||||
case 2:
|
||||
$info = pathinfo($file);
|
||||
return CQ::image(ZMConfig::get("global", "http_reverse_link") . "/images/" . $info["basename"]);
|
||||
return CQ::image(ZMConfig::get('global', 'http_reverse_link') . '/images/' . $info['basename']);
|
||||
}
|
||||
return "";
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,12 +94,15 @@ class MessageUtil
|
||||
* @param $msg
|
||||
* @return array|string[]
|
||||
*/
|
||||
public static function splitCommand($msg): array {
|
||||
$word = explodeMsg(str_replace("\r", "", $msg));
|
||||
if (empty($word)) $word = [""];
|
||||
public static function splitCommand($msg): array
|
||||
{
|
||||
$word = explodeMsg(str_replace("\r", '', $msg));
|
||||
if (empty($word)) {
|
||||
$word = [''];
|
||||
}
|
||||
if (count(explode("\n", $word[0])) >= 2) {
|
||||
$enter = explode("\n", $msg);
|
||||
$first = split_explode(" ", array_shift($enter));
|
||||
$first = split_explode(' ', array_shift($enter));
|
||||
$word = array_merge($first, $enter);
|
||||
foreach ($word as $k => $v) {
|
||||
$word[$k] = trim($v);
|
||||
@@ -107,9 +114,9 @@ class MessageUtil
|
||||
/**
|
||||
* @param $msg
|
||||
* @param $obj
|
||||
* @return MatchResult
|
||||
*/
|
||||
public static function matchCommand($msg, $obj): MatchResult {
|
||||
public static function matchCommand($msg, $obj): MatchResult
|
||||
{
|
||||
$ls = EventManager::$events[CQCommand::class] ?? [];
|
||||
if (is_array($msg)) {
|
||||
$msg = self::arrayToStr($msg);
|
||||
@@ -117,33 +124,39 @@ class MessageUtil
|
||||
$word = self::splitCommand($msg);
|
||||
$matched = new MatchResult();
|
||||
foreach ($ls as $v) {
|
||||
if (array_diff([$v->match, $v->pattern, $v->regex, $v->keyword, $v->end_with, $v->start_with], [""]) == []) continue;
|
||||
elseif (($v->user_id == 0 || ($v->user_id == $obj["user_id"])) &&
|
||||
($v->group_id == 0 || ($v->group_id == ($obj["group_id"] ?? 0))) &&
|
||||
($v->message_type == '' || ($v->message_type == $obj["message_type"]))
|
||||
if (array_diff([$v->match, $v->pattern, $v->regex, $v->keyword, $v->end_with, $v->start_with], ['']) == []) {
|
||||
continue;
|
||||
}
|
||||
if (($v->user_id == 0 || ($v->user_id == $obj['user_id']))
|
||||
&& ($v->group_id == 0 || ($v->group_id == ($obj['group_id'] ?? 0)))
|
||||
&& ($v->message_type == '' || ($v->message_type == $obj['message_type']))
|
||||
) {
|
||||
if (($word[0] != "" && $v->match == $word[0]) || in_array($word[0], $v->alias)) {
|
||||
if (($word[0] != '' && $v->match == $word[0]) || in_array($word[0], $v->alias)) {
|
||||
array_shift($word);
|
||||
$matched->match = $word;
|
||||
$matched->object = $v;
|
||||
$matched->status = true;
|
||||
break;
|
||||
} elseif ($v->start_with != "" && mb_substr($msg, 0, mb_strlen($v->start_with)) === $v->start_with) {
|
||||
}
|
||||
if ($v->start_with != '' && mb_substr($msg, 0, mb_strlen($v->start_with)) === $v->start_with) {
|
||||
$matched->match = [mb_substr($msg, mb_strlen($v->start_with))];
|
||||
$matched->object = $v;
|
||||
$matched->status = true;
|
||||
break;
|
||||
} elseif ($v->end_with != "" && mb_substr($msg, 0 - mb_strlen($v->end_with)) === $v->end_with) {
|
||||
}
|
||||
if ($v->end_with != '' && mb_substr($msg, 0 - mb_strlen($v->end_with)) === $v->end_with) {
|
||||
$matched->match = [substr($msg, 0, strripos($msg, $v->end_with))];
|
||||
$matched->object = $v;
|
||||
$matched->status = true;
|
||||
break;
|
||||
} elseif ($v->keyword != "" && mb_strpos($msg, $v->keyword) !== false) {
|
||||
}
|
||||
if ($v->keyword != '' && mb_strpos($msg, $v->keyword) !== false) {
|
||||
$matched->match = explode($v->keyword, $msg);
|
||||
$matched->object = $v;
|
||||
$matched->status = true;
|
||||
break;
|
||||
} elseif ($v->pattern != "") {
|
||||
}
|
||||
if ($v->pattern != '') {
|
||||
$match = matchArgs($v->pattern, $msg);
|
||||
if ($match !== false) {
|
||||
$matched->match = $match;
|
||||
@@ -151,8 +164,8 @@ class MessageUtil
|
||||
$matched->status = true;
|
||||
break;
|
||||
}
|
||||
} elseif ($v->regex != "") {
|
||||
if (preg_match("/" . $v->regex . "/u", $msg, $word2) != 0) {
|
||||
} elseif ($v->regex != '') {
|
||||
if (preg_match('/' . $v->regex . '/u', $msg, $word2) != 0) {
|
||||
$matched->match = $word2;
|
||||
$matched->object = $v;
|
||||
$matched->status = true;
|
||||
@@ -164,20 +177,24 @@ class MessageUtil
|
||||
return $matched;
|
||||
}
|
||||
|
||||
public static function addShortCommand($command, string $reply) {
|
||||
/**
|
||||
* @param $command
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function addShortCommand($command, string $reply)
|
||||
{
|
||||
for ($i = 0; $i < ZM_WORKER_NUM; ++$i) {
|
||||
ProcessManager::sendActionToWorker($i, "add_short_command", [$command, $reply]);
|
||||
WorkerManager::sendActionToWorker($i, 'add_short_command', [$command, $reply]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转数组
|
||||
* @param $msg
|
||||
* @param bool $ignore_space
|
||||
* @param false $trim_text
|
||||
* @return array
|
||||
*/
|
||||
public static function strToArray($msg, bool $ignore_space = true, bool $trim_text = false): array {
|
||||
public static function strToArray($msg, bool $ignore_space = true, bool $trim_text = false): array
|
||||
{
|
||||
$arr = [];
|
||||
while (($rear = mb_strstr($msg, '[CQ:')) !== false && ($end = mb_strstr($rear, ']', true)) !== false) {
|
||||
// 把 [CQ: 前面的文字生成段落
|
||||
@@ -188,14 +205,14 @@ class MessageUtil
|
||||
}
|
||||
// 处理 CQ 码
|
||||
$content = mb_substr($end, 4);
|
||||
$cq = explode(",", $content);
|
||||
$cq = explode(',', $content);
|
||||
$object_type = array_shift($cq);
|
||||
$object_params = [];
|
||||
foreach ($cq as $v) {
|
||||
$key = mb_strstr($v, "=", true);
|
||||
$object_params[$key] = CQ::decode(mb_substr(mb_strstr($v, "="), 1), true);
|
||||
$key = mb_strstr($v, '=', true);
|
||||
$object_params[$key] = CQ::decode(mb_substr(mb_strstr($v, '='), 1), true);
|
||||
}
|
||||
$arr[] = ["type" => $object_type, "data" => $object_params];
|
||||
$arr[] = ['type' => $object_type, 'data' => $object_params];
|
||||
$msg = mb_substr(mb_strstr($rear, ']'), 1);
|
||||
}
|
||||
if (($trim_msg = trim($msg)) !== '' || ($msg !== '' && !$ignore_space)) {
|
||||
@@ -207,23 +224,22 @@ class MessageUtil
|
||||
/**
|
||||
* 数组转字符串
|
||||
* 纪念一下,这段代码完全由AI生成,没有人知道它是怎么写的,这句话是我自己写的,不知道是不是有人知道的
|
||||
* @param array $array
|
||||
* @return string
|
||||
* @author Copilot
|
||||
*/
|
||||
public static function arrayToStr(array $array): string {
|
||||
$str = "";
|
||||
public static function arrayToStr(array $array): string
|
||||
{
|
||||
$str = '';
|
||||
foreach ($array as $v) {
|
||||
if ($v['type'] == 'text') {
|
||||
$str .= $v['data']['text'];
|
||||
} else {
|
||||
$str .= "[CQ:" . $v['type'];
|
||||
$str .= '[CQ:' . $v['type'];
|
||||
foreach ($v['data'] as $key => $value) {
|
||||
$str .= "," . $key . "=" . CQ::encode($value, true);
|
||||
$str .= ',' . $key . '=' . CQ::encode($value, true);
|
||||
}
|
||||
$str .= "]";
|
||||
$str .= ']';
|
||||
}
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user