Skip to content
This repository was archived by the owner on May 23, 2025. It is now read-only.

Commit f753b14

Browse files
committed
fix: 风控保护报错
1 parent 385a23a commit f753b14

File tree

8 files changed

+62
-43
lines changed

8 files changed

+62
-43
lines changed

WechatAPI/Client/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def send_at_message(self, wxid: str, content: str, at: list[str]) -> tuple
3737
if not self.wxid:
3838
raise UserLoggedOut("请先登录")
3939
elif not self.ignore_protect and protector.check(14400):
40-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
40+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
4141

4242
output = ""
4343
for id in at:

WechatAPI/Client/chatroom.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def add_chatroom_member(self, chatroom: str, wxid: str) -> bool:
2121
if not self.wxid:
2222
raise UserLoggedOut("请先登录")
2323
elif not self.ignore_protect and protector.check(14400):
24-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
24+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
2525

2626
async with aiohttp.ClientSession() as session:
2727
json_param = {"Wxid": self.wxid, "Chatroom": chatroom, "InviteWxids": wxid}
@@ -69,7 +69,7 @@ async def get_chatroom_info(self, chatroom: str) -> dict:
6969
if not self.wxid:
7070
raise UserLoggedOut("请先登录")
7171
elif not self.ignore_protect and protector.check(14400):
72-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
72+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
7373

7474
async with aiohttp.ClientSession() as session:
7575
json_param = {"Wxid": self.wxid, "Chatroom": chatroom}
@@ -141,7 +141,7 @@ async def invite_chatroom_member(self, wxid: Union[str, list], chatroom: str) ->
141141
if not self.wxid:
142142
raise UserLoggedOut("请先登录")
143143
elif not self.ignore_protect and protector.check(14400):
144-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
144+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
145145

146146
if isinstance(wxid, list):
147147
wxid = ",".join(wxid)

WechatAPI/Client/friend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def accept_friend(self, scene: int, v1: str, v2: str) -> bool:
3333
if not self.wxid:
3434
raise UserLoggedOut("请先登录")
3535
elif not self.ignore_protect and protector.check(14400):
36-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
36+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
3737

3838
async with aiohttp.ClientSession() as session:
3939
json_param = {"Wxid": self.wxid, "Scene": scene, "V1": v1, "V2": v2}

