feat(horse_racing): 实现赛马消息更新替换与自动撤回

重构消息发送逻辑,引入消息类型区分和自动撤回机制。赛马进度更新现在会替换前一条更新消息,避免消息刷屏;比赛结果发送前自动撤回最后一条进度更新,提升聊天体验。同时支持配置不同消息类型的自动撤回时间。

- 新增 MessageService.send_with_recall 方法统一处理消息发送和撤回
- 添加 recall_previous_of_type 方法用于撤回特定类型的上一条消息
- 修改 _send_to_scope 函数支持消息类型参数
- 更新测试代码以适配新的消息发送接口
This commit is contained in:
2026-04-07 20:17:00 +08:00
parent 889cfc799b
commit 9895256064
3 changed files with 63 additions and 26 deletions

View File

@@ -260,18 +260,14 @@ async def settle_race(room: Room) -> RaceResult | None:
return result
async def _send_to_scope(bot: Bot, scope: str, message: str):
async def _send_to_scope(bot: Bot, scope: str, message: str, message_type: str = "race_update"):
"""Send message to group or private chat based on scope."""
try:
outbound_message: str | Message = message
if config.RACE_RENDER_AS_IMAGE:
outbound_message = _build_race_image_message(message)
await bot.send_msg(
message_type="group" if scope.startswith("group_") else "private",
group_id=int(scope.split("_", 1)[1]) if scope.startswith("group_") else None,
user_id=int(scope.split("_", 1)[1]) if scope.startswith("test_") else None,
message=outbound_message,
)
await message_service.send_with_recall(bot, scope, message_type, outbound_message)
except Exception:
pass
@@ -281,7 +277,7 @@ async def run_race_with_settlement(bot: Bot, room: Room, scope: str):
room.state = RoomState.RUNNING
horse_list = "\n".join(f" {_format_horse_label(h)}" for h in _get_horses_in_order(room))
await _send_to_scope(bot, scope, f"比赛开始!\n{horse_list}")
await _send_to_scope(bot, scope, f"比赛开始!\n{horse_list}", "race_update")
# Race loop with progress updates
try:
@@ -290,7 +286,7 @@ async def run_race_with_settlement(bot: Bot, room: Room, scope: str):
finished = race_engine.tick(room)
progress = race_engine.format_progress(room)
await _send_to_scope(bot, scope, progress)
await _send_to_scope(bot, scope, progress, "race_update")
if finished:
champion = race_engine.determine_champion(finished)
@@ -327,7 +323,9 @@ async def run_race_with_settlement(bot: Bot, room: Room, scope: str):
if result:
result_lines.extend(await _format_point_change_lines(room, result.point_changes, result.point_change_summaries, name_map))
await _send_to_scope(bot, scope, "\n".join(result_lines))
# Before sending result, we can recall the last update
await message_service.recall_previous_of_type(bot, scope, "race_update")
await _send_to_scope(bot, scope, "\n".join(result_lines), "race_result")
# Cleanup
race_engine.stop_race(scope)