http-api mute

This commit is contained in:
ryoii 2020-02-05 23:04:23 +08:00
parent 5d7273d2f1
commit 210a3586b7
3 changed files with 70 additions and 7 deletions

View File

@ -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()

View File

@ -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("无操作限权")
/**
* 错误参数
*/

View File

@ -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
}
}
}
}