🐛 修复群组申请通知 (#2026)
Some checks are pending
检查bot是否运行正常 / bot check (push) Waiting to run
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Waiting to run
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, python) (push) Waiting to run
Sequential Lint and Type Check / ruff-call (push) Waiting to run
Sequential Lint and Type Check / pyright-call (push) Blocked by required conditions
Release Drafter / Update Release Draft (push) Waiting to run
Force Sync to Aliyun / sync (push) Waiting to run
Update Version / update-version (push) Waiting to run

*  修复一些bug

- 移除不必要的定时器类,简化代码结构
- 优化好友请求处理逻辑,确保在自动同意和手动处理之间的清晰区分
- 更新缓存机制,避免重复处理相同的好友请求
- 新增判断文件是否为二进制文件的功能,提升文件处理的准确性
- 优化缓存字典的过期检查逻辑,提高性能和可读性

*  更新 get_async_client 函数,支持字符串类型的代理参数

- 修改 proxies 参数类型,允许传入字符串形式的代理地址
- 增强代理处理逻辑,将字符串代理转换为字典格式,提升灵活性和可用性
This commit is contained in:
HibiKier 2025-08-19 16:20:52 +08:00 committed by GitHub
parent 6124e217d0
commit f9a38a26b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 83 additions and 92 deletions

View File