WechatAPI/Client/message.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ async def revoke_message(self, wxid: str, client_msg_id: int, create_time: int,
8080
if not self.wxid:
8181
raise UserLoggedOut("请先登录")
8282
elif not self.ignore_protect and protector.check(14400):
83-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
83+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
8484

8585
async with aiohttp.ClientSession() as session:
8686
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "ClientMsgId": client_msg_id, "CreateTime": create_time,
@@ -122,7 +122,7 @@ async def _send_text_message(self, wxid: str, content: str, at: list[str] = None
122122
if not self.wxid:
123123
raise UserLoggedOut("请先登录")
124124
elif not self.ignore_protect and protector.check(14400):
125-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
125+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
126126

127127
if at is None:
128128
at = []
@@ -164,7 +164,7 @@ async def _send_image_message(self, wxid: str, image_path: str = "", image_base6
164164
if not self.wxid:
165165
raise UserLoggedOut("请先登录")
166166
elif not self.ignore_protect and protector.check(14400):
167-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
167+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
168168

169169
if bool(image_path) == bool(image_base64):
170170
raise ValueError("Please provide either image_path or image_base64")
@@ -281,7 +281,7 @@ async def _send_voice_message(self, wxid: str, voice_base64: str = "", voice_pat
281281
if not self.wxid:
282282
raise UserLoggedOut("请先登录")
283283
elif not self.ignore_protect and protector.check(14400):
284-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
284+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
285285
elif bool(voice_path) == bool(voice_base64):
286286
raise ValueError("Please provide either voice_path or voice_base64")
287287
elif format not in ["amr", "wav", "mp3"]:
@@ -366,7 +366,7 @@ async def _send_link_message(self, wxid: str, url: str, title: str = "", descrip
366366
if not self.wxid:
367367
raise UserLoggedOut("请先登录")
368368
elif not self.ignore_protect and protector.check(14400):
369-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
369+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
370370

371371
async with aiohttp.ClientSession() as session:
372372
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "Url": url, "Title": title, "Desc": description,
@@ -408,7 +408,7 @@ async def _send_emoji_message(self, wxid: str, md5: str, total_length: int) -> t
408408
if not self.wxid:
409409
raise UserLoggedOut("请先登录")
410410
elif not self.ignore_protect and protector.check(14400):
411-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
411+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
412412

413413
async with aiohttp.ClientSession() as session:
414414
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "Md5": md5, "TotalLen": total_length}
@@ -446,7 +446,7 @@ async def _send_card_message(self, wxid: str, card_wxid: str, card_nickname: str
446446
if not self.wxid:
447447
raise UserLoggedOut("请先登录")
448448
elif not self.ignore_protect and protector.check(14400):
449-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
449+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
450450

451451
async with aiohttp.ClientSession() as session:
452452
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "CardWxid": card_wxid, "CardAlias": card_alias,
@@ -487,7 +487,7 @@ async def _send_app_message(self, wxid: str, xml: str, type: int) -> tuple[int,
487487
if not self.wxid:
488488
raise UserLoggedOut("请先登录")
489489
elif not self.ignore_protect and protector.check(14400):
490-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
490+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
491491

492492
async with aiohttp.ClientSession() as session:
493493
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "Xml": xml, "Type": type}
@@ -523,7 +523,7 @@ async def _send_cdn_file_msg(self, wxid: str, xml: str) -> tuple[int, int, int]:
523523
if not self.wxid:
524524
raise UserLoggedOut("请先登录")
525525
elif not self.ignore_protect and protector.check(14400):
526-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
526+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
527527

528528
async with aiohttp.ClientSession() as session:
529529
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "Content": xml}
@@ -558,7 +558,7 @@ async def _send_cdn_img_msg(self, wxid: str, xml: str) -> tuple[int, int, int]:
558558
if not self.wxid:
559559
raise UserLoggedOut("请先登录")
560560
elif not self.ignore_protect and protector.check(14400):
561-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
561+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
562562

563563
async with aiohttp.ClientSession() as session:
564564
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "Content": xml}
@@ -593,7 +593,7 @@ async def _send_cdn_video_msg(self, wxid: str, xml: str) -> tuple[int, int]:
593593
if not self.wxid:
594594
raise UserLoggedOut("请先登录")
595595
elif not self.ignore_protect and protector.check(14400):
596-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
596+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
597597

598598
async with aiohttp.ClientSession() as session:
599599
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "Content": xml}

WechatAPI/Client/tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ async def set_step(self, count: int) -> bool:
130130
131131
Raises:
132132
UserLoggedOut: 未登录时调用
133-
BanProtection: 登录新设备后4小时内请不要操作以避免风控
133+
BanProtection: 风控保护: 新设备登录后4小时内请挂机
134134
根据error_handler处理错误
135135
"""
136136
if not self.wxid:
137137
raise UserLoggedOut("请先登录")
138138
elif not self.ignore_protect and protector.check(14400):
139-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
139+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
140140

141141
async with aiohttp.ClientSession() as session:
142142
json_param = {"Wxid": self.wxid, "StepCount": count}

WechatAPI/Client/user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ async def get_my_qrcode(self, style: int = 0) -> str:
4646
4747
Raises:
4848
UserLoggedOut: 未登录时调用
49-
BanProtection: 登录新设备后4小时内请不要操作以避免风控
49+
BanProtection: 风控保护: 新设备登录后4小时内请挂机
5050
根据error_handler处理错误
5151
"""
5252
if not self.wxid:
5353
raise UserLoggedOut("请先登录")
5454
elif protector.check(14400) and not self.ignore_protect:
55-
raise BanProtection("登录新设备后4小时内请不要操作以避免风控")
55+
raise BanProtection("风控保护: 新设备登录后4小时内请挂机")
5656

5757
async with aiohttp.ClientSession() as session:
5858
json_param = {"Wxid": self.wxid, "Style": style}

bot_core.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,18 @@
33
import os
44
import time
55
import tomllib
6-
import traceback
76
from pathlib import Path
87

98
from loguru import logger
109

1110
import WechatAPI
1211
from WechatAPI import is_running_in_docker
13-
from WechatAPI.errors import BanProtection
1412
from database.database import BotDatabase
1513
from utils.decorators import scheduler
1614
from utils.plugin_manager import plugin_manager
1715
from utils.xybot import XYBot
1816

1917

20-
async def handle_message(xybot, msg):
21-
"""处理单条消息"""
22-
try:
23-
await xybot.process_message(msg)
24-
except BanProtection:
25-
logger.warning("登录新设备后4小时内请不要操作以避免风控")
26-
except Exception:
27-
logger.error(traceback.format_exc())
28-
29-
3018
async def bot_core():
3119
# 设置工作目录
3220
script_dir = Path(__file__).resolve().parent
@@ -218,9 +206,9 @@ async def bot_core():
218206
await asyncio.sleep(5)
219207
continue
220208

221-
data = data.get("AddMsgs", None)
209+
data = data.get("AddMsgs")
222210
if data:
223211
for message in data:
224-
asyncio.create_task(handle_message(xybot, message))
212+
asyncio.create_task(xybot.process_message(message))
225213
while time.time() - now < 1:
226214
pass

utils/xybot.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from loguru import logger
66

77
from WechatAPI import WechatAPIClient
8+
from WechatAPI.Client.protect import protector
89
from utils.event_manager import EventManager
910

1011

@@ -19,10 +20,13 @@ def __init__(self, bot_client: WechatAPIClient):
1920
with open("main_config.toml", "rb") as f:
2021
main_config = tomllib.load(f)
2122

23+
self.ignore_protection = main_config.get("XYBot", {}).get("ignore-protection", False)
24+
2225
self.ignore_mode = main_config.get("XYBot", {}).get("ignore-mode", "")
2326
self.whitelist = main_config.get("XYBot", {}).get("whitelist", [])
2427
self.blacklist = main_config.get("XYBot", {}).get("blacklist", [])
2528

29+
2630
def update_profile(self, wxid: str, nickname: str, alias: str, phone: str):
2731
"""更新机器人信息"""
2832
self.wxid = wxid
@@ -66,7 +70,10 @@ async def process_message(self, message: Dict[str, Any]):
6670
await self.process_system_message(message)
6771

6872
elif msg_type == 37: # 好友请求
69-
await EventManager.emit("friend_request", self.bot, message)
73+
if self.ignore_protection or not protector.check(14400):
74+
await EventManager.emit("friend_request", self.bot, message)
75+
else:
76+
logger.warning("风控保护: 新设备登录后4小时内请挂机")
7077

7178
elif msg_type == 51:
7279
pass
@@ -119,7 +126,10 @@ async def process_text_message(self, message: Dict[str, Any]):
119126
message["Content"])
120127

121128
if self.ignore_check(message["FromWxid"], message["SenderWxid"]):
122-
await EventManager.emit("at_message", self.bot, message)
129+
if self.ignore_protection or not protector.check(14400):
130+
await EventManager.emit("at_message", self.bot, message)
131+
else:
132+
logger.warning("风控保护: 新设备登录后4小时内请挂机")
123133
return
124134

125135
logger.info("收到文本消息: 消息ID:{} 来自:{} 发送人:{} @:{} 内容:{}",
@@ -130,7 +140,10 @@ async def process_text_message(self, message: Dict[str, Any]):
130140
message["Content"])
131141

132142
if self.ignore_check(message["FromWxid"], message["SenderWxid"]):
133-
await EventManager.emit("text_message", self.bot, message)
143+
if self.ignore_protection or not protector.check(14400):
144+
await EventManager.emit("text_message", self.bot, message)
145+
else:
146+
logger.warning("风控保护: 新设备登录后4小时内请挂机")
134147

135148
async def process_image_message(self, message: Dict[str, Any]):
136149
"""处理图片消息"""
@@ -175,7 +188,10 @@ async def process_image_message(self, message: Dict[str, Any]):
175188
message["Content"] = await self.bot.download_image(aeskey, cdnmidimgurl)
176189

177190
if self.ignore_check(message["FromWxid"], message["SenderWxid"]):
178-
await EventManager.emit("image_message", self.bot, message)
191+
if self.ignore_protection or not protector.check(14400):
192+
await EventManager.emit("image_message", self.bot, message)
193+
else:
194+
logger.warning("风控保护: 新设备登录后4小时内请挂机")
179195

180196
async def process_voice_message(self, message: Dict[str, Any]):
181197
"""处理语音消息"""
@@ -225,7 +241,10 @@ async def process_voice_message(self, message: Dict[str, Any]):
225241
message["Content"] = await self.bot.silk_base64_to_wav_byte(silk_base64)
226242

227243
if self.ignore_check(message["FromWxid"], message["SenderWxid"]):
228-
await EventManager.emit("voice_message", self.bot, message)
244+
if self.ignore_protection or not protector.check(14400):
245+
await EventManager.emit("voice_message", self.bot, message)
246+
else:
247+
logger.warning("风控保护: 新设备登录后4小时内请挂机")
229248

230249
async def process_xml_message(self, message: Dict[str, Any]):
231250
"""处理xml消息"""
@@ -364,7 +383,10 @@ async def process_quote_message(self, message: Dict[str, Any]):
364383
message["Quote"])
365384

