📝 将从事件中获取信息的工具函数移动到单独的 utils.event

This commit is contained in:
Expliyh 2024-05-17 15:00:01 +08:00
parent 077658c68d
commit 309397b72c
No known key found for this signature in database
GPG Key ID: F30DE0CFF6157916
12 changed files with 39 additions and 44 deletions

View File

@ -17,7 +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.base.ly_typing import T_Bot, T_MessageEvent
from liteyuki.utils.message.message import MarkdownMessage as md, broadcast_to_superusers from liteyuki.utils.message.message import MarkdownMessage as md, broadcast_to_superusers
from liteyuki.utils.base.reloader import Reloader from liteyuki.utils.base.reloader import Reloader
from liteyuki.utils import satori_utils from liteyuki.utils import event as event_utils, satori_utils
from .api import update_liteyuki from .api import update_liteyuki
require("nonebot_plugin_alconna") require("nonebot_plugin_alconna")
@ -83,7 +83,7 @@ async def _(matcher: Matcher, bot: T_Bot, event: T_MessageEvent):
"reload": True, "reload": True,
"reload_time": time.time(), "reload_time": time.time(),
"reload_bot_id": bot.self_id, "reload_bot_id": bot.self_id,
"reload_session_type": satori_utils.get_message_type(event), "reload_session_type": event_utils.get_message_type(event),
"reload_session_id": (event.group_id if event.message_type == "group" else event.user_id) if not isinstance(event,satori.event.Event) else event.channel.id, "reload_session_id": (event.group_id if event.message_type == "group" else event.user_id) if not isinstance(event,satori.event.Event) else event.channel.id,
"delta_time": 0 "delta_time": 0
} }
@ -118,7 +118,7 @@ async def _(matcher: Matcher, bot: T_Bot, event: T_MessageEvent):
).handle() ).handle()
# Satori OK # Satori OK
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, matcher: Matcher): async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, matcher: Matcher):
ulang = get_user_lang(str(satori_utils.get_user_id(event))) ulang = get_user_lang(str(event_utils.get_user_id(event)))
stored_config: StoredConfig = common_db.where_one(StoredConfig(), default=StoredConfig()) stored_config: StoredConfig = common_db.where_one(StoredConfig(), default=StoredConfig())
if result.subcommands.get("set"): if result.subcommands.get("set"):
key, value = result.subcommands.get("set").args.get("key"), result.subcommands.get("set").args.get("value") key, value = result.subcommands.get("set").args.get("key"), result.subcommands.get("set").args.get("value")
@ -168,7 +168,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, matcher: Matcher
async def _(event: T_MessageEvent, matcher: Matcher): async def _(event: T_MessageEvent, matcher: Matcher):
global markdown_image global markdown_image
# 切换图片模式False以图片形式发送True以markdown形式发送 # 切换图片模式False以图片形式发送True以markdown形式发送
ulang = get_user_lang(str(satori_utils.get_user_id(event))) ulang = get_user_lang(str(event_utils.get_user_id(event)))
stored_config: StoredConfig = common_db.where_one(StoredConfig(), default=StoredConfig()) stored_config: StoredConfig = common_db.where_one(StoredConfig(), default=StoredConfig())
stored_config.config["markdown_image"] = not stored_config.config.get("markdown_image", False) stored_config.config["markdown_image"] = not stored_config.config.get("markdown_image", False)
markdown_image = stored_config.config["markdown_image"] markdown_image = stored_config.config["markdown_image"]

View File

