GlobalEventChannel

全局事件通道. 此通道包含来自所有 Bot 的所有类型的事件. 可通过 EventChannel.filter 过滤得到范围更小的 EventChannel.

See also

Functions

Link copied to clipboard
open override fun asFlow(): Flow<Event>

通过 Flow 接收此通道内的所有事件.

Link copied to clipboard
open override fun context(vararg coroutineContexts: CoroutineContext): EventChannel<Event>

创建一个新的 EventChannel, 该 EventChannel 包含 this.coroutineContext 和添加的 coroutineContexts. coroutineContexts 会覆盖 defaultCoroutineContext 中的重复元素.

Link copied to clipboard
expect fun exceptionHandler(coroutineExceptionHandler: (exception: Throwable) -> Unit): EventChannel<Event>

创建一个新的 EventChannel, 该 EventChannel 包含 this.coroutineContext 和添加的 coroutineExceptionHandler

expect fun exceptionHandler(coroutineExceptionHandler: CoroutineExceptionHandler): EventChannel<Event>

创建一个新的 EventChannel, 该 EventChannel 包含 this.coroutineContext 和添加的 coroutineExceptionHandler

Link copied to clipboard
expect fun filter(filter: (event: Event) -> Boolean): EventChannel<Event>

EventChannel.filter 的 Java 版本.

expect fun filter(filter: suspend (event: Event) -> Boolean): EventChannel<Event>

添加一个过滤器. 过滤器将在收到任何事件之后, 传递给通过 EventChannel.subscribe 注册的监听器之前调用.

Link copied to clipboard
expect inline fun <E : Event> filterIsInstance(): EventChannel<E>
expect fun <E : Event> filterIsInstance(kClass: KClass<out E>): EventChannel<E>

过滤事件的类型. 返回一个只包含 E 类型事件的 EventChannel

Link copied to clipboard
expect fun forwardToChannel(channel: SendChannel<Event>, coroutineContext: CoroutineContext = EmptyCoroutineContext, priority: EventPriority = EventPriority.MONITOR): Listener<Event>

创建事件监听并将监听结果转发到 channel. 当 Channel.send 抛出 ClosedSendChannelException 时停止 Listener 监听和转发.

Link copied to clipboard
inline suspend fun <E : Event> EventChannel<*>.nextEvent(priority: EventPriority = EventPriority.NORMAL, noinline filter: suspend (E) -> Boolean = { true }): E
inline suspend fun <E : Event> EventChannel<*>.nextEvent(priority: EventPriority = EventPriority.NORMAL, intercept: Boolean = false, noinline filter: suspend (E) -> Boolean = { true }): E

挂起当前协程, 直到监听到事件 E 的广播并通过 filter, 返回这个事件实例.

Link copied to clipboard
expect fun parentJob(job: Job): EventChannel<Event>

指定协程父 Job. 之后在此 EventChannel 下创建的事件监听器都会成为 job 的子任务, 当 job 被取消时, 所有的事件监听器都会被取消.

Link copied to clipboard
expect fun parentScope(coroutineScope: CoroutineScope): EventChannel<Event>

coroutineScope 作为这个 EventChannel 的父作用域.

Link copied to clipboard
expect inline fun <E : Event> subscribe(coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrency: ConcurrencyKind = LOCKED, priority: EventPriority = EventPriority.NORMAL, noinline handler: suspend E.(E) -> ListeningStatus): Listener<E>

创建一个事件监听器, 监听事件通道中所有 E 及其子类事件.

expect fun <E : Event> subscribe(eventClass: KClass<out E>, coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrency: ConcurrencyKind = LOCKED, priority: EventPriority = EventPriority.NORMAL, handler: suspend E.(E) -> ListeningStatus): Listener<E>

subscribe 的区别是接受 eventClass 参数, 而不使用 reified 泛型. 通常推荐使用具体化类型参数.

Link copied to clipboard
inline fun <BaseEvent : Event, E : Event> EventChannel<BaseEvent>.subscribe(crossinline handler: (E) -> ListeningStatus, priority: EventPriority = EventPriority.NORMAL, concurrency: ConcurrencyKind = ConcurrencyKind.CONCURRENT, coroutineContext: CoroutineContext = EmptyCoroutineContext): Listener<E>
inline fun <BaseEvent : Event, E : Event> EventChannel<BaseEvent>.subscribe(crossinline handler: suspend E.(E) -> ListeningStatus, priority: EventPriority = EventPriority.NORMAL, concurrency: ConcurrencyKind = ConcurrencyKind.CONCURRENT, coroutineContext: CoroutineContext = EmptyCoroutineContext): Listener<E>

支持 Kotlin 带接收者的挂起函数的函数引用的监听方式.

inline fun <BaseEvent : Event, E : Event> EventChannel<BaseEvent>.subscribe(crossinline handler: E.(E) -> ListeningStatus, priority: EventPriority = EventPriority.NORMAL, concurrency: ConcurrencyKind = ConcurrencyKind.CONCURRENT, coroutineContext: CoroutineContext = EmptyCoroutineContext): Listener<E>

支持 Kotlin 带接收者的函数的函数引用的监听方式.

@JvmName(name = "subscribe1")
inline fun <BaseEvent : Event, E : Event> EventChannel<BaseEvent>.subscribe(crossinline handler: suspend (E) -> ListeningStatus, priority: EventPriority = EventPriority.NORMAL, concurrency: ConcurrencyKind = ConcurrencyKind.CONCURRENT, coroutineContext: CoroutineContext = EmptyCoroutineContext): Listener<E>

支持 Kotlin 挂起函数的函数引用的监听方式.

