diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt index 8890abef1..9fe909dc5 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt @@ -14,6 +14,28 @@ abstract class VerifyDTO : DTO { @Serializable data class BindDTO(override val sessionKey: String, val qq: Long) : VerifyDTO() +@Serializable +data class SendDTO( + override val sessionKey: String, + val target: Long, + val messageChain: MessageChainDTO +) : VerifyDTO() + +typealias GroupTargetDTO = FriendTargetDTO + +@Serializable +data class FriendTargetDTO( + override val sessionKey: String, + val target: Long +) : VerifyDTO() + +@Serializable +data class MuteDTO( + override val sessionKey: String, + val target: Long, + val member: Long = 0, + val time: Int = 0 +) : VerifyDTO() @Serializable open class StateCode(val code: Int, var msg: String) { @@ -22,6 +44,7 @@ open class StateCode(val code: Int, var msg: String) { object IllegalSession : StateCode(3, "Session失效或不存在") object NotVerifySession : StateCode(4, "Session未认证") object NoElement : StateCode(5, "指定对象不存在") + object PermissionDenied : StateCode(10, "无操作权限") // KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575 @Serializable @@ -32,9 +55,3 @@ open class StateCode(val code: Int, var msg: String) { } } -@Serializable -data class SendDTO( - override val sessionKey: String, - val target: Long, - val messageChain: MessageChainDTO -) : VerifyDTO() 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 e74d4f570..fab85148b 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 @@ -28,6 +28,7 @@ fun Application.mirai() { authModule() messageModule() infoModule() + groupManageModule() } /** @@ -111,8 +112,10 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni call.respondStateCode(StateCode.NotVerifySession) } catch (e: NoSuchBotException) { call.respondStateCode(StateCode.NoBot) - }catch (e: NoSuchElementException) { + } catch (e: NoSuchElementException) { call.respondStateCode(StateCode.NoElement) + } catch (e: PermissionDeniedException) { + call.respondStateCode(StateCode.PermissionDenied) } catch (e: IllegalAccessException) { call.respondStateCode(StateCode(400, e.message), HttpStatusCode.BadRequest) } @@ -197,6 +200,11 @@ object NotVerifiedSessionException : IllegalAccessException("Session未激活") */ object NoSuchBotException: IllegalAccessException("指定Bot不存在") +/** + * 指定Bot不存在 + */ +object PermissionDeniedException: IllegalAccessException("无操作限权") + /** * 错误参数 */ diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/GroupManageModule.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/GroupManageModule.kt new file mode 100644 index 000000000..794f02fc3 --- /dev/null +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/GroupManageModule.kt @@ -0,0 +1,38 @@ +package net.mamoe.mirai.api.http.route + +import io.ktor.application.Application +import io.ktor.application.call +import io.ktor.routing.routing +import net.mamoe.mirai.api.http.dto.MuteDTO +import net.mamoe.mirai.api.http.dto.StateCode + + +fun Application.groupManageModule() { + routing { + + miraiVerify<MuteDTO>("/muteAll") { + it.session.bot.getGroup(it.target).muteAll = true + call.respondStateCode(StateCode.Success) + } + + miraiVerify<MuteDTO>("/unmuteAll") { + it.session.bot.getGroup(it.target).muteAll = false + call.respondStateCode(StateCode.Success) + } + + miraiVerify<MuteDTO>("/mute") { + when(it.session.bot.getGroup(it.target).members[it.member].mute(it.time)) { + true -> call.respondStateCode(StateCode.Success) + else -> throw PermissionDeniedException + } + } + + miraiVerify<MuteDTO>("/unmute") { + when(it.session.bot.getGroup(it.target).members[it.member].unmute()) { + true -> call.respondStateCode(StateCode.Success) + else -> throw PermissionDeniedException + } + } + + } +} \ No newline at end of file