首次提交
This commit is contained in:
14
danding_bot/plugins/welcome_plugin/__init__.py
Normal file
14
danding_bot/plugins/welcome_plugin/__init__.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from nonebot import get_plugin_config
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from . import welcome
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="welcome_plugin",
|
||||
description="入群欢迎插件",
|
||||
usage="""
|
||||
# 欢迎插件
|
||||
当新用户加入群聊(621016172)时,会自动欢迎并发送帮助菜单
|
||||
|
||||
注意:本插件仅在特定群组中可用
|
||||
""",
|
||||
)
|
||||
64
danding_bot/plugins/welcome_plugin/welcome.py
Normal file
64
danding_bot/plugins/welcome_plugin/welcome.py
Normal file
@@ -0,0 +1,64 @@
|
||||
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))
|
||||
Reference in New Issue
Block a user