mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-09 09:50:16 +08:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
27b44563ad
@ -3,7 +3,7 @@ kotlin.code.style=official
|
||||
# config
|
||||
mirai_version=0.22.0
|
||||
mirai_japt_version=1.1.0
|
||||
mirai_console_version=0.1.1
|
||||
mirai_console_version=0.2.0
|
||||
kotlin.incremental.multiplatform=true
|
||||
kotlin.parallel.tasks.in.project=true
|
||||
# kotlin
|
||||
|
@ -20,12 +20,14 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import net.mamoe.mirai.api.http.route.mirai
|
||||
import net.mamoe.mirai.utils.DefaultLogger
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import org.slf4j.helpers.NOPLoggerFactory
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
object MiraiHttpAPIServer : CoroutineScope {
|
||||
|
||||
var logger = DefaultLogger("Mirai HTTP API")
|
||||
var logger: MiraiLogger = DefaultLogger("Mirai HTTP API")
|
||||
|
||||
override val coroutineContext: CoroutineContext =
|
||||
CoroutineExceptionHandler { _, throwable -> logger.error(throwable) }
|
||||
|
||||
|
@ -74,17 +74,17 @@ internal object SessionManager {
|
||||
|
||||
|
||||
/**
|
||||
* @author NaturalHG
|
||||
* 这个用于管理不同Client与Mirai HTTP的会话
|
||||
* 管理不同 Client 与 Mirai HTTP 的会话
|
||||
*
|
||||
* [Session]均为内部操作用类
|
||||
* 需使用[SessionManager]
|
||||
* [Session] 均为内部操作用类
|
||||
* @see [SessionManager]
|
||||
* @author NaturalHG
|
||||
*/
|
||||
abstract class Session internal constructor(
|
||||
internal abstract class Session internal constructor(
|
||||
coroutineContext: CoroutineContext,
|
||||
val key: String = generateSessionKey()
|
||||
) : CoroutineScope {
|
||||
val supervisorJob = SupervisorJob(coroutineContext[Job])
|
||||
private val supervisorJob = SupervisorJob(coroutineContext[Job])
|
||||
final override val coroutineContext: CoroutineContext = supervisorJob + coroutineContext
|
||||
|
||||
internal open fun close() {
|
||||
@ -98,13 +98,13 @@ abstract class Session internal constructor(
|
||||
*
|
||||
* TempSession在建立180s内没有转变为[AuthedSession]应被清除
|
||||
*/
|
||||
class TempSession internal constructor(coroutineContext: CoroutineContext) : Session(coroutineContext)
|
||||
internal class TempSession internal constructor(coroutineContext: CoroutineContext) : Session(coroutineContext)
|
||||
|
||||
/**
|
||||
* 任何[TempSession]认证后转化为一个[AuthedSession]
|
||||
* 在这一步[AuthedSession]应该已经有assigned的bot
|
||||
*/
|
||||
class AuthedSession internal constructor(val bot: Bot, originKey: String, coroutineContext: CoroutineContext) : Session(coroutineContext, originKey) {
|
||||
internal class AuthedSession internal constructor(val bot: Bot, originKey: String, coroutineContext: CoroutineContext) : Session(coroutineContext, originKey) {
|
||||
|
||||
companion object {
|
||||
const val CHECK_TIME = 1800L // 1800s aka 30min
|
||||
|
@ -1,3 +1,12 @@
|
||||
/*
|
||||
* Copyright 2020 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.api.http.data
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
@ -1,9 +1,17 @@
|
||||
/*
|
||||
* Copyright 2020 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.api.http.data.common
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.mamoe.mirai.contact.MemberPermission
|
||||
import net.mamoe.mirai.event.events.BotEvent
|
||||
import net.mamoe.mirai.event.events.*
|
||||
import net.mamoe.mirai.message.MessagePacket
|
||||
import net.mamoe.mirai.utils.MiraiExperimentalAPI
|
||||
|
@ -1,3 +1,12 @@
|
||||
/*
|
||||
* Copyright 2020 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.api.http.data.common
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
@ -13,7 +22,7 @@ data class AuthDTO(val authKey: String) : DTO
|
||||
abstract class VerifyDTO : DTO {
|
||||
abstract val sessionKey: String
|
||||
@Transient
|
||||
lateinit var session: AuthedSession // 反序列化验证成功后传入
|
||||
internal lateinit var session: AuthedSession // 反序列化验证成功后传入
|
||||
}
|
||||
|
||||
@Serializable
|
||||
|
@ -1,3 +1,12 @@
|
||||
/*
|
||||
* Copyright 2020 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.api.http.data
|
||||
|
||||
/**
|
@ -37,11 +37,7 @@ import net.mamoe.mirai.api.http.data.common.VerifyDTO
|
||||
import net.mamoe.mirai.api.http.util.jsonParseOrNull
|
||||
import net.mamoe.mirai.api.http.util.toJson
|
||||
import net.mamoe.mirai.contact.PermissionDeniedException
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.helpers.NOPLogger
|
||||
import org.slf4j.helpers.NOPLoggerFactory
|
||||
import org.slf4j.impl.SimpleLogger
|
||||
import org.slf4j.impl.SimpleLoggerFactory
|
||||
|
||||
fun Application.mirai() {
|
||||
install(DefaultHeaders)
|
||||
@ -86,7 +82,7 @@ internal fun Route.miraiGet(
|
||||
val sessionKey = call.parameters["sessionKey"] ?: throw IllegalParamException("参数格式错误")
|
||||
if (!SessionManager.containSession(sessionKey)) throw IllegalSessionException
|
||||
|
||||
when(val session = SessionManager[sessionKey]) {
|
||||
when (val session = SessionManager[sessionKey]) {
|
||||
is TempSession -> throw NotVerifiedSessionException
|
||||
is AuthedSession -> this.body(session)
|
||||
}
|
||||
@ -153,9 +149,11 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
|
||||
/*
|
||||
extend function
|
||||
*/
|
||||
internal suspend inline fun <reified T : StateCode> ApplicationCall.respondStateCode(code: T, status: HttpStatusCode = HttpStatusCode.OK) = respondJson(code.toJson(StateCode.serializer()), status)
|
||||
internal suspend inline fun <reified T : StateCode> ApplicationCall.respondStateCode(code: T, status: HttpStatusCode = HttpStatusCode.OK) =
|
||||
respondJson(code.toJson(StateCode.serializer()), status)
|
||||
|
||||
internal suspend inline fun <reified T : DTO> ApplicationCall.respondDTO(dto: T, status: HttpStatusCode = HttpStatusCode.OK) = respondJson(dto.toJson(), status)
|
||||
internal suspend inline fun <reified T : DTO> ApplicationCall.respondDTO(dto: T, status: HttpStatusCode = HttpStatusCode.OK) =
|
||||
respondJson(dto.toJson(), status)
|
||||
|
||||
internal suspend fun ApplicationCall.respondJson(json: String, status: HttpStatusCode = HttpStatusCode.OK) =
|
||||
respondText(json, defaultTextContentType(ContentType("application", "json")), status)
|
||||
|
File diff suppressed because one or more lines are too long
@ -102,12 +102,6 @@ abstract class Bot : CoroutineScope {
|
||||
*/
|
||||
abstract val qqs: ContactList<QQ>
|
||||
|
||||
/**
|
||||
* 获取一个好友对象. 若没有这个好友, 则会抛出异常 [NoSuchElementException]
|
||||
*/
|
||||
@Deprecated(message = "这个函数有歧义. 它获取的是好友, 却名为 getQQ", replaceWith = ReplaceWith("getFriend(id)"))
|
||||
fun getQQ(id: Long): QQ = getFriend(id)
|
||||
|
||||
/**
|
||||
* 获取一个好友或一个群.
|
||||
* 在一些情况下这可能会造成歧义. 请考虑后使用.
|
||||
|
@ -96,7 +96,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
|
||||
/**
|
||||
* 异步监听这个事件, 并尝试从这个事件中获取一个值.
|
||||
*
|
||||
* 若 [filter] 抛出了一个异常, [Deferred.await] 会抛出这个异常或.
|
||||
* 若 [filter] 抛出的异常将会被传递给 [Deferred.await] 抛出.
|
||||
*
|
||||
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
|
||||
* @param coroutineContext 额外的 [CoroutineContext]
|
||||
|
@ -249,6 +249,36 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
|
||||
operator fun invoke(onEvent: MessageListener<T>): Listener<T> {
|
||||
return content(filter, onEvent)
|
||||
}
|
||||
|
||||
infix fun reply(toReply: String): Listener<T> {
|
||||
return content(filter) { reply(toReply) }
|
||||
}
|
||||
|
||||
infix fun reply(message: Message): Listener<T> {
|
||||
return content(filter) { reply(message) }
|
||||
}
|
||||
|
||||
infix fun reply(replier: (@MessageDsl suspend T.(String) -> Any?)): Listener<T> {
|
||||
return content(filter) {
|
||||
@Suppress("DSL_SCOPE_VIOLATION_WARNING")
|
||||
executeAndReply(replier)
|
||||
}
|
||||
}
|
||||
|
||||
infix fun quoteReply(toReply: String): Listener<T> {
|
||||
return content(filter) { quoteReply(toReply) }
|
||||
}
|
||||
|
||||
infix fun quoteReply(message: Message): Listener<T> {
|
||||
return content(filter) { quoteReply(message) }
|
||||
}
|
||||
|
||||
infix fun quoteReply(replier: (@MessageDsl suspend T.(String) -> Any?)): Listener<T> {
|
||||
return content(filter) {
|
||||
@Suppress("DSL_SCOPE_VIOLATION_WARNING")
|
||||
executeAndQuoteReply(replier)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -698,6 +728,17 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
|
||||
}
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") // false positive
|
||||
internal suspend inline fun T.executeAndQuoteReply(replier: suspend T.(String) -> Any?) {
|
||||
when (val message = replier(this, this.message.toString())) {
|
||||
is Message -> this.quoteReply(message)
|
||||
is Unit -> {
|
||||
|
||||
}
|
||||
else -> this.quoteReply(message.toString())
|
||||
}
|
||||
}
|
||||
/* 易产生迷惑感
|
||||
fun replyCase(equals: String, trim: Boolean = true, replier: MessageReplier<T>) = case(equals, trim) { reply(replier(this)) }
|
||||
fun replyContains(value: String, replier: MessageReplier<T>) = content({ value in it }) { replier(this) }
|
||||
|
Loading…
Reference in New Issue
Block a user