Files
DanDingNoneBot/danding_bot/plugins/welcome_plugin/welcome.py
Mr.Xia c01338f496 refactor(plugins): comprehensive code review - ~35 fixes across 14 plugins
Phase 1 - Plugin code review (14/14 plugins):
- Security: 3x token leak in print→logger.debug, Bearer prefix handling
- Bug: bare except→specific exceptions, HorseState type safety, sync→async
- Critical: response_model undefined, route dead code, sync blocking event loop
- Quality: 11x print()→logger, variable name shadowing, consistent logging

Phase 2 - Deep analysis:
- Fix: payout int truncation→max(1, round(amount*odds))
- Fix: room_store get_lock race condition→dict.setdefault()
- Verify: data_manager f-string SQL is safe (uses ? placeholders)

Infrastructure: review reports generated for all plugins.
2026-05-09 23:22:28 +08:00

63 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from nonebot import on_notice, logger
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):
group_id = event.group_id
if group_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):
"""处理群成员增加事件,发送欢迎消息和帮助菜单"""
# 获取新成员的用户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))