@ -1,7 +1,6 @@
import asyncio
from datetime import datetime
import random
import time
from nonebot import on_message, on_request
from nonebot.adapters.onebot.v11 import (
@ -12,7 +11,6 @@ from nonebot.adapters.onebot.v11 import (
from nonebot.adapters.onebot.v11 import Bot as v11Bot
from nonebot.adapters.onebot.v12 import Bot as v12Bot
from nonebot.plugin import PluginMetadata
from nonebot_plugin_apscheduler import scheduler
from nonebot_plugin_session import EventSession
from zhenxun.configs.config import BotConfig, Config
@ -66,19 +64,6 @@ __plugin_meta__ = PluginMetadata(
)
class Timer:
data: dict[str, float] = {} # noqa: RUF012
@classmethod
def check(cls, uid: int | str):
return True if uid not in cls.data else time.time() - cls.data[uid] > 5 * 60
@classmethod
def clear(cls):
now = time.time()
cls.data = {k: v for k, v in cls.data.items() if v - now < 5 * 60}
# TODO: 其他平台请求
friend_req = on_request(priority=5, block=True)
@ -86,68 +71,70 @@ group_req = on_request(priority=5, block=True)
_t = on_message(priority=999, block=False, rule=lambda: False)
cache = CacheRoot.cache_dict(
"REQUEST_CACHE", (base_config.get("TIP_MESSAGE_LIMIT") or 360) * 60, str
)
cache = CacheRoot.cache_dict("REQUEST_CACHE", 60, str)
@friend_req.handle()
async def _(bot: v12Bot | v11Bot, event: FriendRequestEvent, session: EventSession):
if event.user_id and Timer.check(event.user_id):
logger.debug("收录好友请求...", "好友请求", target=event.user_id)
user = await bot.get_stranger_info(user_id=event.user_id)
nickname = user["nickname"]
# sex = user["sex"]
# age = str(user["age"])
comment = event.comment
if base_config.get("AUTO_ADD_FRIEND"):
logger.debug(
"已开启好友请求自动同意,成功通过该请求",
"好友请求",
target=event.user_id,
)
await asyncio.sleep(random.randint(1, 10))
await bot.set_friend_add_request(flag=event.flag, approve=True)
await FriendUser.create(
user_id=str(user["user_id"]), user_name=user["nickname"]
)
else:
# 旧请求全部设置为过期
await FgRequest.filter(
request_type=RequestType.FRIEND,
user_id=str(event.user_id),
handle_type__isnull=True,
).update(handle_type=RequestHandleType.EXPIRE)
f = await FgRequest.create(
request_type=RequestType.FRIEND,
platform=session.platform,
bot_id=bot.self_id,
flag=event.flag,
user_id=event.user_id,
nickname=nickname,
comment=comment,
)
cache_key = str(event.user_id)
if not cache.get(cache_key):
cache.set(cache_key, "1")
results = await PlatformUtils.send_superuser(
bot,
f"*****一份好友申请*****\n"
f"ID: {f.id}\n"
f"昵称:{nickname}({event.user_id})\n"
f"自动同意:{'' if base_config.get('AUTO_ADD_FRIEND') else '×'}\n"
f"日期:{datetime.now().replace(microsecond=0)}\n"
f"备注:{event.comment}",
)
if message_ids := [
str(r[1].msg_ids[0]["message_id"])
for r in results
if r[1] and r[1].msg_ids
]:
f.message_ids = ",".join(message_ids)
await f.save(update_fields=["message_ids"])
logger.debug("收录好友请求...", "好友请求", target=event.user_id)
user = await bot.get_stranger_info(user_id=event.user_id)
nickname = user["nickname"]
# sex = user["sex"]
# age = str(user["age"])
comment = event.comment
if base_config.get("AUTO_ADD_FRIEND"):
logger.debug(
"已开启好友请求自动同意,成功通过该请求",
"好友请求",
target=event.user_id,
)
await asyncio.sleep(random.randint(1, 10))
await bot.set_friend_add_request(flag=event.flag, approve=True)
await FriendUser.create(
user_id=str(user["user_id"]), user_name=user["nickname"]
)
else:
logger.debug("好友请求五分钟内重复, 已忽略", "好友请求", target=event.user_id)
# 旧请求全部设置为过期
await FgRequest.filter(
request_type=RequestType.FRIEND,
user_id=str(event.user_id),
handle_type__isnull=True,
).update(handle_type=RequestHandleType.EXPIRE)
f = await FgRequest.create(
request_type=RequestType.FRIEND,
platform=session.platform,
bot_id=bot.self_id,
flag=event.flag,
user_id=event.user_id,
nickname=nickname,
comment=comment,
)
cache_key = str(event.user_id)
if not cache.get(cache_key):
cache.set(cache_key, "1")
results = await PlatformUtils.send_superuser(
bot,
f"*****一份好友申请*****\n"
f"ID: {f.id}\n"
f"昵称:{nickname}({event.user_id})\n"
f"自动同意:{'' if base_config.get('AUTO_ADD_FRIEND') else '×'}\n"
f"日期:{datetime.now().replace(microsecond=0)}\n"
f"备注:{event.comment}",
)
if message_ids := [
str(r[1].msg_ids[0]["message_id"])
for r in results
if r[1] and r[1].msg_ids
]:
f.message_ids = ",".join(message_ids)
await f.save(update_fields=["message_ids"])
else:
tip_limit = base_config.get("TIP_MESSAGE_LIMIT") or 360
logger.debug(
f"好友请求{tip_limit}分钟内重复, 已忽略",
"好友请求",
target=cache_key,
)
@group_req.handle()
@ -227,7 +214,7 @@ async def _(bot: v12Bot | v11Bot, event: GroupRequestEvent, session: EventSessio
"\n在群组中 群组管理员与群主 允许使用管理员帮助"
"包括ban与功能开关等\n请在群组中发送 '管理员帮助'",
)
elif cache.get(f"{event.group_id}"):
elif not cache.get(f"{event.group_id}"):
cache.set(f"{event.group_id}", "1")
logger.debug(
f"收录 用户[{event.user_id}] 群聊[{event.group_id}] 群聊请求",
@ -284,15 +271,3 @@ async def _(bot: v12Bot | v11Bot, event: GroupRequestEvent, session: EventSessio
"群聊请求",
target=f"{event.user_id}:{event.group_id}",
)
@scheduler.scheduled_job(
"interval",
minutes=5,
)
async def _():
Timer.clear()
async def _():
Timer.clear()

View File

@ -82,12 +82,7 @@ class CacheDict(Generic[T]):
return False
# 检查是否过期
data = self._data[key]
if data.expire_time > 0 and data.expire_time < time.time():
del self._data[key]
return False
return True
return bool(self.expire_time(key))
def get(self, key: str, default: Any = None) -> T | None:
"""获取字典项,如果不存在返回默认值

View File

@ -98,7 +98,7 @@ def get_client() -> AsyncClient:
def get_async_client(
proxies: dict[str, str] | None = None,
proxies: dict[str, str] | str | None = None,
proxy: str | None = None,
verify: bool = False,
**kwargs,
@ -109,6 +109,8 @@ def get_async_client(
"""
transport = kwargs.pop("transport", None) or AsyncHTTPTransport(verify=verify)
if proxies:
if isinstance(proxies, str):
proxies = {"http://": proxies, "https://": proxies}
http_proxy = proxies.get("http://")
https_proxy = proxies.get("https://")
return httpx.AsyncClient(

View File

@ -64,6 +64,25 @@ class ResourceDirManager:
cls.__tree_append(path, deep)
def is_binary_file(file_path: str) -> bool:
"""判断是否为二进制文件"""
binary_extensions = {
".jpg",
".jpeg",
".png",
".gif",
".bmp",
".ico",
".pdf",
".zip",
".rar",
".7z",
".exe",
".dll",
}
return any(file_path.lower().endswith(ext) for ext in binary_extensions)
def cn2py(word: str) -> str:
"""将字符串转化为拼音