mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-09 01:30:17 +08:00
Add async shortcuts for nextMessage
This commit is contained in:
parent
79bb9e7dc9
commit
ecda34deee
@ -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>()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user