保证报错肯定会在终端发出来

This commit is contained in:
shenjack 2024-08-10 11:56:50 +08:00
parent e8c4ed8c60
commit 1a8714488c
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -1,3 +1,4 @@
from __future__ import annotations
import io import io
import time import time
import requests import requests
@ -9,7 +10,7 @@ import urllib.parse
from typing import TYPE_CHECKING, TypeVar, Optional, Tuple, List from typing import TYPE_CHECKING, TypeVar, Optional, Tuple, List
if TYPE_CHECKING: if TYPE_CHECKING:
from ica_typing import IcaNewMessage, IcaClient, ConfigData from ica_typing import IcaNewMessage, IcaClient, ConfigData, TailchatClient
CONFIG_DATA: ConfigData CONFIG_DATA: ConfigData
else: else:
CONFIG_DATA = None # type: ignore CONFIG_DATA = None # type: ignore
@ -60,7 +61,7 @@ def format_hit_count(count: int) -> str:
return count_str return count_str
def wrap_request(url: str, msg: IcaNewMessage, client: IcaClient) -> Optional[dict]: def wrap_request(url: str, msg: IcaNewMessage, client: IcaClient | TailchatClient) -> Optional[dict]:
try: try:
cookie = CONFIG_DATA["cookie"] # type: ignore cookie = CONFIG_DATA["cookie"] # type: ignore
if cookie == "填写你的 cookie" or cookie is None: if cookie == "填写你的 cookie" or cookie is None:
@ -70,22 +71,22 @@ def wrap_request(url: str, msg: IcaNewMessage, client: IcaClient) -> Optional[di
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
warn_msg = f"数据请求失败, 请检查网络\n{traceback.format_exc()}" warn_msg = f"数据请求失败, 请检查网络\n{traceback.format_exc()}"
reply = msg.reply_with(warn_msg) reply = msg.reply_with(warn_msg)
client.send_and_warn(reply) client.send_and_warn(reply) # pyright: ignore reportArgumentType
return None return None
except Exception as _: except Exception as _:
warn_msg = f"数据请求中发生未知错误, 请呼叫 shenjack\n{traceback.format_exc()}" warn_msg = f"数据请求中发生未知错误, 请呼叫 shenjack\n{traceback.format_exc()}"
reply = msg.reply_with(warn_msg) reply = msg.reply_with(warn_msg)
client.send_and_warn(reply) client.send_and_warn(reply) # pyright: ignore reportArgumentType
return None return None
if not response.status_code == 200 or response.reason != "OK": if not response.status_code == 200 or response.reason != "OK":
warn_msg = f"请求失败, 请检查网络\n{response.status_code} {response.reason}" warn_msg = f"请求失败, 请检查网络\n{response.status_code} {response.reason}"
reply = msg.reply_with(warn_msg) reply = msg.reply_with(warn_msg)
client.send_and_warn(reply) client.send_and_warn(reply) # pyright: ignore reportArgumentType
return None return None
return response.json() return response.json()
def bmcl_dashboard(msg: IcaNewMessage, client: IcaClient) -> None: def bmcl_dashboard(msg: IcaNewMessage, client: IcaClient | TailchatClient) -> None:
req_time = time.time() req_time = time.time()
# 记录请求时间 # 记录请求时间
data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/dashboard", msg, client) data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/dashboard", msg, client)
@ -214,7 +215,7 @@ def bmcl_rank_general(msg, client):
client.send_message(reply) client.send_message(reply)
def bmcl_rank(msg: IcaNewMessage, client: IcaClient, name: str) -> None: def bmcl_rank(msg: IcaNewMessage, client: IcaClient | TailchatClient, name: str) -> None:
req_time = time.time() req_time = time.time()
# 记录请求时间 # 记录请求时间
rank_data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/rank", msg, client) rank_data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/rank", msg, client)
@ -323,13 +324,14 @@ def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None:
bangbang_img(msg, client) bangbang_img(msg, client)
except: # noqa except: # noqa
report_msg = f"bmcl插件发生错误,请呼叫shenjack\n{traceback.format_exc()}" report_msg = f"bmcl插件发生错误,请呼叫shenjack\n{traceback.format_exc()}"
client.warn(report_msg)
if len(report_msg) > 200: if len(report_msg) > 200:
report_msg = report_msg[:200] + "..." # 防止消息过长 report_msg = report_msg[:200] + "..." # 防止消息过长
reply = msg.reply_with(report_msg) reply = msg.reply_with(report_msg)
client.send_and_warn(reply) client.send_and_warn(reply)
def on_tailchat_message(msg, client) -> None: def on_tailchat_message(msg, client: TailchatClient) -> None:
if not msg.is_reply: if not msg.is_reply:
if '\n' in msg.content: if '\n' in msg.content:
return return
@ -366,6 +368,7 @@ def on_tailchat_message(msg, client) -> None:
bangbang_img(msg, client) bangbang_img(msg, client)
except: # noqa except: # noqa
report_msg = f"bmcl插件发生错误,请呼叫shenjack\n{traceback.format_exc()}" report_msg = f"bmcl插件发生错误,请呼叫shenjack\n{traceback.format_exc()}"
client.warn(report_msg)
if len(report_msg) > 200: if len(report_msg) > 200:
report_msg = report_msg[:200] + "..." # 防止消息过长 report_msg = report_msg[:200] + "..." # 防止消息过长
reply = msg.reply_with(report_msg) reply = msg.reply_with(report_msg)