Commit 210a3586 authored by ryoii's avatar ryoii

http-api mute

parent 5d7273d2
...@@ -14,6 +14,28 @@ abstract class VerifyDTO : DTO { ...@@ -14,6 +14,28 @@ abstract class VerifyDTO : DTO {
@Serializable @Serializable
data class BindDTO(override val sessionKey: String, val qq: Long) : VerifyDTO() 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 @Serializable
open class StateCode(val code: Int, var msg: String) { open class StateCode(val code: Int, var msg: String) {
...@@ -22,6 +44,7 @@ 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 IllegalSession : StateCode(3, "Session失效或不存在")
object NotVerifySession : StateCode(4, "Session未认证") object NotVerifySession : StateCode(4, "Session未认证")
object NoElement : StateCode(5, "指定对象不存在") object NoElement : StateCode(5, "指定对象不存在")
object PermissionDenied : StateCode(10, "无操作权限")
// KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575 // KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575
@Serializable @Serializable
...@@ -32,9 +55,3 @@ open class StateCode(val code: Int, var msg: String) { ...@@ -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()
...@@ -28,6 +28,7 @@ fun Application.mirai() { ...@@ -28,6 +28,7 @@ fun Application.mirai() {
authModule() authModule()
messageModule() messageModule()
infoModule() infoModule()
groupManageModule()
} }
/** /**
...@@ -111,8 +112,10 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni ...@@ -111,8 +112,10 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
call.respondStateCode(StateCode.NotVerifySession) call.respondStateCode(StateCode.NotVerifySession)
} catch (e: NoSuchBotException) { } catch (e: NoSuchBotException) {
call.respondStateCode(StateCode.NoBot) call.respondStateCode(StateCode.NoBot)
}catch (e: NoSuchElementException) { } catch (e: NoSuchElementException) {
call.respondStateCode(StateCode.NoElement) call.respondStateCode(StateCode.NoElement)
} catch (e: PermissionDeniedException) {
call.respondStateCode(StateCode.PermissionDenied)
} catch (e: IllegalAccessException) { } catch (e: IllegalAccessException) {
call.respondStateCode(StateCode(400, e.message), HttpStatusCode.BadRequest) call.respondStateCode(StateCode(400, e.message), HttpStatusCode.BadRequest)
} }
...@@ -197,6 +200,11 @@ object NotVerifiedSessionException : IllegalAccessException("Session未激活") ...@@ -197,6 +200,11 @@ object NotVerifiedSessionException : IllegalAccessException("Session未激活")
*/ */
object NoSuchBotException: IllegalAccessException("指定Bot不存在") object NoSuchBotException: IllegalAccessException("指定Bot不存在")
/**
* 指定Bot不存在
*/
object PermissionDeniedException: IllegalAccessException("无操作限权")
/** /**
* 错误参数 * 错误参数
*/ */
......
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment