From 077658c68d9c542ab135f4da23b6e353cae37eaf Mon Sep 17 00:00:00 2001 From: Expliyh Date: Fri, 17 May 2024 14:42:37 +0800 Subject: [PATCH] =?UTF-8?q?:pencil:=20=E5=B0=86=20Satori=20=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=BC=93=E5=AD=98=E6=9B=B4=E6=96=B0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=9A=84=E8=BE=93=E5=87=BA=E6=97=B6=E6=9C=BA=E7=94=B1=E5=8E=9F?= =?UTF-8?q?=E6=9D=A5=E7=9A=84=E6=AF=8F=E6=AC=A1=E8=B0=83=E7=94=A8=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E8=B0=83=E6=95=B4=E4=B8=BA=E6=AF=8F=E6=AC=A1=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BF=A1=E6=81=AF=E5=8F=91=E7=94=9F=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteyuki_satori_user_info/auto_update.py | 8 ++--- liteyuki/utils/satori_utils/user_info.py | 30 +++++++++++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/liteyuki/plugins/liteyuki_satori_user_info/auto_update.py b/liteyuki/plugins/liteyuki_satori_user_info/auto_update.py index 08080f4..8b6199e 100644 --- a/liteyuki/plugins/liteyuki_satori_user_info/auto_update.py +++ b/liteyuki/plugins/liteyuki_satori_user_info/auto_update.py @@ -1,3 +1,5 @@ +import nonebot + from liteyuki.utils import satori_utils from nonebot.message import event_preprocessor # from nonebot_plugin_alconna.typings import Event @@ -10,13 +12,11 @@ from liteyuki.plugins.liteyuki_status.counter_for_satori import satori_counter @event_preprocessor async def pre_handle(event: Event): - print("UPDATE_USER") - print(event.__dict__) if isinstance(event, satori.MessageEvent): if event.user.id == event.self_id: satori_counter.msg_sent += 1 else: satori_counter.msg_received += 1 if event.user.name is not None: - await satori_utils.user_infos.put(event.user) - print(event.user) + if await satori_utils.user_infos.put(event.user): + nonebot.logger.info(f"Satori user {event.user.name}<{event.user.id}> updated") diff --git a/liteyuki/utils/satori_utils/user_info.py b/liteyuki/utils/satori_utils/user_info.py index f4b9b4b..6500e71 100644 --- a/liteyuki/utils/satori_utils/user_info.py +++ b/liteyuki/utils/satori_utils/user_info.py @@ -28,8 +28,34 @@ class UserInfo: except KeyError: return None - async def put(self, user: User): - self.user_infos[str(user.id)] = user + async def put(self, user: User) -> bool: + """ + 向用户信息数据库中添加/修改一项,返回值仅代表数据是否变更,不代表操作是否成功 + Args: + user: 要加入数据库的用户 + + Returns: 当数据库中用户信息发生变化时返回 True, 否则返回 False + + """ + try: + old_user: User = self.user_infos[str(user.id)] + attr_edited = False + if user.name is not None: + if old_user.name != user.name: + attr_edited = True + self.user_infos[str(user.id)].name = user.name + if user.nick is not None: + if old_user.nick != user.nick: + attr_edited = True + self.user_infos[str(user.id)].nick = user.nick + if user.avatar is not None: + if old_user.avatar != user.avatar: + attr_edited = True + self.user_infos[str(user.id)].avatar = user.avatar + return attr_edited + except KeyError: + self.user_infos[str(user.id)] = user + return True def __init__(self): pass