2025-07-24 15:59:28 +08:00
|
|
|
|
import asyncio
|
2025-02-12 23:32:46 +08:00
|
|
|
|
from typing_extensions import Self
|
|
|
|
|
|
|
2024-08-27 23:30:43 +08:00
|
|
|
|
from nonebot.adapters import Bot
|
2024-12-10 19:49:11 +08:00
|
|
|
|
from tortoise import fields
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
2025-06-09 14:39:28 +08:00
|
|
|
|
from zhenxun.configs.config import BotConfig
|
2024-12-10 19:49:11 +08:00
|
|
|
|
from zhenxun.models.group_console import GroupConsole
|
2024-02-04 04:18:54 +08:00
|
|
|
|
from zhenxun.services.db_context import Model
|
2025-07-16 02:51:06 +08:00
|
|
|
|
from zhenxun.services.log import logger
|
2025-06-09 14:39:28 +08:00
|
|
|
|
from zhenxun.utils.common_utils import SqlUtils
|
2024-12-10 19:49:11 +08:00
|
|
|
|
from zhenxun.utils.enum import RequestHandleType, RequestType
|
2024-02-04 04:18:54 +08:00
|
|
|
|
from zhenxun.utils.exception import NotFoundError
|
2025-07-16 02:51:06 +08:00
|
|
|
|
from zhenxun.utils.manager.bot_profile_manager import BotProfileManager
|
|
|
|
|
|
from zhenxun.utils.message import MessageUtils
|
|
|
|
|
|
from zhenxun.utils.platform import PlatformUtils
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FgRequest(Model):
|
|
|
|
|
|
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
|
|
|
|
|
"""自增id"""
|
2024-07-31 04:58:29 +08:00
|
|
|
|
request_type = fields.CharEnumField(
|
|
|
|
|
|
RequestType, default=None, description="请求类型"
|
|
|
|
|
|
)
|
2024-02-04 04:18:54 +08:00
|
|
|
|
"""请求类型"""
|
|
|
|
|
|
platform = fields.CharField(255, description="平台")
|
|
|
|
|
|
"""平台"""
|
|
|
|
|
|
bot_id = fields.CharField(255, description="Bot Id")
|
|
|
|
|
|
"""botId"""
|
|
|
|
|
|
flag = fields.CharField(max_length=255, default="", description="flag")
|
|
|
|
|
|
"""flag"""
|
|
|
|
|
|
user_id = fields.CharField(max_length=255, description="请求用户id")
|
|
|
|
|
|
"""请求用户id"""
|
|
|
|
|
|
group_id = fields.CharField(max_length=255, null=True, description="邀请入群id")
|
|
|
|
|
|
"""邀请入群id"""
|
|
|
|
|
|
nickname = fields.CharField(max_length=255, description="请求人名称")
|
|
|
|
|
|
"""对象名称"""
|
|
|
|
|
|
comment = fields.CharField(max_length=255, null=True, description="验证信息")
|
|
|
|
|
|
"""验证信息"""
|
2024-07-31 04:58:29 +08:00
|
|
|
|
handle_type = fields.CharEnumField(
|
|
|
|
|
|
RequestHandleType, null=True, description="处理类型"
|
|
|
|
|
|
)
|
2024-02-04 04:18:54 +08:00
|
|
|
|
"""处理类型"""
|
2025-06-09 14:39:28 +08:00
|
|
|
|
message_ids = fields.CharField(max_length=255, null=True, description="消息id列表")
|
|
|
|
|
|
"""消息id列表"""
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
2025-01-07 14:36:22 +08:00
|
|
|
|
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
2024-02-04 04:18:54 +08:00
|
|
|
|
table = "fg_request"
|
|
|
|
|
|
table_description = "好友群组请求"
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-02-12 23:32:46 +08:00
|
|
|
|
async def approve(cls, bot: Bot, id: int) -> Self:
|
2024-02-04 04:18:54 +08:00
|
|
|
|
"""同意请求
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
bot: Bot
|
|
|
|
|
|
id: 请求id
|
|
|
|
|
|
|
|
|
|
|
|
异常:
|
|
|
|
|
|
NotFoundError: 未发现请求
|
|
|
|
|
|
"""
|
2025-02-12 23:32:46 +08:00
|
|
|
|
return await cls._handle_request(bot, id, RequestHandleType.APPROVE)
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-02-12 23:32:46 +08:00
|
|
|
|
async def refused(cls, bot: Bot, id: int) -> Self:
|
2024-02-04 04:18:54 +08:00
|
|
|
|
"""拒绝请求
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
bot: Bot
|
|
|
|
|
|
id: 请求id
|
|
|
|
|
|
|
|
|
|
|
|
异常:
|
|
|
|
|
|
NotFoundError: 未发现请求
|
|
|
|
|
|
"""
|
2025-02-12 23:32:46 +08:00
|
|
|
|
return await cls._handle_request(bot, id, RequestHandleType.REFUSED)
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-02-12 23:32:46 +08:00
|
|
|
|
async def ignore(cls, id: int) -> Self:
|
2024-07-31 04:58:29 +08:00
|
|
|
|
"""忽略请求
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
id: 请求id
|
|
|
|
|
|
|
|
|
|
|
|
异常:
|
|
|
|
|
|
NotFoundError: 未发现请求
|
|
|
|
|
|
"""
|
2025-02-12 23:32:46 +08:00
|
|
|
|
return await cls._handle_request(None, id, RequestHandleType.IGNORE)
|
2024-07-31 04:58:29 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def expire(cls, id: int):
|
2024-02-04 04:18:54 +08:00
|
|
|
|
"""忽略请求
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
id: 请求id
|
|
|
|
|
|
|
|
|
|
|
|
异常:
|
|
|
|
|
|
NotFoundError: 未发现请求
|
|
|
|
|
|
"""
|
2024-07-31 04:58:29 +08:00
|
|
|
|
await cls._handle_request(None, id, RequestHandleType.EXPIRE)
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def _handle_request(
|
|
|
|
|
|
cls,
|
2024-07-31 04:58:29 +08:00
|
|
|
|
bot: Bot | None,
|
2024-02-04 04:18:54 +08:00
|
|
|
|
id: int,
|
|
|
|
|
|
handle_type: RequestHandleType,
|
2025-02-12 23:32:46 +08:00
|
|
|
|
) -> Self:
|
2024-02-04 04:18:54 +08:00
|
|
|
|
"""处理请求
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
bot: Bot
|
|
|
|
|
|
id: 请求id
|
|
|
|
|
|
handle_type: 处理类型
|
|
|
|
|
|
|
|
|
|
|
|
异常:
|
|
|
|
|
|
NotFoundError: 未发现请求
|
|
|
|
|
|
"""
|
2024-07-31 04:58:29 +08:00
|
|
|
|
req = await cls.get_or_none(id=id)
|
2024-02-04 04:18:54 +08:00
|
|
|
|
if not req:
|
|
|
|
|
|
raise NotFoundError
|
2024-08-02 20:46:51 +08:00
|
|
|
|
req.handle_type = handle_type
|
2024-02-04 04:18:54 +08:00
|
|
|
|
await req.save(update_fields=["handle_type"])
|
2024-07-31 04:58:29 +08:00
|
|
|
|
if bot and handle_type not in [
|
|
|
|
|
|
RequestHandleType.IGNORE,
|
|
|
|
|
|
RequestHandleType.EXPIRE,
|
|
|
|
|
|
]:
|
|
|
|
|
|
if req.request_type == RequestType.FRIEND:
|
2024-02-04 04:18:54 +08:00
|
|
|
|
await bot.set_friend_add_request(
|
|
|
|
|
|
flag=req.flag, approve=handle_type == RequestHandleType.APPROVE
|
|
|
|
|
|
)
|
2025-07-16 02:51:06 +08:00
|
|
|
|
if BotProfileManager.is_auto_send_profile():
|
|
|
|
|
|
file_path = await BotProfileManager.build_bot_profile_image(
|
|
|
|
|
|
bot.self_id
|
|
|
|
|
|
)
|
|
|
|
|
|
if file_path:
|
2025-07-24 15:59:28 +08:00
|
|
|
|
await asyncio.sleep(2)
|
2025-07-16 02:51:06 +08:00
|
|
|
|
await PlatformUtils.send_message(
|
|
|
|
|
|
bot,
|
|
|
|
|
|
req.user_id,
|
|
|
|
|
|
None,
|
|
|
|
|
|
MessageUtils.build_message(
|
|
|
|
|
|
[
|
|
|
|
|
|
f"你好,我是{BotConfig.self_nickname}, "
|
|
|
|
|
|
"初次见面,希望我们可以好好相处!",
|
|
|
|
|
|
file_path,
|
|
|
|
|
|
]
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
logger.info(
|
|
|
|
|
|
"添加好友自动发送BOT自我介绍图片", session=req.user_id
|
|
|
|
|
|
)
|
2024-02-04 04:18:54 +08:00
|
|
|
|
else:
|
2024-08-27 23:30:43 +08:00
|
|
|
|
await GroupConsole.update_or_create(
|
|
|
|
|
|
group_id=req.group_id, defaults={"group_flag": 1}
|
|
|
|
|
|
)
|
2025-06-09 14:39:28 +08:00
|
|
|
|
if req.flag == "0":
|
|
|
|
|
|
# 用户手动申请入群,创建群认证后提醒用户拉群
|
|
|
|
|
|
await bot.send_private_msg(
|
|
|
|
|
|
user_id=req.user_id,
|
|
|
|
|
|
message=f"已同意你对{BotConfig.self_nickname}的申请群组:"
|
|
|
|
|
|
f"{req.group_id},可以直接手动拉入群组,{BotConfig.self_nickname}会自动同意。",
|
|
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
# 正常同意群组请求
|
|
|
|
|
|
await bot.set_group_add_request(
|
|
|
|
|
|
flag=req.flag,
|
|
|
|
|
|
sub_type="invite",
|
|
|
|
|
|
approve=handle_type == RequestHandleType.APPROVE,
|
|
|
|
|
|
)
|
2025-02-12 23:32:46 +08:00
|
|
|
|
return req
|
2025-06-09 14:39:28 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def _run_script(cls):
|
|
|
|
|
|
return [
|
|
|
|
|
|
SqlUtils.add_column("fg_request", "message_ids", "character varying(255)")
|
|
|
|
|
|
]
|