fix message convert bug

This commit is contained in:
crazywhalecc
2023-02-11 12:27:59 +08:00
committed by Jerry
parent e5463fc15a
commit dcce05c019
3 changed files with 7 additions and 7 deletions

View File

@@ -52,6 +52,7 @@ class AnnotationHandler
*/ */
public static function interrupt(mixed $return_var = null) public static function interrupt(mixed $return_var = null)
{ {
logger()->debug('阻断事件!');
throw new InterruptException($return_var); throw new InterruptException($return_var);
} }

View File

@@ -72,9 +72,11 @@ trait BotActionTrait
// 调用机器人连接发送 Action首先试试看是不是 WebSocket // 调用机器人连接发送 Action首先试试看是不是 WebSocket
if ($this->base_event instanceof WebSocketMessageEvent) { if ($this->base_event instanceof WebSocketMessageEvent) {
logger()->debug('使用传入的 base_event 发送消息');
$result = $this->base_event->send(json_encode($a->jsonSerialize())); $result = $this->base_event->send(json_encode($a->jsonSerialize()));
} }
if (!isset($result) && container()->has('ws.message.event')) { if (!isset($result) && container()->has('ws.message.event')) {
logger()->debug('使用容器的 Event 发送消息');
$result = container()->get('ws.message.event')->send(json_encode($a->jsonSerialize())); $result = container()->get('ws.message.event')->send(json_encode($a->jsonSerialize()));
} }
// 如果是 HTTP WebHook 的形式,那么直接调用 Response // 如果是 HTTP WebHook 的形式,那么直接调用 Response

View File

@@ -61,23 +61,20 @@ class MessageUtil
return $arr; return $arr;
} }
public static function convertToArr(MessageSegment|\Stringable|array|string $message) public static function convertToArr(MessageSegment|\Stringable|array|string $message): array
{ {
if (is_array($message)) { if (is_array($message)) {
foreach ($message as $k => $v) { foreach ($message as $k => $v) {
if (is_string($v)) { if (is_string($v)) {
$message[$k] = new MessageSegment('text', ['text' => $v]); $message[$k] = segment('text', ['text' => $v])->jsonSerialize();
} }
} }
return $message; return $message;
} }
if ($message instanceof MessageSegment) { if ($message instanceof MessageSegment) {
return [$message]; return [$message->jsonSerialize()];
} }
if ($message instanceof \Stringable) { return [segment('text', ['text' => $message])->jsonSerialize()];
return new MessageSegment('text', ['text' => $message->__toString()]);
}
return new MessageSegment('text', ['text' => $message]);
} }
/** /**