mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-16 01:30:03 +08:00
@@ -2,10 +2,9 @@ from datetime import datetime
|
||||
import os
|
||||
from pathlib import Path
|
||||
import random
|
||||
import re
|
||||
|
||||
from nonebot.adapters import Bot
|
||||
from nonebot_plugin_alconna import At
|
||||
from nonebot_plugin_alconna import At, UniMessage
|
||||
from nonebot_plugin_uninfo import Uninfo
|
||||
import ujson as json
|
||||
|
||||
@@ -28,7 +27,7 @@ base_config = Config.get("invite_manager")
|
||||
|
||||
limit_cd = base_config.get("welcome_msg_cd")
|
||||
|
||||
WELCOME_PATH = DATA_PATH / "welcome_message" / "qq"
|
||||
WELCOME_PATH = DATA_PATH / "welcome_message"
|
||||
|
||||
DEFAULT_IMAGE_PATH = IMAGE_PATH / "qxz"
|
||||
|
||||
@@ -153,44 +152,68 @@ class GroupManager:
|
||||
await cls.__refresh_level(bot, group_id)
|
||||
|
||||
@classmethod
|
||||
def __build_welcome_message(cls, user_id: str, path: Path) -> list[At | Path | str]:
|
||||
"""构造群欢迎消息
|
||||
def get_path(cls, session: Uninfo) -> Path | None:
|
||||
"""根据Session获取存储路径
|
||||
|
||||
参数:
|
||||
user_id: 用户id
|
||||
path: 群欢迎消息存储路径
|
||||
session: Uninfo:
|
||||
|
||||
返回:
|
||||
list[At | Path | str]: 消息列表
|
||||
Path: 存储路径
|
||||
"""
|
||||
file = path / "text.json"
|
||||
data = json.load(file.open(encoding="utf-8"))
|
||||
message = data["message"]
|
||||
msg_split = re.split(r"\[image:\d+\]", message)
|
||||
msg_list = []
|
||||
if data["at"]:
|
||||
msg_list.append(At(flag="user", target=user_id))
|
||||
for i, text in enumerate(msg_split):
|
||||
msg_list.append(text)
|
||||
img_file = path / f"{i}.png"
|
||||
if img_file.exists():
|
||||
msg_list.append(img_file)
|
||||
return msg_list
|
||||
if not session.group:
|
||||
return None
|
||||
platform = PlatformUtils.get_platform(session)
|
||||
path = WELCOME_PATH / f"{platform}" / f"{session.group.id}"
|
||||
if session.group.parent:
|
||||
path = (
|
||||
WELCOME_PATH
|
||||
/ f"{platform}"
|
||||
/ f"{session.group.parent.id}"
|
||||
/ f"{session.group.id}"
|
||||
)
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
return path
|
||||
|
||||
@classmethod
|
||||
async def __send_welcome_message(cls, user_id: str, group_id: str):
|
||||
def __get_welcome_data(cls, session: Uninfo) -> dict | None:
|
||||
"""获取存储数据
|
||||
|
||||
参数:
|
||||
session: Uninfo
|
||||
|
||||
返回:
|
||||
dict | None: 欢迎消息数据
|
||||
"""
|
||||
if not session.group:
|
||||
return None
|
||||
path = cls.get_path(session)
|
||||
if not path:
|
||||
return None
|
||||
file = path / "text.json"
|
||||
if not file.exists():
|
||||
return None
|
||||
with file.open(encoding="utf8") as f:
|
||||
return json.load(f)
|
||||
|
||||
@classmethod
|
||||
async def __send_welcome_message(cls, session: Uninfo, user_id: str):
|
||||
"""发送群欢迎消息
|
||||
|
||||
参数:
|
||||
user_id: 用户id
|
||||
group_id: 群组id
|
||||
"""
|
||||
cls._flmt.start_cd(group_id)
|
||||
path = WELCOME_PATH / f"{group_id}"
|
||||
file = path / "text.json"
|
||||
if file.exists():
|
||||
msg_list = cls.__build_welcome_message(user_id, path)
|
||||
logger.info("发送群欢迎消息...", "入群检测", group_id=group_id)
|
||||
if not session.group:
|
||||
return
|
||||
cls._flmt.start_cd(session.group.id)
|
||||
if json_data := cls.__get_welcome_data(session):
|
||||
key = random.choice([k for k in json_data.keys() if json_data[k]["status"]])
|
||||
welcome_data = json_data[key]
|
||||
msg_list = UniMessage().load(welcome_data["message"])
|
||||
if welcome_data["at"]:
|
||||
msg_list.insert(0, At("user", user_id))
|
||||
logger.info("发送群欢迎消息...", "入群检测", session=session)
|
||||
if msg_list:
|
||||
await MessageUtils.build_message(msg_list).send() # type: ignore
|
||||
else:
|
||||
@@ -199,7 +222,7 @@ class GroupManager:
|
||||
)
|
||||
await MessageUtils.build_message(
|
||||
[
|
||||
"新人快跑啊!!本群现状↓(快使用自定义!)",
|
||||
"新人快跑啊!!本群现状↓(快使用自定义群欢迎消息!)",
|
||||
image,
|
||||
]
|
||||
).send()
|
||||
@@ -224,7 +247,7 @@ class GroupManager:
|
||||
if not await CommonUtils.task_is_block(
|
||||
session, "group_welcome"
|
||||
) and cls._flmt.check(group_id):
|
||||
await cls.__send_welcome_message(user_id, group_id)
|
||||
await cls.__send_welcome_message(session, user_id)
|
||||
|
||||
@classmethod
|
||||
async def kick_bot(cls, bot: Bot, group_id: str, operator_id: str):
|
||||
|
||||
Reference in New Issue
Block a user