from nonebot import on_request, get_plugin_config, logger from nonebot.adapters.onebot.v11 import FriendRequestEvent, Bot from .config import Config import asyncio import random # 获取插件配置 plugin_config = get_plugin_config(Config) # 注册好友请求事件处理器 friend_request = on_request(priority=5, block=True) @friend_request.handle() async def handle_friend_request(bot: Bot, event: FriendRequestEvent): """处理好友请求,根据配置自动同意并发送欢迎消息""" if not plugin_config.auto_accept_enabled: logger.info(f"好友请求被忽略(功能禁用): user_id={event.user_id} flag={event.flag}") return # 同意好友请求 try: await bot.set_friend_add_request(flag=event.flag, approve=True) except Exception as e: logger.error(f"同意好友请求失败: user_id={event.user_id} flag={event.flag} error={e}") return logger.info(f"已自动同意好友请求: user_id={event.user_id} flag={event.flag}") # 发送欢迎消息(如果配置了) if not plugin_config.auto_reply_message: return await asyncio.sleep(random.uniform( plugin_config.reply_delay_min, plugin_config.reply_delay_max )) try: await bot.send_private_msg( user_id=event.user_id, message=plugin_config.auto_reply_message ) logger.info(f"已发送欢迎消息: user_id={event.user_id}") except Exception as e: logger.error(f"发送欢迎消息失败: user_id={event.user_id} error={e}")