fix: 检测存活支持分组节点,分组节点不被自动清理误删

This commit is contained in:
mango
2026-02-25 17:24:11 +08:00
parent 321bb434a5
commit daf2e1e776

27
bot.py
View File

@@ -175,13 +175,27 @@ async def cb_menu(update: Update, context: ContextTypes.DEFAULT_TYPE):
elif action == 'menu_check':
msg = await send_del(q.message, '🔍 检测中...', delay=120)
results = []
# 检测默认分组
if data['subs']:
results.append('📦 *默认分组*')
for s in data['subs']:
ok = await check_node(s['link'], s['type'])
s['alive'] = ok
results.append(f"{'🟢' if ok else '🔴'} [{s['type']}] {s['name']}")
# 检测所有订阅分组
for sg in data.get('sub_groups', []):
if not sg.get('nodes'): continue
results.append(f"\n📁 *{sg['name']}*")
for s in sg['nodes']:
ok = await check_node(s['link'], s['type'])
s['alive'] = ok
results.append(f"{'🟢' if ok else '🔴'} [{s['type']}] {s['name']}")
save_data(data)
try: await msg.delete()
except: pass
if not results:
await send_del(q.message, '📭 暂无订阅')
else:
await send_del(q.message, '📊 *检测结果*\n\n' + '\n'.join(results), parse_mode='Markdown')
elif action == 'menu_pick_multi':
@@ -763,11 +777,12 @@ async def cb_subgroups(update: Update, context: ContextTypes.DEFAULT_TYPE):
except: pass
async def auto_cleanup(bot_token):
"""Every 6h: check nodes, auto-delete dead ones, notify group"""
"""Every 6h: check nodes, auto-delete dead ones from default subs only, check group nodes but don't delete"""
import httpx
await asyncio.sleep(60)
while True:
data = load_data()
# 默认分组:检测并删除不可用
if data['subs']:
dead = []
for s in list(data['subs']):
@@ -786,6 +801,16 @@ async def auto_cleanup(bot_token):
json={'chat_id': gid, 'text': msg})
except: pass
log.info(f'Auto cleanup: removed {len(dead)} dead nodes')
# 订阅分组:只更新存活状态,不删除
changed = False
for sg in data.get('sub_groups', []):
for s in sg.get('nodes', []):
ok = await check_node(s['link'], s['type'])
if s.get('alive', True) != ok:
s['alive'] = ok
changed = True
if changed:
save_data(data)
await asyncio.sleep(21600)
def run_cleanup():