Link copied to clipboard
expect inline fun <E : Event> subscribeAlways(coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrency: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.NORMAL, noinline handler: suspend E.(E) -> Unit): Listener<E>

创建一个事件监听器, 监听事件通道中所有 E 及其子类事件. 每当 事件广播 时, handler 都会被执行.

expect fun <E : Event> subscribeAlways(eventClass: KClass<out E>, coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrency: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.NORMAL, handler: suspend E.(E) -> Unit): Listener<E>
Link copied to clipboard
inline fun <BaseEvent : Event, E : Event> EventChannel<BaseEvent>.subscribeAlways(crossinline handler: (E) -> Unit, priority: EventPriority = EventPriority.NORMAL, concurrency: ConcurrencyKind = ConcurrencyKind.CONCURRENT, coroutineContext: CoroutineContext = EmptyCoroutineContext): Listener<E>
inline fun <BaseEvent : Event, E : Event> EventChannel<BaseEvent>.subscribeAlways(crossinline handler: suspend E.(E) -> Unit, priority: EventPriority = EventPriority.NORMAL, concurrency: ConcurrencyKind = ConcurrencyKind.CONCURRENT, coroutineContext: CoroutineContext = EmptyCoroutineContext): Listener<E>

支持 Kotlin 带接收者的挂起函数的函数引用的监听方式.

inline fun <BaseEvent : Event, E : Event> EventChannel<BaseEvent>.subscribeAlways(crossinline handler: E.(E) -> Unit, priority: EventPriority = EventPriority.NORMAL, concurrency: ConcurrencyKind = ConcurrencyKind.CONCURRENT, coroutineContext: CoroutineContext = EmptyCoroutineContext): Listener<E>

支持 Kotlin 带接收者的函数的函数引用的监听方式.

@JvmName(name = "subscribeAlways1")
inline fun <BaseEvent : Event, E : Event> EventChannel<BaseEvent>.subscribeAlways(crossinline handler: suspend (E) -> Unit, priority: EventPriority = EventPriority.NORMAL, concurrency: ConcurrencyKind = ConcurrencyKind.CONCURRENT, coroutineContext: CoroutineContext = EmptyCoroutineContext): Listener<E>

支持 Kotlin 挂起函数的函数引用的监听方式.

Link copied to clipboard
fun <R> EventChannel<*>.subscribeFriendMessages(coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrencyKind: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.MONITOR, listeners: FriendMessageSubscribersBuilder.() -> R): R

通过 DSL 订阅来自所有 Bot 的所有好友消息事件. DSL 语法查看 subscribeMessages.

Link copied to clipboard
fun <R> EventChannel<*>.subscribeGroupMessages(coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrencyKind: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.MONITOR, listeners: GroupMessageSubscribersBuilder.() -> R): R

通过 DSL 订阅来自所有 Bot 的所有群会话消息事件. DSL 语法查看 subscribeMessages.

Link copied to clipboard
fun <R> EventChannel<*>.subscribeGroupTempMessages(coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrencyKind: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.MONITOR, listeners: GroupTempMessageSubscribersBuilder.() -> R): R

通过 DSL 订阅来自所有 Bot 的所有 GroupTempMessageEvent. DSL 语法查看 subscribeMessages.

Link copied to clipboard
fun <R> EventChannel<*>.subscribeMessages(coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrencyKind: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.MONITOR, listeners: MessageEventSubscribersBuilder.() -> R): R

通过 DSL 订阅来自所有 Bot 的所有联系人的消息事件.

Link copied to clipboard
expect inline fun <E : Event> subscribeOnce(coroutineContext: CoroutineContext = EmptyCoroutineContext, priority: EventPriority = EventPriority.NORMAL, noinline handler: suspend E.(E) -> Unit): Listener<E>

创建一个事件监听器, 监听事件通道中所有 E 及其子类事件, 只监听一次. 当 事件广播 时, handler 会被执行.

expect fun <E : Event> subscribeOnce(eventClass: KClass<out E>, coroutineContext: CoroutineContext = EmptyCoroutineContext, priority: EventPriority = EventPriority.NORMAL, handler: suspend E.(E) -> Unit): Listener<E>
Link copied to clipboard
fun <R> EventChannel<*>.subscribeOtherClientMessages(coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrencyKind: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.MONITOR, listeners: OtherClientMessageSubscribersBuilder.() -> R): R

通过 DSL 订阅来自所有 Bot 的所有 OtherClient 消息事件. DSL 语法查看 subscribeMessages.

Link copied to clipboard
fun <R> EventChannel<*>.subscribeStrangerMessages(coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrencyKind: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.MONITOR, listeners: StrangerMessageSubscribersBuilder.() -> R): R

通过 DSL 订阅来自所有 Bot 的所有 Stranger 消息事件. DSL 语法查看 subscribeMessages.

Link copied to clipboard
fun <R> EventChannel<*>.subscribeUserMessages(coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrencyKind: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.MONITOR, listeners: UserMessageSubscribersBuilder.() -> R): R

通过 DSL 订阅来自所有 Bot 的所 User 消息事件. DSL 语法查看 subscribeMessages.

Link copied to clipboard
inline suspend fun <E : Event, R : Any> EventChannel<*>.syncFromEvent(priority: EventPriority = EventPriority.NORMAL, noinline mapper: suspend (E) -> R?): R

挂起当前协程, 监听事件 E, 并尝试从这个事件中获取一个值, 在超时时抛出 TimeoutCancellationException

Properties

Link copied to clipboard
expect val baseEventClass: KClass<out Event>
Link copied to clipboard

此事件通道的默认 CoroutineScope.coroutineContext. 将会被添加给所有注册的事件监听器.