mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-07 04:40:00 +08:00
refactor: extract is_ban_cached method to BanConsole
This commit is contained in:
@@ -64,6 +64,8 @@ async def _():
|
||||
_client = get_async_client(
|
||||
headers=get_user_agent(),
|
||||
follow_redirects=True,
|
||||
limits=httpx.Limits(max_connections=200, max_keepalive_connections=100),
|
||||
timeout=httpx.Timeout(10),
|
||||
**client_kwargs,
|
||||
)
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
from nonebot.adapters import Bot
|
||||
from nonebot.adapters.onebot.v11 import Message, MessageSegment
|
||||
from nonebot_plugin_alconna import (
|
||||
At,
|
||||
@@ -16,6 +17,7 @@ from nonebot_plugin_alconna import (
|
||||
Video,
|
||||
Voice,
|
||||
)
|
||||
from nonebot_plugin_uninfo import Uninfo
|
||||
from pydantic import BaseModel
|
||||
import ujson as json
|
||||
|
||||
@@ -104,22 +106,32 @@ class MessageUtils:
|
||||
cls,
|
||||
msg_list: MESSAGE_TYPE | list[MESSAGE_TYPE | list[MESSAGE_TYPE]],
|
||||
format_args: dict | None = None,
|
||||
auto_forward_msg: Bot | Uninfo | None = None,
|
||||
) -> UniMessage:
|
||||
"""构造消息
|
||||
|
||||
参数:
|
||||
msg_list: 消息列表
|
||||
format_args: 用于格式化字符串的参数字典.
|
||||
auto_forward_msg: 是否自动转发消息
|
||||
|
||||
返回:
|
||||
UniMessage: 构造完成的消息列表
|
||||
"""
|
||||
from zhenxun.utils.platform import PlatformUtils
|
||||
|
||||
message_list = []
|
||||
if not isinstance(msg_list, list):
|
||||
msg_list = [msg_list]
|
||||
for m in msg_list:
|
||||
_data = m if isinstance(m, list) else [m]
|
||||
message_list += cls.__build_message(_data, format_args)
|
||||
if auto_forward_msg and PlatformUtils.is_forward_merge_supported(
|
||||
auto_forward_msg
|
||||
):
|
||||
message_list = cls.alc_forward_msg(
|
||||
message_list, auto_forward_msg.self_id, auto_forward_msg.self_id
|
||||
)
|
||||
return UniMessage(message_list)
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -18,7 +18,6 @@ from zhenxun.models.friend_user import FriendUser
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.exception import NotFindSuperuser
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
from zhenxun.utils.message import MessageUtils
|
||||
|
||||
driver = nonebot.get_driver()
|
||||
@@ -226,6 +225,8 @@ class PlatformUtils:
|
||||
user_id: 用户id
|
||||
platform: 平台
|
||||
"""
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
|
||||
url = None
|
||||
if platform == "qq":
|
||||
if user_id.isdigit():
|
||||
|
||||
+17
-11
@@ -9,6 +9,7 @@ from types import TracebackType
|
||||
from typing import Any, ClassVar
|
||||
|
||||
import httpx
|
||||
from nonebot_plugin_session import EventSession, Session
|
||||
from nonebot_plugin_uninfo import Uninfo
|
||||
import pypinyin
|
||||
|
||||
@@ -209,7 +210,7 @@ def is_valid_date(date_text: str, separator: str = "-") -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def get_entity_ids(session: Uninfo) -> EntityIDs:
|
||||
def get_entity_ids(session: Uninfo | EventSession) -> EntityIDs:
|
||||
"""获取用户id,群组id,频道id
|
||||
|
||||
参数:
|
||||
@@ -218,16 +219,21 @@ def get_entity_ids(session: Uninfo) -> EntityIDs:
|
||||
返回:
|
||||
EntityIDs: 用户id,群组id,频道id
|
||||
"""
|
||||
user_id = session.user.id
|
||||
group_id = None
|
||||
channel_id = None
|
||||
if session.group:
|
||||
if session.group.parent:
|
||||
group_id = session.group.parent.id
|
||||
channel_id = session.group.id
|
||||
else:
|
||||
group_id = session.group.id
|
||||
return EntityIDs(user_id=user_id, group_id=group_id, channel_id=channel_id)
|
||||
if isinstance(session, Session):
|
||||
user_id = session.id1
|
||||
group_id = session.id2
|
||||
channel_id = session.id3
|
||||
else:
|
||||
user_id = session.user.id
|
||||
group_id = session.group.id if session.group else None
|
||||
channel_id = session.channel.id if session.channel else None
|
||||
if session.group:
|
||||
if session.group.parent:
|
||||
group_id = session.group.parent.id
|
||||
channel_id = session.group.id
|
||||
else:
|
||||
group_id = session.group.id
|
||||
return EntityIDs(user_id=user_id or "", group_id=group_id, channel_id=channel_id)
|
||||
|
||||
|
||||
def is_number(text: str) -> bool:
|
||||
|
||||
@@ -8,9 +8,11 @@ from nonebot.adapters import Bot
|
||||
from nonebot.adapters.onebot.v11 import Bot as v11Bot
|
||||
from nonebot.adapters.onebot.v12 import Bot as v12Bot
|
||||
from nonebot_plugin_session import EventSession
|
||||
from nonebot_plugin_uninfo import Uninfo
|
||||
from ruamel.yaml.comments import CommentedSeq
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.utils import get_entity_ids
|
||||
|
||||
|
||||
class WithdrawManager:
|
||||
@@ -18,7 +20,9 @@ class WithdrawManager:
|
||||
_index = 0
|
||||
|
||||
@classmethod
|
||||
def check(cls, session: EventSession, withdraw_time: tuple[int, int]) -> bool:
|
||||
def check(
|
||||
cls, session: Uninfo | EventSession, withdraw_time: tuple[int, int]
|
||||
) -> bool:
|
||||
"""配置项检查
|
||||
|
||||
参数:
|
||||
@@ -28,12 +32,17 @@ class WithdrawManager:
|
||||
返回:
|
||||
bool: 是否允许撤回
|
||||
"""
|
||||
entity_ids = get_entity_ids(session)
|
||||
if withdraw_time[0] and withdraw_time[0] > 0:
|
||||
if withdraw_time[1] == 2:
|
||||
return True
|
||||
if withdraw_time[1] == 1 and (session.id2 or session.id3):
|
||||
if withdraw_time[1] == 1 and entity_ids.group_id:
|
||||
return True
|
||||
if withdraw_time[1] == 0 and not session.id2 and not session.id3:
|
||||
if (
|
||||
withdraw_time[1] == 0
|
||||
and not entity_ids.group_id
|
||||
and not entity_ids.channel_id
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user