Skip to content

Commit

Permalink
ignore status when request custom action
Browse files Browse the repository at this point in the history
fix #118
  • Loading branch information
MrXiaoM committed Dec 21, 2024
1 parent cb7a024 commit e0158ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
9 changes: 5 additions & 4 deletions onebot/src/main/kotlin/client/core/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ class Bot(
*/
@JvmBlockingBridge
suspend inline fun <reified T : Any> customRequestData(action: ActionPath, params: JsonObject?): ActionData<T> {
val result = actionHandler.action(this, action, params)
val result = actionHandler.action(this, action, params, ignoreStatus = true)
return result.withToken()
}
/**
Expand All @@ -1027,7 +1027,7 @@ class Bot(
*/
@JvmBlockingBridge
suspend inline fun <reified T : Any> customRequestList(action: ActionPath, params: JsonObject?): ActionList<T> {
val result = actionHandler.action(this, action, params)
val result = actionHandler.action(this, action, params, ignoreStatus = true)
return result.withToken()
}
/**
Expand All @@ -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()
}
/**
Expand Down Expand Up @@ -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,
)
}

Expand Down
4 changes: 2 additions & 2 deletions onebot/src/main/kotlin/client/handler/ActionHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
42 changes: 21 additions & 21 deletions onebot/src/main/kotlin/client/util/ActionSendRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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",
Expand Down

0 comments on commit e0158ba

Please sign in to comment.