使用satori时维护一个有昵称的用户列表

get_plugin_session_enable 判断当前使用的适配器
This commit is contained in:
Expliyh 2024-05-16 19:20:54 +08:00
parent 90e7a90bcf
commit 24722447da
No known key found for this signature in database
GPG Key ID: F30DE0CFF6157916
11 changed files with 120 additions and 14 deletions

1
.gitignore vendored
View File

@ -35,3 +35,4 @@ prompt.txt
# js
**/echarts.js
.env

View File

@ -5,6 +5,7 @@ from typing import Any
import nonebot
import pip
from nonebot import Bot, get_driver, require
from nonebot.adapters import satori
from nonebot.adapters.onebot.v11 import Message, escape, unescape
from nonebot.exception import MockApiException
from nonebot.internal.matcher import Matcher
@ -16,6 +17,7 @@ from liteyuki.utils.base.language import get_user_lang
from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent
from liteyuki.utils.message.message import MarkdownMessage as md, broadcast_to_superusers
from liteyuki.utils.base.reloader import Reloader
from liteyuki.utils import satori_utils
from .api import update_liteyuki
require("nonebot_plugin_alconna")
@ -269,6 +271,8 @@ async def on_shutdown():
@driver.on_bot_connect
async def _(bot: T_Bot):
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
if isinstance(bot,satori.Bot):
await satori_utils.user_infos.load_friends(bot)
# 用于重启计时
if temp_data.data.get("reload", False):
temp_data.data["reload"] = False

View File

@ -3,6 +3,7 @@ from typing import Optional
import aiofiles
import nonebot.plugin
from nonebot.adapters import satori
from liteyuki.utils.base.data import LiteModel
from liteyuki.utils.base.data_manager import GlobalPlugin, Group, User, group_db, plugin_db, user_db
@ -95,16 +96,23 @@ def get_plugin_session_enable(event: T_MessageEvent, plugin_name: str) -> bool:
Returns:
bool: 插件当前状态
"""
if event.message_type == "group":
group_id = str(event.group_id)
if isinstance(event, satori.event.Event):
if event.guild is not None:
message_type = "group"
else:
message_type = "private"
else:
message_type = event.message_type
if message_type == "group":
group_id = str(event.guild.id if isinstance(event, satori.event.Event) else event.group_id)
if group_id not in __group_data:
group: Group = group_db.where_one(Group(), "group_id = ?", group_id, default=Group(group_id=group_id))
__group_data[str(event.group_id)] = group
__group_data[str(group_id)] = group
session = __group_data[group_id]
else:
# session: User = user_db.first(User(), "user_id = ?", event.user_id, default=User(user_id=str(event.user_id)))
user_id = str(event.user_id)
user_id = str(event.user.id if isinstance(event, satori.event.Event) else event.user_id)
if user_id not in __user_data:
user: User = user_db.where_one(User(), "user_id = ?", user_id, default=User(user_id=user_id))
__user_data[user_id] = user
@ -132,7 +140,8 @@ def set_plugin_session_enable(event: T_MessageEvent, plugin_name: str, enable: b
"""
if event.message_type == "group":
session = group_db.where_one(Group(), "group_id = ?", str(event.group_id), default=Group(group_id=str(event.group_id)))
session = group_db.where_one(Group(), "group_id = ?", str(event.group_id),
default=Group(group_id=str(event.group_id)))
else:
session = user_db.where_one(User(), "user_id = ?", str(event.user_id), default=User(user_id=str(event.user_id)))
default_enable = get_plugin_default_enable(plugin_name)

View File

@ -0,0 +1,16 @@
from nonebot.plugin import PluginMetadata
from .auto_update import *
__author__ = "expliyh"
__plugin_meta__ = PluginMetadata(
name="Satori 用户数据自动更新(临时措施)",
description="",
usage="",
type="application",
homepage="https://github.com/snowykami/LiteyukiBot",
extra={
"liteyuki": True,
"toggleable" : True,
"default_enable" : True,
}
)

View File

