From 703f0cc073f4636b9382642f9fa3b87ab04ded3d Mon Sep 17 00:00:00 2001
From: ryoii <ryoii@foxmail.com>
Date: Wed, 26 Feb 2020 15:08:13 +0800
Subject: [PATCH] Http api handle Bot muted exception while sending group
 message, and response BotMuted(code 20)

---
 mirai-api-http/README_CH.md                   | 21 ++++++++++---------
 .../mamoe/mirai/api/http/data/StateCode.kt    |  1 +
 .../mamoe/mirai/api/http/route/BaseRoute.kt   |  8 ++++---
 3 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/mirai-api-http/README_CH.md b/mirai-api-http/README_CH.md
index 05a7cd35e..f47416845 100644
--- a/mirai-api-http/README_CH.md
+++ b/mirai-api-http/README_CH.md
@@ -96,16 +96,17 @@ fun main() {
 }
 ```
 
-| 状态码 | 原因                                |
-| ------ | ----------------------------------- |
-| 0      | 正常                                |
-| 1      | 错误的auth key                      |
-| 2      | 指定的Bot不存在                     |
-| 3      | Session失效或不存在                 |
-| 4      | Session未认证(未激活)               |
-| 5      | 发送消息目标不存在(指定对象不存在)  |
-| 10     | 无操作权限,指Bot没有对应操作的限权 |
-| 400    | 错误的访问,如参数错误等            |
+| 状态码 | 原因                                     |
+| ------ | ---------------------------------------- |
+| 0      | 正常                                     |
+| 1      | 错误的auth key                           |
+| 2      | 指定的Bot不存在(常发生在Session认证时)   |
+| 3      | Session失效或不存在                      |
+| 4      | Session未认证(未激活)                    |
+| 5      | 发送消息目标不存在(指定对象不存在)       |
+| 10     | 无操作权限,指Bot没有对应操作的限权      |
+| 20     | Bot被禁言,指Bot当前无法向指定群发送消息 |
+| 400    | 错误的访问,如参数错误等                 |
 
 
 
diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/StateCode.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/StateCode.kt
index 9777d5144..acf48d220 100644
--- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/StateCode.kt
+++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/StateCode.kt
@@ -11,6 +11,7 @@ open class StateCode(val code: Int, var msg: String) : DTO {
     object NotVerifySession : StateCode(4, "Session未认证")
     object NoElement : StateCode(5, "指定对象不存在")
     object PermissionDenied : StateCode(10, "无操作权限")
+    object BotMuted : StateCode(20, "Bot被禁言")
 
     // KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575
     @Serializable
diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt
index 002bf0716..85513a0c2 100644
--- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt
+++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt
@@ -130,17 +130,19 @@ internal inline fun <reified T : VerifyDTO> Route.miraiVerify(
 internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Unit, ApplicationCall>.() -> Unit) = handle {
     try {
         blk(this)
+    } catch (e: NoSuchBotException) { // Bot不存在
+        call.respondStateCode(StateCode.NoBot)
     } catch (e: IllegalSessionException) { // Session过期
         call.respondStateCode(StateCode.IllegalSession)
     } catch (e: NotVerifiedSessionException) { // Session未认证
         call.respondStateCode(StateCode.NotVerifySession)
-    } catch (e: NoSuchBotException) { // Bot不存在
-        call.respondStateCode(StateCode.NoBot)
     } catch (e: NoSuchElementException) { // 指定对象不存在
         call.respondStateCode(StateCode.NoElement)
     } catch (e: PermissionDeniedException) { // 缺少权限
         call.respondStateCode(StateCode.PermissionDenied)
-    } catch (e: IllegalAccessException) {
+    } catch (e: IllegalStateException) { // Bot被禁言
+        call.respondStateCode(StateCode.BotMuted)
+    } catch (e: IllegalAccessException) { // 错误访问
         call.respondStateCode(StateCode(400, e.message), HttpStatusCode.BadRequest)
     } catch (e: Throwable) {
         e.printStackTrace()