From 0ca8d36cb6cd543f6f8074aa0f3568c8e94d7ab1 Mon Sep 17 00:00:00 2001 From: B1gM8c <89020353+B1gM8c@users.noreply.github.com> Date: Sun, 19 Mar 2023 20:18:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E7=94=B5=E8=84=91=E7=89=88=E5=9C=A8=E7=BE=A4=E4=B8=AD@?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E6=97=B6=E5=80=99=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=BA=9Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复微信电脑版在群中@机器人时候的一些bug --- src/wechaty/user/message.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/wechaty/user/message.py b/src/wechaty/user/message.py index a711326d..b0e79e3c 100644 --- a/src/wechaty/user/message.py +++ b/src/wechaty/user/message.py @@ -457,9 +457,11 @@ async def id_to_contact(contact_id: str) -> Contact: return contact # TODO -> change to python async best practice - contacts = [ - await id_to_contact(contact_id) - for contact_id in self.payload.mention_ids] + contacts = [] + for contact_id in self.payload.mention_ids: + if contact_id: + contact = await id_to_contact(contact_id) + contacts.append(contact) return contacts # TODO -> have to check that mention_id is not in room situation From f8b2d9cb389fb5915ef88697654a6f1ff1a5d1d8 Mon Sep 17 00:00:00 2001 From: B1gM8c <89020353+B1gM8c@users.noreply.github.com> Date: Sun, 19 Mar 2023 23:57:06 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8Dcontacts=E8=8E=B7=E5=8F=96=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B9=B6=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E8=81=94=E7=B3=BB=E4=BA=BA=E7=BC=93=E5=AD=98=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 防止频繁获取重复的联系人,加入get_cached_contacts函数,将获取到的联系人缓存起来。 --- src/wechaty/user/message.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/wechaty/user/message.py b/src/wechaty/user/message.py index b0e79e3c..f6333892 100644 --- a/src/wechaty/user/message.py +++ b/src/wechaty/user/message.py @@ -450,23 +450,25 @@ async def mention_list(self) -> List[Contact]: # Use mention list if mention list is available # otherwise, process the message and get the mention list + if self.payload is not None and self.payload.mention_ids is not None: - async def id_to_contact(contact_id: str) -> Contact: - contact = self.wechaty.Contact.load(contact_id) - await contact.ready() - return contact - - # TODO -> change to python async best practice - contacts = [] - for contact_id in self.payload.mention_ids: - if contact_id: - contact = await id_to_contact(contact_id) - contacts.append(contact) + mention_ids = self.payload.mention_ids + contact_list = await self.get_cached_contacts() + contacts = [contact for contact in contact_list if contact.contact_id in mention_ids] return contacts # TODO -> have to check that mention_id is not in room situation return [] + async def get_cached_contacts(self) -> List[Contact]: + """ + Get cached contacts or load them from Wechaty. + """ + if not hasattr(self, '_cached_contacts'): + contact_list = await self.wechaty.Contact.find_all() + self._cached_contacts = {contact.contact_id: contact for contact in contact_list} + return list(self._cached_contacts.values()) + async def mention_text(self) -> str: """ get mention text