366385
if self.ignore_check(message["FromWxid"], message["SenderWxid"]):
367-
await EventManager.emit("quote_message", self.bot, message)
386+
if self.ignore_protection or not protector.check(14400):
387+
await EventManager.emit("quote_message", self.bot, message)
388+
else:
389+
logger.warning("风控保护: 新设备登录后4小时内请挂机")
368390

369391
async def process_video_message(self, message):
370392
# 预处理消息
@@ -394,7 +416,10 @@ async def process_video_message(self, message):
394416
message["Video"] = await self.bot.download_video(message["MsgId"])
395417

396418
if self.ignore_check(message["FromWxid"], message["SenderWxid"]):
397-
await EventManager.emit("video_message", self.bot, message)
419+
if self.ignore_protection or not protector.check(14400):
420+
await EventManager.emit("video_message", self.bot, message)
421+
else:
422+
logger.warning("风控保护: 新设备登录后4小时内请挂机")
398423

399424
async def process_file_message(self, message: Dict[str, Any]):
400425
"""处理文件消息"""
@@ -419,7 +444,10 @@ async def process_file_message(self, message: Dict[str, Any]):
419444
message["File"] = await self.bot.download_attach(attach_id)
420445

421446
if self.ignore_check(message["FromWxid"], message["SenderWxid"]):
422-
await EventManager.emit("file_message", self.bot, message)
447+
if self.ignore_protection or not protector.check(14400):
448+
await EventManager.emit("file_message", self.bot, message)
449+
else:
450+
logger.warning("风控保护: 新设备登录后4小时内请挂机")
423451

424452
async def process_system_message(self, message: Dict[str, Any]):
425453
"""处理系统消息"""
@@ -480,7 +508,10 @@ async def process_pat_message(self, message: Dict[str, Any]):
480508
message["PatSuffix"])
481509

482510
if self.ignore_check(message["FromWxid"], message["SenderWxid"]):
483-
await EventManager.emit("pat_message", self.bot, message)
511+
if self.ignore_protection or not protector.check(14400):
512+
await EventManager.emit("pat_message", self.bot, message)
513+
else:
514+
logger.warning("风控保护: 新设备登录后4小时内请挂机")
484515

485516
def ignore_check(self, FromWxid: str, SenderWxid: str):
486517
if self.ignore_mode == "Whitelist":

0 commit comments

Comments
 (0)