From 11142253fb205bbb2da6e36f6b8f35475e77b39c Mon Sep 17 00:00:00 2001 From: student_2333 Date: Mon, 12 Feb 2024 17:53:50 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20Fix:=20`Message.=5F=5Fcontains=5F=5F()`?= =?UTF-8?q?=20=E6=9C=AA=E8=80=83=E8=99=91=20`bool(MessageSegment)`=20?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=20False=20=E6=83=85=E5=86=B5=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E5=BC=82=E5=B8=B8=E7=BB=93=E6=9E=9C=20(#2572?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> --- nonebot/internal/adapter/message.py | 2 +- tests/test_adapters/test_message.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/nonebot/internal/adapter/message.py b/nonebot/internal/adapter/message.py index d83a6893..0d1e25b0 100644 --- a/nonebot/internal/adapter/message.py +++ b/nonebot/internal/adapter/message.py @@ -285,7 +285,7 @@ class Message(List[TMS], abc.ABC): 消息内是否存在给定消息段或给定类型的消息段 """ if isinstance(value, str): - return bool(next((seg for seg in self if seg.type == value), None)) + return next((seg for seg in self if seg.type == value), None) is not None return super().__contains__(value) def has(self, value: Union[TMS, str]) -> bool: diff --git a/tests/test_adapters/test_message.py b/tests/test_adapters/test_message.py index 7c20fbe7..9ca21c39 100644 --- a/tests/test_adapters/test_message.py +++ b/tests/test_adapters/test_message.py @@ -192,6 +192,11 @@ def test_message_contains(): assert message.has("foo") is False assert "foo" not in message + assert not bool(FakeMessageSegment.text("")) + msg_with_empty_seg = FakeMessage([FakeMessageSegment.text("")]) + assert msg_with_empty_seg.has("text") is True + assert "text" in msg_with_empty_seg + def test_message_only(): message = FakeMessage(