Skip to content

Commit

Permalink
abort if remote onebot version is 12
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Aug 25, 2024
1 parent 481d3c4 commit d59fa60
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
28 changes: 24 additions & 4 deletions onebot/src/main/kotlin/client/core/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ class Bot(
) {
private var name: String = "Onebot"
private var version: String = "Unknown"
private var onebot: Int = 11
val appName: String
get() = name
val appVersion: String
get() = version
val onebotVersion: Int
get() = onebot
val channel: WebSocket
get() = conn
private var idInternal: Long = 0
Expand Down Expand Up @@ -1330,10 +1333,27 @@ class Bot(
@JvmBlockingBridge
suspend fun getVersionInfo(): JsonObject {
val action = ActionPathEnum.GET_VERSION_INFO
return actionHandler.action(this, action).apply {
val data = ignorableObject("data") { JsonObject() }
name = data.ignorable("app_name", "onebot").trim()
version = data.ignorable("app_version", "Unknown").trim()
return actionHandler.action(this, action, showWarning = false).apply {
if (ignorable("status", "failed") != "ok") {
val result = actionHandler.action(this@Bot, ActionPathEnum.GET_VERSION, showWarning = false)
val data = result.ignorableObject("data") { JsonObject() }
val target = data.ignorable("onebot_version", "unknown (12 request)")
if (target != "12") {
throw IllegalStateException("无法获取该 Onebot 实现的版本信息 [$target],确定你连接的 Onebot 实现为 Onebot 11 或 Onebot 12 吗?")
}
name = data.ignorable("impl", "onebot").trim()
version = data.ignorable("version", "Unknown").trim()
onebot = 12
} else {
val data = ignorableObject("data") { JsonObject() }
val target = data.ignorable("protocol_version", "unknown (11 request)")
if (target != "v11") {
throw IllegalStateException("无法获取该 Onebot 实现的版本信息 [$target],确定你连接的 Onebot 实现为 Onebot 11 或 Onebot 12 吗?")
}
name = data.ignorable("app_name", "onebot").trim()
version = data.ignorable("app_version", "Unknown").trim()
onebot = 11
}
}
}

Expand Down
8 changes: 5 additions & 3 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): JsonObject {
suspend fun action(bot: Bot, action: ActionPath, params: JsonObject? = null, showWarning: Boolean = true): JsonObject {
if (!bot.channel.isOpen) {
return JsonObject().apply {
addProperty("status", "failed")
Expand All @@ -65,8 +65,10 @@ class ActionHandler(
return try {
request.send(reqJson)
} catch (e: Exception) {
logger.warn("请求失败: [${action.path}] ${e.message}。如果你认为这是 Overflow 的问题,请带上 logs/onebot 中的日志来反馈。")
logger.trace("Stacktrace: ", e)
if (showWarning) {
logger.warn("请求失败: [${action.path}] ${e.message}。如果你认为这是 Overflow 的问题,请带上 logs/onebot 中的日志来反馈。")
logger.trace("Stacktrace: ", e)
}
if (e is ActionFailedException) e.json
else JsonObject().apply {
addProperty("status", "failed")
Expand Down
4 changes: 4 additions & 0 deletions onebot/src/main/kotlin/sdk/enums/ActionPathEnum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ enum class ActionPathEnum(
* 获取版本信息
*/
GET_VERSION_INFO("get_version_info"),
/**
* 获取版本信息
*/
GET_VERSION("get_version"),
/**
* 获取 Cookie
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
if (printInfo) {
logger.info("协议端版本信息\n${gson.toJson(versionInfo.getAsJsonObject("data"))}")
}
if (botImpl.onebotVersion == 12) {
throw IllegalStateException("Overflow 暂不支持 Onebot 12")
}
val bot = botImpl.wrap(configuration, workingDir)

return bot.also {
Expand Down

0 comments on commit d59fa60

Please sign in to comment.