@ -5,7 +5,7 @@ import aiofiles
import nonebot.plugin import nonebot.plugin
from nonebot.adapters import satori from nonebot.adapters import satori
from liteyuki.utils import satori_utils from liteyuki.utils import event as event_utils
from liteyuki.utils.base.data import LiteModel from liteyuki.utils.base.data import LiteModel
from liteyuki.utils.base.data_manager import GlobalPlugin, Group, User, group_db, plugin_db, user_db from liteyuki.utils.base.data_manager import GlobalPlugin, Group, User, group_db, plugin_db, user_db
from liteyuki.utils.base.ly_typing import T_MessageEvent from liteyuki.utils.base.ly_typing import T_MessageEvent
@ -140,12 +140,12 @@ def set_plugin_session_enable(event: T_MessageEvent, plugin_name: str, enable: b
Returns: Returns:
""" """
if satori_utils.get_message_type(event) == "group": if event_utils.get_message_type(event) == "group":
session = group_db.where_one(Group(), "group_id = ?", str(satori_utils.get_group_id(event)), session = group_db.where_one(Group(), "group_id = ?", str(event_utils.get_group_id(event)),
default=Group(group_id=str(satori_utils.get_group_id(event)))) default=Group(group_id=str(event_utils.get_group_id(event))))
else: else:
session = user_db.where_one(User(), "user_id = ?", str(satori_utils.get_user_id(event)), session = user_db.where_one(User(), "user_id = ?", str(event_utils.get_user_id(event)),
default=User(user_id=str(satori_utils.get_user_id(event)))) default=User(user_id=str(event_utils.get_user_id(event))))
default_enable = get_plugin_default_enable(plugin_name) default_enable = get_plugin_default_enable(plugin_name)
if default_enable: if default_enable:
if enable: if enable:
@ -158,12 +158,12 @@ def set_plugin_session_enable(event: T_MessageEvent, plugin_name: str, enable: b
else: else:
session.enabled_plugins.remove(plugin_name) session.enabled_plugins.remove(plugin_name)
if satori_utils.get_message_type(event) == "group": if event_utils.get_message_type(event) == "group":
__group_data[str(satori_utils.get_group_id(event))] = session __group_data[str(event_utils.get_group_id(event))] = session
print(session) print(session)
group_db.save(session) group_db.save(session)
else: else:
__user_data[str(satori_utils.get_user_id(event))] = session __user_data[str(event_utils.get_user_id(event))] = session
user_db.save(session) user_db.save(session)

View File

@ -1,6 +1,5 @@
import nonebot import nonebot
from liteyuki.utils import satori_utils
from nonebot.message import event_preprocessor from nonebot.message import event_preprocessor
# from nonebot_plugin_alconna.typings import Event # from nonebot_plugin_alconna.typings import Event
from liteyuki.utils.base.ly_typing import T_MessageEvent from liteyuki.utils.base.ly_typing import T_MessageEvent

View File

@ -1,9 +1,9 @@
from nonebot import Bot, require from nonebot import Bot, require
from liteyuki.utils.message.npl import convert_duration, convert_time_to_seconds from liteyuki.utils.message.npl import convert_duration, convert_time_to_seconds
from .stat_api import * from .stat_api import *
from ...utils import satori_utils from liteyuki.utils import event as event_utils
from ...utils.base.language import Language from liteyuki.utils.base.language import Language
from ...utils.base.ly_typing import T_MessageEvent from liteyuki.utils.base.ly_typing import T_MessageEvent
require("nonebot_plugin_alconna") require("nonebot_plugin_alconna")
@ -45,7 +45,7 @@ stat_msg = on_alconna(
@stat_msg.assign("message") @stat_msg.assign("message")
async def _(result: Arparma, event: T_MessageEvent, bot: Bot): async def _(result: Arparma, event: T_MessageEvent, bot: Bot):
ulang = Language(satori_utils.get_user_id(event)) ulang = Language(event_utils.get_user_id(event))
try: try:
duration = convert_time_to_seconds(result.other_args.get("duration", "2d")) # 秒数 duration = convert_time_to_seconds(result.other_args.get("duration", "2d")) # 秒数
@ -58,7 +58,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: Bot):
bot_id = result.other_args.get("bot_id") bot_id = result.other_args.get("bot_id")
if group_id in ["current", "c"]: if group_id in ["current", "c"]:
group_id = str(satori_utils.get_group_id(event)) group_id = str(event_utils.get_group_id(event))
if group_id in ["all", "a"]: if group_id in ["all", "a"]:
group_id = "all" group_id = "all"

View File

@ -9,7 +9,7 @@ from liteyuki.utils.base.ly_typing import v11, satori
from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent
from .common import MessageEventModel, msg_db from .common import MessageEventModel, msg_db
from liteyuki.utils import satori_utils from liteyuki.utils import event as event_utils
require("nonebot_plugin_alconna") require("nonebot_plugin_alconna")
@ -64,6 +64,6 @@ async def satori_event_monitor(bot: satori.Bot, event: satori.MessageEvent):
message_id=str(event.message.id), message_id=str(event.message.id),
message=event.message, message=event.message,
message_text=event.message.content, message_text=event.message.content,
message_type=satori_utils.get_message_type(event), message_type=event_utils.get_message_type(event),
) )
msg_db.save(mem) msg_db.save(mem)

View File

