refactor: extract admin check helper & harden room_store JSON parsing

- Extract duplicated admin/owner check from race.py into shared._is_admin_or_owner()
- Add try/except around JSON.loads in room_store.load_rooms for corrupted data resilience
- Use .get() for safer dict access in room deserialization
This commit is contained in:
2026-05-09 23:37:55 +08:00
parent 14397ab645
commit fd2fd90f05
3 changed files with 32 additions and 30 deletions

View File

@@ -7,6 +7,7 @@ from .shared import (
_send_to_scope, _build_race_image_message,
_get_user_name, _get_horses_in_order, _format_horse_label,
run_race_with_settlement, points_service,
_is_admin_or_owner,
)
from ..models import RoomState, HorseState
@@ -64,17 +65,7 @@ async def handle_start(bot: Bot, event: Event):
return
is_participant = user_id in [h.owner_id for h in room.horses.values()]
is_admin = False
if isinstance(event, GroupMessageEvent):
try:
member_info = await bot.get_group_member_info(
group_id=event.group_id,
user_id=int(user_id)
)
role = member_info.get("role", "")
is_admin = role in ("admin", "owner")
except Exception:
pass
is_admin = await _is_admin_or_owner(bot, event)
if not is_participant and not is_admin:
await start_cmd.finish("只有参赛者或群管理员可以开赛")
@@ -114,17 +105,7 @@ async def handle_cancel_race(bot: Bot, event: Event):
return
is_participant = user_id in [h.owner_id for h in room.horses.values()]
is_admin = False
if isinstance(event, GroupMessageEvent):
try:
member_info = await bot.get_group_member_info(
group_id=event.group_id,
user_id=int(user_id)
)
role = member_info.get("role", "")
is_admin = role in ("admin", "owner")
except Exception:
pass
is_admin = await _is_admin_or_owner(bot, event)
if not is_participant and not is_admin:
await cancel_race_cmd.finish("只有参赛者或群管理员可以取消比赛")