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.
This commit is contained in:
@@ -1,48 +1,46 @@
|
||||
from nonebot import on_request, get_plugin_config, logger
|
||||
from nonebot.adapters.onebot.v11 import FriendRequestEvent, Bot
|
||||
from nonebot.typing import T_State
|
||||
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, state: T_State):
|
||||
"""处理好友请求,根据配置自动同意并发送欢迎消息"""
|
||||
|
||||
# 检查是否启用自动同意
|
||||
if not plugin_config.auto_accept_enabled:
|
||||
logger.info(f"收到来自 {event.user_id} 的好友请求,但自动同意功能已禁用")
|
||||
return
|
||||
|
||||
try:
|
||||
# 获取请求的标识信息
|
||||
flag = event.flag
|
||||
|
||||
# 调用OneBot接口处理好友请求(设置为同意)
|
||||
await bot.set_friend_add_request(flag=flag, approve=True)
|
||||
|
||||
logger.info(f"已自动同意来自 {event.user_id} 的好友请求")
|
||||
|
||||
# 如果配置了自动回复消息,则发送欢迎消息
|
||||
if plugin_config.auto_reply_message:
|
||||
# 添加随机延迟,模拟真人回复
|
||||
await asyncio.sleep(random.uniform(2, 5))
|
||||
|
||||
try:
|
||||
# 发送欢迎消息
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id,
|
||||
message=plugin_config.auto_reply_message
|
||||
)
|
||||
logger.info(f"已向新好友 {event.user_id} 发送欢迎消息")
|
||||
except Exception as e:
|
||||
logger.error(f"向新好友 {event.user_id} 发送欢迎消息失败: {e}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"处理好友请求失败: {e}")
|
||||
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}")
|
||||
@@ -1,9 +1,13 @@
|
||||
from pydantic import BaseModel, validator
|
||||
from typing import Optional
|
||||
|
||||
class Config(BaseModel):
|
||||
# 是否启用自动同意好友请求
|
||||
auto_accept_enabled: bool = True
|
||||
|
||||
# 自动回复的消息,如果为空则不发送
|
||||
auto_reply_message: Optional[str] = ""
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
class Config(BaseModel):
|
||||
# 是否启用自动同意好友请求
|
||||
auto_accept_enabled: bool = True
|
||||
|
||||
# 自动回复的消息,None表示不发送
|
||||
auto_reply_message: Optional[str] = None
|
||||
|
||||
# 欢迎消息发送前的随机延迟范围(秒)
|
||||
reply_delay_min: float = 2.0
|
||||
reply_delay_max: float = 5.0
|
||||
Reference in New Issue
Block a user