尝试修复挑战系统中的用户名错误
This commit is contained in:
@@ -4,9 +4,11 @@ from __future__ import annotations
|
||||
|
||||
from typing import Optional, Sequence
|
||||
|
||||
from PWF.Convention.Runtime.Architecture import Architecture
|
||||
from PWF.Convention.Runtime.GlobalConfig import ConsoleFrontColor, ProjectConfig
|
||||
|
||||
from Plugins.WPSAPI import GuideEntry
|
||||
from Plugins.WPSConfigSystem import WPSConfigAPI
|
||||
from .combat_plugin_base import WPSCombatBase
|
||||
|
||||
|
||||
@@ -23,7 +25,7 @@ class WPSCombatBattle(WPSCombatBase):
|
||||
return (
|
||||
GuideEntry(
|
||||
title="挑战",
|
||||
identifier="挑战 <目标用户ID>",
|
||||
identifier="挑战 <目标用户>",
|
||||
description="向指定玩家发起 PVP 挑战。",
|
||||
metadata={"别名": "challenge"},
|
||||
icon="⚔️",
|
||||
@@ -103,7 +105,7 @@ class WPSCombatBattle(WPSCombatBase):
|
||||
处理PVP命令
|
||||
|
||||
命令格式:
|
||||
- 挑战 <目标用户ID>
|
||||
- 挑战 <目标用户>
|
||||
- 接受挑战 <挑战ID>
|
||||
- 拒绝挑战 <挑战ID>
|
||||
- 战斗 <战斗ID> <技能名>
|
||||
@@ -147,30 +149,28 @@ class WPSCombatBattle(WPSCombatBase):
|
||||
"""处理挑战命令"""
|
||||
if not args:
|
||||
return await self.send_markdown_message(
|
||||
"❌ 请指定目标用户ID\n用法:`挑战 <目标用户ID>`",
|
||||
"❌ 请指定目标用户\n用法:`挑战 <目标用户名|用户ID>`",
|
||||
chat_id,
|
||||
user_id
|
||||
)
|
||||
|
||||
try:
|
||||
target_id = int(args[0])
|
||||
except ValueError:
|
||||
target_identifier = args[0]
|
||||
target_id, error_message = self._resolve_target_user(target_identifier)
|
||||
if error_message:
|
||||
return await self.send_markdown_message(error_message, chat_id, user_id)
|
||||
if target_id is None:
|
||||
return await self.send_markdown_message(
|
||||
"❌ 用户ID格式错误",
|
||||
"❌ 未找到目标用户,请确认用户名或ID",
|
||||
chat_id,
|
||||
user_id
|
||||
)
|
||||
|
||||
if target_id == user_id:
|
||||
return await self.send_markdown_message(
|
||||
"❌ 不能挑战自己",
|
||||
chat_id,
|
||||
user_id
|
||||
)
|
||||
|
||||
service = self.service()
|
||||
success, msg, challenge_id = service.create_pvp_challenge(user_id, target_id)
|
||||
|
||||
return await self.send_markdown_message(msg, chat_id, user_id)
|
||||
|
||||
async def _handle_accept(
|
||||
@@ -291,7 +291,7 @@ class WPSCombatBattle(WPSCombatBase):
|
||||
"""帮助信息"""
|
||||
return """# ⚔️ PVP对战系统
|
||||
**命令格式:**
|
||||
- `挑战 <目标用户ID>`:发起PVP挑战
|
||||
- `挑战 <目标用户名|用户ID>`:发起PVP挑战
|
||||
- `接受挑战 <挑战ID>`:接受挑战
|
||||
- `拒绝挑战 <挑战ID>`:拒绝挑战
|
||||
- `战斗 <战斗ID> <技能名>`:执行战斗动作
|
||||
@@ -305,11 +305,38 @@ class WPSCombatBattle(WPSCombatBase):
|
||||
- 可随时投降
|
||||
|
||||
**示例:**
|
||||
- `挑战 玩家A`:向用户名为玩家A的用户发起挑战
|
||||
- `挑战 12345`:向用户12345发起挑战
|
||||
- `接受挑战 1`:接受挑战ID为1的挑战
|
||||
- `战斗 1 攻击`:在战斗1中使用"攻击"技能
|
||||
- `投降 1`:在战斗1中投降
|
||||
"""
|
||||
|
||||
def _resolve_target_user(self, identifier: str) -> tuple[Optional[int], Optional[str]]:
|
||||
text = identifier.strip()
|
||||
if not text:
|
||||
return None, "❌ 请指定目标用户"
|
||||
try:
|
||||
config_api: WPSConfigAPI = Architecture.Get(WPSConfigAPI)
|
||||
except Exception as exc:
|
||||
logger.Log(
|
||||
"Error",
|
||||
f"{ConsoleFrontColor.RED}获取 WPSConfigAPI 实例失败: {exc}{ConsoleFrontColor.RESET}",
|
||||
)
|
||||
return None, "❌ 系统繁忙,请稍后重试"
|
||||
try:
|
||||
resolved_id = config_api.find_user_id_by_username(text)
|
||||
if resolved_id is not None:
|
||||
return resolved_id, None
|
||||
except Exception as exc:
|
||||
logger.Log(
|
||||
"Error",
|
||||
f"{ConsoleFrontColor.RED}解析用户名 {text} 失败: {exc}{ConsoleFrontColor.RESET}",
|
||||
)
|
||||
return None, "❌ 系统繁忙,请稍后重试"
|
||||
if text.isdigit():
|
||||
return int(text), None
|
||||
return None, None
|
||||
|
||||
|
||||
__all__ = ["WPSCombatBattle"]
|
||||
|
||||
Reference in New Issue
Block a user