@ -0,0 +1,16 @@
from liteyuki.utils import satori_utils
from nonebot.message import event_preprocessor
# from nonebot_plugin_alconna.typings import Event
from liteyuki.utils.base.ly_typing import T_MessageEvent
from liteyuki.utils import satori_utils
from nonebot.adapters import satori
from nonebot_plugin_alconna.typings import Event
@event_preprocessor
async def pre_handle(event: Event):
print("UPDATE_USER")
if isinstance(event, satori.MessageEvent):
if event.user.name is not None:
await satori_utils.user_infos.put(event.user)
print(event.user)

View File

@ -4,7 +4,7 @@ from nonebot import require
from nonebot.message import event_postprocessor
from liteyuki.utils.base.data import Database, LiteModel
from liteyuki.utils.base.ly_typing import v11
from liteyuki.utils.base.ly_typing import v11, satori
from .common import MessageEventModel, msg_db
@ -36,3 +36,26 @@ async def onebot_v11_event_monitor(bot: v11.Bot, event: v11.MessageEvent):
message_type=event.message_type,
)
msg_db.save(mem)
async def satori_event_monitor(bot: satori.Bot, event: satori.MessageEvent):
if event.guild is not None:
event: satori.MessageEvent
group_id = str(event.guild.id)
else:
group_id = ""
mem = MessageEventModel(
time=int(time.time()),
bot_id=bot.self_id,
adapter="satori",
group_id=group_id,
user_id=str(event.user.id),
message_id=str(event.message.id),
message=event.message,
message_text=event.message.content,
message_type=event.message_type,
)
msg_db.save(mem)

View File

@ -52,7 +52,6 @@ def get_uni_set() -> set:
async def pre_handle(event: Event):
try:
user_id = str(event.get_user_id())
except:
return

View File

@ -1,7 +1,8 @@
from nonebot.adapters.onebot import v11, v12
from nonebot.adapters import satori
T_Bot = v11.Bot | v12.Bot
T_GroupMessageEvent = v11.GroupMessageEvent | v12.GroupMessageEvent
T_PrivateMessageEvent = v11.PrivateMessageEvent | v12.PrivateMessageEvent
T_MessageEvent = v11.MessageEvent | v12.MessageEvent
T_Message = v11.Message | v12.Message
T_Bot = v11.Bot | v12.Bot | satori.Bot
T_GroupMessageEvent = v11.GroupMessageEvent | v12.GroupMessageEvent | satori.MessageEvent
T_PrivateMessageEvent = v11.PrivateMessageEvent | v12.PrivateMessageEvent | satori.MessageEvent
T_MessageEvent = v11.MessageEvent | v12.MessageEvent | satori.MessageEvent
T_Message = v11.Message | v12.Message | satori.Message

View File

@ -0,0 +1 @@
from .user_info import user_infos

View File

@ -0,0 +1,35 @@
from nonebot.adapters import satori
from nonebot.adapters.satori.models import User
class UserInfo:
user_infos: dict = {}
async def load_friends(self, bot: satori.Bot):
print("LoadFriends")
friend_response = await bot.friend_list()
while friend_response.next is not None:
for i in friend_response.data:
i: User = i
self.user_infos[str(i.id)] = i
friend_response = await bot.friend_list(next_token=friend_response.next)
for i in friend_response.data:
i: User = i
self.user_infos[str(i.id)] = i
print(i)
async def get(self, uid: int | str) -> User | None:
try:
return self.user_infos[str(uid)]
except KeyError:
return None
async def put(self, user: User):
self.user_infos[str(user.id)] = user
def __init__(self):
pass
user_infos = UserInfo()

View File

@ -1,5 +1,6 @@
import nonebot
from nonebot.adapters.onebot import v11, v12
from nonebot.adapters import satori
from liteyuki.utils import init
from liteyuki.utils.base.config import load_from_yaml
from liteyuki.utils.base.data_manager import StoredConfig, common_db
@ -11,7 +12,7 @@ if __name__ == "__mp_main__":
static_config = load_from_yaml("config.yml")
store_config.update(static_config)
nonebot.init(**store_config)
adapters = [v11.Adapter, v12.Adapter]
adapters = [v11.Adapter, v12.Adapter, satori.Adapter]
driver = nonebot.get_driver()
for adapter in adapters: