From 0a4bdf9343a56c274107c548a528f25fc1708e34 Mon Sep 17 00:00:00 2001 From: sunxyw <31698606+sunxyw@users.noreply.github.com> Date: Tue, 10 Jan 2023 11:21:35 +0800 Subject: [PATCH] fix event segment (#87) --- src/OneBot/V12/Object/OneBotEvent.php | 2 +- tests/OneBot/V12/Object/OneBotEventTest.php | 47 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/OneBot/V12/Object/OneBotEventTest.php diff --git a/src/OneBot/V12/Object/OneBotEvent.php b/src/OneBot/V12/Object/OneBotEvent.php index 4437a27..c2c9ddf 100644 --- a/src/OneBot/V12/Object/OneBotEvent.php +++ b/src/OneBot/V12/Object/OneBotEvent.php @@ -130,7 +130,7 @@ public function getMessage(bool $return_assoc_array = false): ?array } $this->message_segment_cache = []; foreach ($this->data['message'] as $segment) { - $this->message_segment_cache[] = new MessageSegment($segment['type'], $segment['data']); + $this->message_segment_cache[] = $segment instanceof MessageSegment ? $segment : new MessageSegment($segment['type'], $segment['data']); } return $this->message_segment_cache; } diff --git a/tests/OneBot/V12/Object/OneBotEventTest.php b/tests/OneBot/V12/Object/OneBotEventTest.php new file mode 100644 index 0000000..a2b8d2c --- /dev/null +++ b/tests/OneBot/V12/Object/OneBotEventTest.php @@ -0,0 +1,47 @@ + '123', + 'type' => 'message', + 'self' => [ + 'user_id' => '123', + 'platform' => 'test', + ], + 'detail_type' => 'group', + 'sub_type' => 'normal', + 'time' => 123, + 'alt_message' => '123', + 'group_id' => '123', + 'user_id' => '123', + 'guild_id' => '123', + 'channel_id' => '123', + 'operator_id' => '123', + 'message_id' => '123', + 'message' => [ + [ + 'type' => 'text', + 'data' => [ + 'text' => '123', + ], + ], + ], + ]); + $event->setMessage([ob_segment('mention', ['user_id' => '123456'])]); + $this->assertInstanceOf(MessageSegment::class, $event->getMessage()[0]); + } +}