mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-23 06:10:30 +08:00
Add subscribers for TempMessage
This commit is contained in:
parent
2053ab198c
commit
3dafb8ea90
@ -23,6 +23,7 @@ import net.mamoe.mirai.event.events.BotEvent
|
||||
import net.mamoe.mirai.message.ContactMessage
|
||||
import net.mamoe.mirai.message.FriendMessage
|
||||
import net.mamoe.mirai.message.GroupMessage
|
||||
import net.mamoe.mirai.message.TempMessage
|
||||
import net.mamoe.mirai.message.data.Message
|
||||
import net.mamoe.mirai.message.data.firstIsInstance
|
||||
import net.mamoe.mirai.utils.SinceMirai
|
||||
@ -115,6 +116,31 @@ fun <R> CoroutineScope.subscribeFriendMessages(
|
||||
}.run(listeners)
|
||||
}
|
||||
|
||||
typealias TempMessageSubscribersBuilder = MessageSubscribersBuilder<TempMessage, Listener<TempMessage>, Unit, Unit>
|
||||
|
||||
/**
|
||||
* 订阅来自所有 [Bot] 的所有临时会话消息事件
|
||||
*
|
||||
* @see CoroutineScope.incoming 打开一个指定事件的接收通道
|
||||
*/
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
fun <R> CoroutineScope.subscribeTempMessages(
|
||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
||||
listeners: TempMessageSubscribersBuilder.() -> R
|
||||
): R {
|
||||
contract {
|
||||
callsInPlace(listeners, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return TempMessageSubscribersBuilder(Unit) { filter, listener ->
|
||||
subscribeAlways(coroutineContext, concurrencyKind) {
|
||||
val toString = this.message.contentToString()
|
||||
if (filter(this, toString))
|
||||
listener(this, toString)
|
||||
}
|
||||
}.run(listeners)
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅来自这个 [Bot] 的所有联系人的消息事件. 联系人可以是任意群或任意好友或临时会话.
|
||||
*
|
||||
@ -186,6 +212,31 @@ fun <R> Bot.subscribeFriendMessages(
|
||||
}.run(listeners)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 订阅来自这个 [Bot] 的所有临时会话消息事件.
|
||||
*
|
||||
* @see CoroutineScope.incoming 打开一个指定事件的接收通道
|
||||
*/
|
||||
@SinceMirai("0.35.0")
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
fun <R> Bot.subscribeTempMessages(
|
||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||
concurrencyKind: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
|
||||
listeners: TempMessageSubscribersBuilder.() -> R
|
||||
): R {
|
||||
contract {
|
||||
callsInPlace(listeners, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return TempMessageSubscribersBuilder(Unit) { filter, listener ->
|
||||
this.subscribeAlways(coroutineContext, concurrencyKind) {
|
||||
val toString = this.message.contentToString()
|
||||
if (filter(this, toString))
|
||||
listener(this, toString)
|
||||
}
|
||||
}.run(listeners)
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开一个指定事件的接收通道
|
||||
*
|
||||
@ -582,6 +633,10 @@ open class MessageSubscribersBuilder<M : ContactMessage, out Ret, R : RR, RR>(
|
||||
@MessageDsl
|
||||
fun sentByFriend(): ListeningFilter = newListeningFilter { this is FriendMessage }
|
||||
|
||||
/** 如果是好友发来的消息 */
|
||||
@MessageDsl
|
||||
fun sentByTemp(): ListeningFilter = newListeningFilter { this is TempMessage }
|
||||
|
||||
/** 如果是管理员或群主发的消息 */
|
||||
@MessageDsl
|
||||
fun sentByOperator(): ListeningFilter =
|
||||
|
Loading…
Reference in New Issue
Block a user