64 lines
2.5 KiB
Python
64 lines
2.5 KiB
Python
from nonebot import on_notice, logger
|
||
from nonebot.typing import T_State
|
||
from nonebot.adapters.onebot.v11.event import GroupIncreaseNoticeEvent
|
||
from nonebot.adapters.onebot.v11 import Bot, Message
|
||
from nonebot_plugin_saa import Text, Image, MessageFactory
|
||
import os
|
||
import asyncio
|
||
import random
|
||
|
||
# 定义用于过滤目标群的规则函数
|
||
async def rule_fun(event: GroupIncreaseNoticeEvent):
|
||
id = event.group_id
|
||
if id in [621016172]:
|
||
return True
|
||
return False
|
||
|
||
# 监听群成员增加事件
|
||
group_welcome = on_notice(rule=rule_fun, priority=1, block=True)
|
||
|
||
@group_welcome.handle()
|
||
async def handle_group_increase(bot: Bot, event: GroupIncreaseNoticeEvent, state: T_State):
|
||
"""处理群成员增加事件,发送欢迎消息和帮助菜单"""
|
||
# 获取新成员的用户ID
|
||
user_id = event.get_user_id()
|
||
|
||
# 构建欢迎消息文本
|
||
welcome_messages = [
|
||
f"本群通过祈愿召唤了勇者大人:[CQ:at,qq={user_id}],欢迎加入!",
|
||
f"欢迎 [CQ:at,qq={user_id}] 加入本群!请发送帮助查看更多功能~",
|
||
f"[CQ:at,qq={user_id}] 已成功加入蛋定助手大家庭!请发送帮助查看更多功能,祝您使用愉快~"
|
||
]
|
||
# 随机选择一条欢迎语
|
||
welcome_text = random.choice(welcome_messages)
|
||
|
||
try:
|
||
# 获取帮助菜单图片的绝对路径
|
||
# 这里不需要获取父目录,直接引用danding_help插件的路径
|
||
image_path = os.path.join(os.path.dirname(os.path.dirname(__file__)),
|
||
"danding_help", "img", "帮助菜单.jpg")
|
||
|
||
# 检查文件是否存在
|
||
if not os.path.exists(image_path):
|
||
logger.error(f"帮助菜单图片不存在: {image_path}")
|
||
await group_welcome.finish(Message(welcome_text))
|
||
return
|
||
|
||
# 读取图片
|
||
with open(image_path, "rb") as f:
|
||
image_bytes = f.read()
|
||
|
||
# 添加随机延迟,模拟人工反应
|
||
await asyncio.sleep(random.uniform(2, 3))
|
||
|
||
# 发送欢迎消息和帮助菜单图片
|
||
await MessageFactory([
|
||
Text(welcome_text),
|
||
Image(image_bytes)
|
||
]).send()
|
||
|
||
logger.info(f"已发送欢迎消息给新成员 {user_id} 在群 {event.group_id}")
|
||
except Exception as e:
|
||
logger.error(f"发送欢迎消息失败: {e}")
|
||
# 发生错误时尝试直接发送文本消息
|
||
await group_welcome.finish(Message(welcome_text)) |