Files
zhenxun_bot-sdgb_api/unlock.py
2025-10-15 19:42:27 +08:00

102 lines
3.5 KiB
Python

bind_lock = asyncio.Lock()
async def _ensure_login(userId: int):
ts = generateTimestamp()
loginResult = apiLogin(ts, userId)
return ts, loginResult
async def handle_unlock(bot: Bot, message: UniMsg, session: EventSession):
user_qq = str(session.id1)
async with bind_lock:
if user_qq not in bind_data:
await MessageUtils.build_message("请先绑定 /bind").send(reply_to=True)
return
userId = bind_data[user_qq]
args = message.extract_plain_text().strip().split()
if len(args) < 3:
await MessageUtils.build_message(
"用法: /unlock <类型> <itemId>\n示例: /unlock 歌 11734\n示例: /unlock 搭档 10"
).send(reply_to=True)
return
itemType = args[1]
itemId = args[2]
ts, loginResult = await _ensure_login(userId)
return_code = loginResult.get("returnCode")
if return_code == 1:
pass
elif return_code == 100:
await MessageUtils.build_message("用户正在上机游玩,请下机后再试,或等待 15 分钟。").send(reply_to=True)
return
elif return_code == 102:
await MessageUtils.build_message("zako~zako~又不获取二维码吗").send(reply_to=True)
return
elif return_code == 103:
await MessageUtils.build_message("登录的账号 UID 无效,请检查账号是否正确。").send(reply_to=True)
return
else:
error_details = loginResult.get("message", str(loginResult))
await MessageUtils.build_message(f"登录失败!这不应该发生,请反馈此问题。\n错误详情:{error_details}").send(reply_to=True)
return
try:
if itemType in ["", "乐曲", "MUSIC"]:
try:
musicId = int(itemId)
except ValueError:
await MessageUtils.build_message("乐曲ID必须是数字").send(reply_to=True)
return
result_str = implUnlockMusic(musicId, userId, ts, loginResult)
else:
if itemType in itemKindzhCNDict:
itemType = itemKindzhCNDict[itemType]
if itemType not in itemKindDict:
await MessageUtils.build_message(
f"未知类型 {itemType},可用类型: {','.join(itemKindzhCNDict.keys())}"
).send(reply_to=True)
return
try:
itemId = int(itemId)
except ValueError:
await MessageUtils.build_message("itemId 必须是数字").send(reply_to=True)
return
result_str = implUnlockSingleItem(
itemId, itemKindDict[itemType], userId, ts, loginResult
)
msg = None
try:
result_json = json.loads(result_str) if isinstance(result_str, str) else result_str
if isinstance(result_json, dict):
if result_json.get("returnCode") == 1:
msg = f"✅ 解锁成功: {itemType} {itemId}"
else:
msg = f"❌ 解锁失败: {result_json.get('message', result_json)}"
except Exception:
pass
if not msg:
msg = f"解锁返回: {result_str}"
await MessageUtils.build_message(msg).send(reply_to=True)
except Exception as e:
await MessageUtils.build_message(f"❌ 解锁异常: {e}").send(reply_to=True)
logger.error(f"解锁异常: {e}")
finally:
try:
apiLogout(ts, userId)
except Exception as e:
logger.warning(f"登出失败: {e}")