Skip to content

Commit 9f882bd

Browse files
committed
🐛修复bug
1 parent 02c66d6 commit 9f882bd

6 files changed

Lines changed: 55 additions & 33 deletions

File tree

.pre-commit-config.yaml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ ci:
55
autoupdate_branch: main
66
autoupdate_schedule: monthly
77
autoupdate_commit_msg: ':arrow_up: auto update by pre-commit hooks'
8-
8+
99
repos:
1010
- repo: https://github.com/astral-sh/ruff-pre-commit
11-
rev: v0.5.6
11+
rev: v0.14.14
1212
hooks:
1313
- id: ruff
14-
args: [--fix, --exit-non-zero-on-fix]
14+
args: [--fix]
15+
- id: ruff-format
1516

16-
# - repo: https://github.com/RobertCraigie/pyright-python
17-
# rev: v1.1.318
18-
# hooks:
19-
# - id: pyright
17+
- repo: https://github.com/pre-commit/pre-commit-hooks
18+
rev: v6.0.0
19+
hooks:
20+
- id: end-of-file-fixer
21+
- id: trailing-whitespace
2022

21-
- repo: https://github.com/psf/black
22-
rev: 24.8.0
23+
- repo: https://github.com/astral-sh/uv-pre-commit
24+
# uv version.
25+
rev: 0.9.28
2326
hooks:
24-
- id: black
25-
stages: [pre-commit]
27+
- id: uv-lock
28+
- id: uv-export

data/L4D2/l4d2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

nonebot_plugin_l4d2_server/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from . import __main__ as __main__ # noqa: E402
2828
from .config import ConfigModel # noqa: E402
2929
from .l4_help import __version__ # noqa: E402
30+
from .utils.database import models # noqa: E402, F401
3031

3132
driver = get_driver()
3233

nonebot_plugin_l4d2_server/l4_image/html_img.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,15 @@ async def server_ip_pic(server_dict: List[OutServer]):
4646
)[:max_number]
4747
logger.debug(sorted_players)
4848

49-
# 时间转换
50-
max_duration_len = max(
51-
[len(str(await convert_duration(i.duration))) for i in sorted_players],
52-
)
49+
# 时间转换 - 添加空列表安全检查
50+
max_duration_len = 1
51+
if sorted_players:
52+
max_duration_len = max(
53+
[
54+
len(str(await convert_duration(i.duration)))
55+
for i in sorted_players
56+
],
57+
)
5358
for player in sorted_players:
5459
chines_dur = await convert_duration(player.duration)
5560
dur = "{:^{}}".format(chines_dur, max_duration_len)
@@ -70,7 +75,6 @@ async def server_ip_pic(server_dict: List[OutServer]):
7075
async def get_server_img(plugins: List[OutServer]) -> Optional[bytes]:
7176
try:
7277
if config.l4_style == "default":
73-
7478
template = env.get_template("normal.html")
7579
else:
7680
template = env.get_template("normal_old.html")

nonebot_plugin_l4d2_server/l4_request/draw_msg.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,32 @@ async def get_much_server(
260260

261261
all_server = await L4API.a2s_info(search_list, is_player=True)
262262

263+
# 确保两个列表长度相同,如果不同则用默认值填充
264+
while len(all_server) < len(server_json):
265+
# 创建默认的无响应服务器信息
266+
import a2s
267+
268+
default_server = a2s.SourceInfo(
269+
protocol=0,
270+
server_name="服务器无响应",
271+
map_name="无",
272+
folder="m",
273+
game="L4D2",
274+
app_id=114514,
275+
steam_id=len(all_server),
276+
player_count=0,
277+
max_players=0,
278+
bot_count=0,
279+
server_type="w",
280+
platform="w",
281+
password_protected=False,
282+
vac_enabled=False,
283+
version="1.0",
284+
edf=0,
285+
ping=0,
286+
)
287+
all_server.append((default_server, []))
288+
263289
for (server, player), srv_json in zip(all_server, server_json):
264290
out_server.append(
265291
OutServer(

nonebot_plugin_l4d2_server/utils/api/request.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import contextlib
3-
import socket
43
from copy import deepcopy
54
from pathlib import Path
65
from typing import Any, Dict, List, Literal, Optional, Tuple, Union, cast
@@ -70,11 +69,7 @@ async def a2s_server_info(
7069
encoding="utf8",
7170
)
7271

73-
except (
74-
asyncio.exceptions.TimeoutError,
75-
ConnectionRefusedError,
76-
socket.gaierror,
77-
):
72+
except Exception:
7873
server = a2s.SourceInfo(
7974
protocol=0,
8075
server_name="服务器无响应",
@@ -124,7 +119,7 @@ async def a2s_info(
124119

125120
try:
126121
results = await asyncio.gather(*tasks)
127-
msg_list = [r for r in results if r is not None]
122+
msg_list = results # 不过滤 None,保持与输入列表相同的长度
128123
except Exception as e:
129124
logger.error(f"获取服务器信息时发生错误: {e}")
130125

@@ -156,11 +151,7 @@ async def process_message(
156151
)
157152
if server is not None:
158153
server.steam_id = index # type: ignore
159-
except (
160-
asyncio.exceptions.TimeoutError,
161-
ConnectionRefusedError,
162-
socket.gaierror,
163-
):
154+
except Exception:
164155
server = a2s.SourceInfo(
165156
protocol=0,
166157
server_name="服务器无响应",
@@ -204,11 +195,7 @@ async def process_message(
204195
)
205196

206197
if is_player:
207-
with contextlib.suppress(
208-
asyncio.exceptions.TimeoutError,
209-
ConnectionRefusedError,
210-
socket.gaierror,
211-
):
198+
with contextlib.suppress(Exception):
212199
play = await a2s.aplayers(ip, timeout=3, encoding="utf8")
213200
else:
214201
play = []

0 commit comments

Comments
 (0)