Add async shortcuts for nextMessage

This commit is contained in:
Him188 2020-03-19 10:25:57 +08:00
parent 79bb9e7dc9
commit ecda34deee

View File

@ -11,6 +11,8 @@
package net.mamoe.mirai.message
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.io.ByteReadChannel
import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.Contact
@ -254,6 +256,20 @@ suspend inline fun <reified P : MessagePacket<*, *>> P.nextMessage(
}.message
}
/**
* @see nextMessage
*/
inline fun <reified P : MessagePacket<*, *>> P.nextMessageAsync(
timeoutMillis: Long = -1,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Deferred<MessageChain> {
return this.bot.async(coroutineContext) {
subscribingGet<P, P>(timeoutMillis) {
takeIf { this.isContextIdenticalWith(this@nextMessageAsync) }
}.message
}
}
/**
* 挂起当前协程, 等待下一条 [MessagePacket.sender] [MessagePacket.subject] [this] 相同的 [MessagePacket]
*
@ -272,6 +288,20 @@ suspend inline fun <reified P : MessagePacket<*, *>> P.nextMessageOrNull(
}?.message
}
/**
* @see nextMessageOrNull
*/
inline fun <reified P : MessagePacket<*, *>> P.nextMessageOrNullAsync(
timeoutMillis: Long = -1,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Deferred<MessageChain?> {
return this.bot.async(coroutineContext) {
subscribingGetOrNull<P, P>(timeoutMillis) {
takeIf { this.isContextIdenticalWith(this@nextMessageOrNullAsync) }
}?.message
}
}
/**
* 挂起当前协程, 等待下一条 [MessagePacket.sender] [MessagePacket.subject] [this] 相同的 [MessagePacket]
*
@ -289,6 +319,18 @@ suspend inline fun <reified M : Message> MessagePacket<*, *>.nextMessageContaini
}.message.first()
}
inline fun <reified M : Message> MessagePacket<*, *>.nextMessageContainingAsync(
timeoutMillis: Long = -1,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Deferred<M> {
return this.bot.async(coroutineContext) {
@Suppress("RemoveExplicitTypeArguments")
subscribingGet<MessagePacket<*, *>, MessagePacket<*, *>>(timeoutMillis) {
takeIf { this.isContextIdenticalWith(this@nextMessageContainingAsync) }
}.message.first<M>()
}
}
/**
* 挂起当前协程, 等待下一条 [MessagePacket.sender] [MessagePacket.subject] [this] 相同并含有 [M] 类型的消息的 [MessagePacket]
*
@ -305,4 +347,16 @@ suspend inline fun <reified M : Message> MessagePacket<*, *>.nextMessageContaini
return subscribingGetOrNull<MessagePacket<*, *>, MessagePacket<*, *>>(timeoutMillis) {
takeIf { this.isContextIdenticalWith(this@nextMessageContainingOrNull) }
}?.message?.first()
}
}
inline fun <reified M : Message> MessagePacket<*, *>.nextMessageContainingOrNullAsync(
timeoutMillis: Long = -1,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Deferred<M?> {
return this.bot.async(coroutineContext) {
subscribingGetOrNull<MessagePacket<*, *>, MessagePacket<*, *>>(timeoutMillis) {
takeIf { this.isContextIdenticalWith(this@nextMessageContainingOrNullAsync) }
}?.message?.first<M>()
}
}