diff --git a/onebot/src/main/kotlin/client/core/Bot.kt b/onebot/src/main/kotlin/client/core/Bot.kt index d63c7c6..d96771e 100644 --- a/onebot/src/main/kotlin/client/core/Bot.kt +++ b/onebot/src/main/kotlin/client/core/Bot.kt @@ -1015,7 +1015,7 @@ class Bot( */ @JvmBlockingBridge suspend inline fun customRequestData(action: ActionPath, params: JsonObject?): ActionData { - val result = actionHandler.action(this, action, params) + val result = actionHandler.action(this, action, params, ignoreStatus = true) return result.withToken() } /** @@ -1027,7 +1027,7 @@ class Bot( */ @JvmBlockingBridge suspend inline fun customRequestList(action: ActionPath, params: JsonObject?): ActionList { - val result = actionHandler.action(this, action, params) + val result = actionHandler.action(this, action, params, ignoreStatus = true) return result.withToken() } /** @@ -1039,7 +1039,7 @@ class Bot( */ @JvmBlockingBridge suspend fun customRequestRaw(action: ActionPath, params: JsonObject?): ActionRaw { - val result = actionHandler.action(this, action, params) + val result = actionHandler.action(this, action, params, ignoreStatus = true) return result.withClass() } /** @@ -1068,7 +1068,8 @@ class Bot( return actionHandler.action( bot = this, action = action, - params = params?.run { JsonParser.parseString(this).asJsonObject } + params = params?.run { JsonParser.parseString(this).asJsonObject }, + ignoreStatus = true, ) } diff --git a/onebot/src/main/kotlin/client/handler/ActionHandler.kt b/onebot/src/main/kotlin/client/handler/ActionHandler.kt index ffba3d9..b3978f2 100644 --- a/onebot/src/main/kotlin/client/handler/ActionHandler.kt +++ b/onebot/src/main/kotlin/client/handler/ActionHandler.kt @@ -50,7 +50,7 @@ class ActionHandler( * @param params 请求参数 * @return 请求结果 */ - suspend fun action(bot: Bot, action: ActionPath, params: JsonObject? = null, showWarning: Boolean = true): JsonObject { + suspend fun action(bot: Bot, action: ActionPath, params: JsonObject? = null, showWarning: Boolean = true, ignoreStatus: Boolean = false): JsonObject { if (!bot.channel.isOpen) { return JsonObject().apply { addProperty("status", "failed") @@ -63,7 +63,7 @@ class ActionHandler( apiCallbackMap[echo] = request } return try { - request.send(reqJson) + request.send(reqJson, ignoreStatus) } catch (e: Exception) { val message = "请求失败: [${action.path}] ${e.message}。如果你认为这是 Overflow 的问题,请带上 logs/onebot 中的日志来反馈。" if (showWarning) logger.warn(message) else logger.trace(message) diff --git a/onebot/src/main/kotlin/client/util/ActionSendRequest.kt b/onebot/src/main/kotlin/client/util/ActionSendRequest.kt index d6d8d04..70e18d2 100644 --- a/onebot/src/main/kotlin/client/util/ActionSendRequest.kt +++ b/onebot/src/main/kotlin/client/util/ActionSendRequest.kt @@ -2,6 +2,9 @@ package cn.evolvefield.onebot.client.util import cn.evolvefield.onebot.sdk.util.ignorable import cn.evolvefield.onebot.client.core.Bot +import cn.evolvefield.onebot.sdk.util.ignorableArray +import cn.evolvefield.onebot.sdk.util.ignorableObject +import com.google.gson.JsonArray import com.google.gson.JsonObject import kotlinx.coroutines.* import kotlinx.coroutines.sync.Mutex @@ -33,7 +36,7 @@ class ActionSendRequest( * @return Response json data */ @Throws(TimeoutCancellationException::class, ActionFailedException::class) - suspend fun send(req: JsonObject): JsonObject { + suspend fun send(req: JsonObject, ignoreStatus: Boolean = false): JsonObject { val resp = mutex.withLock { kotlin.runCatching { withTimeout(requestTimeout) { @@ -43,27 +46,24 @@ class ActionSendRequest( } }.onFailure { resp.cancel() }.getOrThrow() } - if (resp.get("status").asString == "failed") { - val extra = run { - req["params"]?.asJsonObject?.also { params -> - params["message"]?.asJsonArray?.also { messages -> - val extraFileTypes = messages.filter { - listOf("image", "record", "video").contains(it.asJsonObject?.get("type")?.asString) - && it.asJsonObject?.has("file") == true - }.mapNotNull { - val file = it.asJsonObject!!["file"].asString - if (file.startsWith("base64://")) { - val bytes = Base64.getDecoder().decode(file.replace("base64://", "")) - "msgFileType=${bytes.fileType}" - } else null - } - if (extraFileTypes.isNotEmpty()) { - return@run extraFileTypes.joinToString(", ") - } - } + if (resp.ignorable("status", if (ignoreStatus) "" else "failed") == "failed") { + val extra = runCatching { + val params = req.ignorableObject("params") { JsonObject() } + val messages = params.ignorableArray("message") { JsonArray() } + val extraFileTypes = messages.filter { + listOf("image", "record", "video").contains(it.asJsonObject?.get("type")?.asString) + && it.asJsonObject?.has("file") == true + }.mapNotNull { + val file = it.asJsonObject!!["file"].asString + if (file.startsWith("base64://")) { + val bytes = Base64.getDecoder().decode(file.replace("base64://", "")) + "msgFileType=${bytes.fileType}" + } else null } - "" - } + return@runCatching if (extraFileTypes.isNotEmpty()) { + "; " + extraFileTypes.joinToString(", ") + } else "" + }.getOrElse { "" } throw ActionFailedException( app = "${bot.appName} v${bot.appVersion}", msg = "${resp.ignorable("message", "")}$extra",