@ -4,8 +4,8 @@ from liteyuki.utils.base.resource import get_path
from liteyuki.utils.message.html_tool import template2image from liteyuki.utils.message.html_tool import template2image
from liteyuki.utils.base.language import get_user_lang from liteyuki.utils.base.language import get_user_lang
from .api import * from .api import *
from ...utils import satori_utils from liteyuki.utils import event as event_utils
from ...utils.base.ly_typing import T_Bot, T_MessageEvent from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent
require("nonebot_plugin_alconna") require("nonebot_plugin_alconna")
from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma, UniMessage from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma, UniMessage
@ -28,7 +28,7 @@ status_alc = on_alconna(
@status_alc.handle() @status_alc.handle()
async def _(event: T_MessageEvent, bot: T_Bot): async def _(event: T_MessageEvent, bot: T_Bot):
ulang = get_user_lang(satori_utils.get_user_id(event)) ulang = get_user_lang(event_utils.get_user_id(event))
if ulang.lang_code in status_card_cache: if ulang.lang_code in status_card_cache:
image = status_card_cache[ulang.lang_code] image = status_card_cache[ulang.lang_code]
else: else:

View File

@ -9,7 +9,7 @@ from liteyuki.utils.base.language import Language, change_user_lang, get_all_lan
from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent
from liteyuki.utils.message.message import MarkdownMessage as md from liteyuki.utils.message.message import MarkdownMessage as md
from .const import representative_timezones_list from .const import representative_timezones_list
from ...utils import satori_utils from liteyuki.utils import event as event_utils
require("nonebot_plugin_alconna") require("nonebot_plugin_alconna")
from nonebot_plugin_alconna import Alconna, Args, Arparma, Subcommand, on_alconna from nonebot_plugin_alconna import Alconna, Args, Arparma, Subcommand, on_alconna
@ -42,13 +42,13 @@ class Profile(LiteModel):
@profile_alc.handle() @profile_alc.handle()
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
user: User = user_db.where_one(User(), "user_id = ?", satori_utils.get_user_id(event), user: User = user_db.where_one(User(), "user_id = ?", event_utils.get_user_id(event),
default=User(user_id=str(satori_utils.get_user_id(event)))) default=User(user_id=str(event_utils.get_user_id(event))))
ulang = get_user_lang(str(satori_utils.get_user_id(event))) ulang = get_user_lang(str(event_utils.get_user_id(event)))
if result.subcommands.get("set"): if result.subcommands.get("set"):
if result.subcommands["set"].args.get("value"): if result.subcommands["set"].args.get("value"):
# 对合法性进行校验后设置 # 对合法性进行校验后设置
r = set_profile(result.args["key"], result.args["value"], str(satori_utils.get_user_id(event))) r = set_profile(result.args["key"], result.args["value"], str(event_utils.get_user_id(event)))
if r: if r:
user.profile[result.args["key"]] = result.args["value"] user.profile[result.args["key"]] = result.args["value"]
user_db.save(user) # 数据库保存 user_db.save(user) # 数据库保存

View File

@ -11,7 +11,7 @@ from liteyuki.utils.base.data_manager import User, user_db
from liteyuki.utils.base.language import Language, get_user_lang from liteyuki.utils.base.language import Language, get_user_lang
from liteyuki.utils.base.resource import get_path from liteyuki.utils.base.resource import get_path
from liteyuki.utils.message.html_tool import template2image from liteyuki.utils.message.html_tool import template2image
from ...utils import satori_utils from liteyuki.utils import event as event_utils
require("nonebot_plugin_alconna") require("nonebot_plugin_alconna")
from nonebot_plugin_alconna import on_alconna, Alconna, Args, MultiVar, Arparma from nonebot_plugin_alconna import on_alconna, Alconna, Args, MultiVar, Arparma
@ -43,11 +43,11 @@ async def _(event: T_MessageEvent, matcher: Matcher):
async def get_weather_now_card(matcher: Matcher, event: T_MessageEvent, keyword: list[str], tip: bool = True): async def get_weather_now_card(matcher: Matcher, event: T_MessageEvent, keyword: list[str], tip: bool = True):
ulang = get_user_lang(satori_utils.get_user_id(event)) ulang = get_user_lang(event_utils.get_user_id(event))
qw_lang = get_qw_lang(ulang.lang_code) qw_lang = get_qw_lang(ulang.lang_code)
key = get_config("weather_key") key = get_config("weather_key")
is_dev = get_memory_data("weather.is_dev", True) is_dev = get_memory_data("weather.is_dev", True)
user: User = user_db.where_one(User(), "user_id = ?", satori_utils.get_user_id(event), default=User()) user: User = user_db.where_one(User(), "user_id = ?", event_utils.get_user_id(event), default=User())
# params # params
unit = user.profile.get("unit", "m") unit = user.profile.get("unit", "m")
stored_location = user.profile.get("location", None) stored_location = user.profile.get("location", None)

View File

@ -0,0 +1 @@
from .get_info import *

View File

@ -15,3 +15,10 @@ def get_group_id(event: T_MessageEvent):
return event.guild.id return event.guild.id
else: else:
return event.group_id return event.group_id
def get_message_type(event: T_MessageEvent) -> str:
if isinstance(event, satori.event.Event):
return "private" if event.guild is None else "group"
else:
return event.message_type

View File

@ -1,5 +1,3 @@
from .user_info import user_infos from .user_info import user_infos
from .get_message_type import get_message_type
from .event_tools import *
from .count_friends import count_friends from .count_friends import count_friends
from .count_groups import count_groups from .count_groups import count_groups

View File

@ -1,10 +0,0 @@
from nonebot.adapters import satori
from liteyuki.utils.base.ly_typing import T_MessageEvent
def get_message_type(event: T_MessageEvent) -> str:
if isinstance(event, satori.event.Event):
return "private" if event.guild is None else "group"
else:
return event.message_type