📝 将 Satori 用户缓存更新日志的输出时机由原来的每次调用函数调整为每次用户信息发生变更

This commit is contained in:
Expliyh 2024-05-17 14:42:37 +08:00
parent 322ad19889
commit 077658c68d
No known key found for this signature in database
GPG Key ID: F30DE0CFF6157916
2 changed files with 32 additions and 6 deletions

View File

@ -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")

View File

@ -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