EventChannel

open class EventChannel<out BaseEvent : Event>

事件通道. 事件通道是监听事件的入口. 在不同的事件通道中可以监听到不同类型的事件.

GlobalEventChannel 是最大的通道: 所有的事件都可以在 GlobalEventChannel 监听到. 通过 Bot.eventChannel 得到的通道只能监听到来自这个 Bot 的事件.

对通道的操作

创建事件监听

获取事件通道

See also

Functions

Link copied to clipboard
fun asChannel(    capacity: Int = Channel.RENDEZVOUS,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrency: ConcurrencyKind = CONCURRENT,     priority: EventPriority = EventPriority.NORMAL): <ERROR CLASS><out BaseEvent>

创建事件监听并将监听结果发送在 Channel. 将返回值 关闭Channel.close 时将会同时关闭事件监听.

Link copied to clipboard
fun context(vararg coroutineContexts: CoroutineContext): EventChannel<BaseEvent>

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

Link copied to clipboard
fun exceptionHandler(coroutineExceptionHandler: <ERROR CLASS>): EventChannel<BaseEvent>

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

fun exceptionHandler(coroutineExceptionHandler: (exception: Throwable) -> Unit): EventChannel<BaseEvent>

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

Link copied to clipboard
fun filter(filter: (BaseEvent) -> Boolean): EventChannel<BaseEvent>

EventChannel.filter 的 Java 版本.

fun filter(filter: suspend (BaseEvent) -> Boolean): EventChannel<BaseEvent>

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

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

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

Link copied to clipboard
fun forwardToChannel(    channel: <ERROR CLASS><@UnsafeVariance BaseEvent>,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     priority: EventPriority = EventPriority.MONITOR): Listener<@UnsafeVariance BaseEvent>

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

Link copied to clipboard
fun parentJob(job: <ERROR CLASS>): EventChannel<BaseEvent>

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

Link copied to clipboard
fun parentScope(coroutineScope: <ERROR CLASS>): EventChannel<BaseEvent>

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

Link copied to clipboard
fun registerListenerHost(host: ListenerHost, coroutineContext: CoroutineContext = EmptyCoroutineContext)

注册 ListenerHost 中的所有 EventHandler 标注的方法到这个 EventChannel. 查看 EventHandler.

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

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

fun <E : Event> subscribe(    eventClass: <ERROR CLASS><out E>,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrency: ConcurrencyKind = CONCURRENT,     priority: EventPriority = EventPriority.NORMAL,     handler: <ERROR CLASS><E, ListeningStatus>): Listener<E>

Java API. 查看 subscribe 获取更多信息.

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 <E : Event> subscribeAlways(    coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrency: ConcurrencyKind = CONCURRENT,     priority: EventPriority = EventPriority.NORMAL,     noinline handler: suspend E.(E) -> Unit): Listener<E>

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

fun <E : Event> subscribeAlways(    eventClass: <ERROR CLASS><out E>,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrency: ConcurrencyKind = CONCURRENT,     priority: EventPriority = EventPriority.NORMAL,     handler: <ERROR CLASS><E>): Listener<E>

Java API. 查看 subscribeAlways 获取更多信息.

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 <E : Event> subscribeOnce(    coroutineContext: CoroutineContext = EmptyCoroutineContext,     priority: EventPriority = EventPriority.NORMAL,     noinline handler: suspend E.(E) -> Unit): Listener<E>

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

fun <E : Event> subscribeOnce(    eventClass: KClass<out E>,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     priority: EventPriority = EventPriority.NORMAL,     handler: suspend E.(E) -> Unit): Listener<E>

fun <E : Event> subscribeOnce(    eventClass: <ERROR CLASS><out E>,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrency: ConcurrencyKind = CONCURRENT,     priority: EventPriority = EventPriority.NORMAL,     handler: <ERROR CLASS><E>): Listener<E>

Java API. 查看 subscribeOnce 获取更多信息.

Properties

Link copied to clipboard
val baseEventClass: KClass<out BaseEvent>
Link copied to clipboard
val defaultCoroutineContext: CoroutineContext

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

Inheritors

Link copied to clipboard

Extensions

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

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

Link copied to clipboard
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>
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>

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

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 挂起函数的函数引用的监听方式.

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 带接收者的函数的函数引用的监听方式.

Link copied to clipboard
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 挂起函数的函数引用的监听方式.

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>
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>

支持 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 带接收者的函数的函数引用的监听方式.

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
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<*>.subscribeTempMessages(    coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrencyKind: ConcurrencyKind = CONCURRENT,     priority: EventPriority = EventPriority.MONITOR,     listeners: GroupTempMessageSubscribersBuilder.() -> R): R

通过 DSL 订阅来自所有 Bot 的所有临时会话消息事件. 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