Package net.mamoe.mirai.event

Types

AbstractEvent
Link copied to clipboard
abstract class AbstractEvent : Event

所有实现了 Event 接口的类都应该继承的父类.

BroadcastControllable
Link copied to clipboard
interface BroadcastControllable : Event

可控制是否需要广播这个事件

CancellableEvent
Link copied to clipboard
interface CancellableEvent : Event

可被取消的事件

ConcurrencyKind
Link copied to clipboard
enum ConcurrencyKind : Enum<ConcurrencyKind>
Event
Link copied to clipboard
interface Event

可被监听的类, 可以是任何 class 或 object.

EventChannel
Link copied to clipboard
open class EventChannel<out BaseEvent : Event>

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

EventHandler
Link copied to clipboard
@Target(allowedTargets = [AnnotationTarget.FUNCTION])
annotation class EventHandler(priority: EventPriority, ignoreCancelled: Boolean, concurrency: ConcurrencyKind)

标注一个函数为事件监听器.

EventPriority
Link copied to clipboard
enum EventPriority : Enum<EventPriority>

事件优先级.

ExceptionInEventChannelFilterException
Link copied to clipboard
class ExceptionInEventChannelFilterException(event: Event, eventChannel: EventChannel<*>, message: String, cause: Throwable) : IllegalStateException

包装 EventChannel.filterfilter lambda 抛出的异常并重新抛出.

ExceptionInEventHandlerException
Link copied to clipboard
class ExceptionInEventHandlerException(event: Event, message: String, cause: Throwable) : IllegalStateException

EventHandler 标记的函数在处理事件时产生异常时包装异常并重新抛出

GlobalEventChannel
Link copied to clipboard
object GlobalEventChannel : EventChannel<Event>

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

Listener
Link copied to clipboard
interface Listener<in E : Event> : CompletableJob

事件监听器. 由 EventChannel.subscribe 等方法返回.

ListenerHost
Link copied to clipboard
interface ListenerHost

实现这个接口的对象可以通过 EventHandler 标注事件监听函数, 并通过 registerTo 注册.

ListeningStatus
Link copied to clipboard
enum ListeningStatus : Enum<ListeningStatus>

订阅者的状态

MessageDsl
Link copied to clipboard
annotation class MessageDsl

DSL 标记. 将能让 IDE 阻止一些错误的方法调用.

MessageEventSubscribersBuilder
Link copied to clipboard
MessageListener
Link copied to clipboard
typealias MessageListener<T, R> = suspend T.(String) -> R

消息事件的处理器.

注: 接受者 T 为 MessageEvent 参数 String 为 转为字符串了的消息 (Message.toString)

MessageSelectBuilder
Link copied to clipboard
abstract class MessageSelectBuilder<M : MessageEvent, R> : MessageSelectBuilderUnit<M, R>

selectMessages 时的 DSL 构建器.

MessageSelectBuilderUnit
Link copied to clipboard
abstract class MessageSelectBuilderUnit<M : MessageEvent, R> : MessageSubscribersBuilder<M, Unit, R, Any?>

selectMessagesUnitselectMessages 时的 DSL 构建器.

MessageSelectionTimeoutChecker
Link copied to clipboard
value class MessageSelectionTimeoutChecker
MessageSelectionTimeoutException
Link copied to clipboard
class MessageSelectionTimeoutException : RuntimeException
MessageSubscribersBuilder
Link copied to clipboard
open class MessageSubscribersBuilder<M : MessageEvent, out Ret, R : RR, RR>

消息订阅构造器

SimpleListenerHost
Link copied to clipboard
abstract class SimpleListenerHost constructor(coroutineContext: CoroutineContext) : ListenerHost, CoroutineScope

携带一个异常处理器的 ListenerHost.

Functions

asyncFromEvent
Link copied to clipboard
inline fun <E : Event, R : Any> CoroutineScope.asyncFromEvent(timeoutMillis: Long = -1, coroutineContext: CoroutineContext = EmptyCoroutineContext, priority: EventPriority = EventPriority.MONITOR, crossinline mapper: suspend E.(E) -> R?): Deferred<R>

异步监听这个事件, 并尝试从这个事件中获取一个值.

asyncFromEventOrNull
Link copied to clipboard
inline fun <E : Event, R : Any> CoroutineScope.asyncFromEventOrNull(timeoutMillis: Long, coroutineContext: CoroutineContext = EmptyCoroutineContext, priority: EventPriority = EventPriority.MONITOR, crossinline mapper: suspend E.(E) -> R?): Deferred<R?>

异步监听这个事件, 并尝试从这个事件中获取一个值.

broadcast
Link copied to clipboard
suspend fun <E : Event> E.broadcast(): E

广播一个事件的唯一途径.

globalEventChannel
Link copied to clipboard
fun CoroutineScope.globalEventChannel(coroutineContext: CoroutineContext = EmptyCoroutineContext): EventChannel<Event>

在此 CoroutineScope 下创建一个监听所有事件的 EventChannel. 相当于 GlobalEventChannel.parentScope(this).context(coroutineContext).

nextEvent
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> nextEvent(timeoutMillis: Long = -1, priority: EventPriority = EventPriority.MONITOR, crossinline filter: (E) -> Boolean = { true }): E

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

nextEventAsync
Link copied to clipboard
inline fun <E : Event> CoroutineScope.nextEventAsync(timeoutMillis: Long = -1, priority: EventPriority = EventPriority.MONITOR, coroutineContext: CoroutineContext = EmptyCoroutineContext, crossinline filter: (E) -> Boolean = { true }): Deferred<E>

返回一个 Deferred, 其值为下一个广播并通过 filter 的事件 E 示例.

nextEventOrNull
Link copied to clipboard
inline suspend fun <E : Event> nextEventOrNull(timeoutMillis: Long, priority: EventPriority = EventPriority.MONITOR, crossinline filter: (E) -> Boolean = { true }): E?

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

nextEventOrNullAsync
Link copied to clipboard
inline fun <E : Event> CoroutineScope.nextEventOrNullAsync(timeoutMillis: Long, priority: EventPriority = EventPriority.MONITOR, coroutineContext: CoroutineContext = EmptyCoroutineContext, crossinline filter: (E) -> Boolean = { true }): Deferred<E?>

返回一个 Deferred, 其值为下一个广播并通过 filter 的事件 E 示例.

registerTo
Link copied to clipboard
inline fun <T : CoroutineScope, ListenerHost> T.registerTo(eventChannel: EventChannel<*>)

反射得到所有标注了 EventHandler 的函数 (Java 为方法), 并注册为事件监听器

selectMessages
Link copied to clipboard
inline suspend fun <T : MessageEvent, R> T.selectMessages(timeoutMillis: Long = -1, filterContext: Boolean = true, priority: EventPriority = EventPriority.MONITOR, crossinline selectBuilder: MessageSelectBuilder<T, R>.() -> Unit): R

挂起当前协程, 等待任意一个事件监听器触发后返回其返回值.

selectMessagesUnit
Link copied to clipboard
inline suspend fun <T : MessageEvent> T.selectMessagesUnit(timeoutMillis: Long = -1, filterContext: Boolean = true, priority: EventPriority = EventPriority.MONITOR, crossinline selectBuilder: MessageSelectBuilderUnit<T, Unit>.() -> Unit)

selectMessagesUnit 返回值捷径 (由于 Kotlin 无法推断 Unit 类型)

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

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

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

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

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

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

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

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 的所有联系人的消息事件.

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

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

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

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

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

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

syncFromEventOrNull
Link copied to clipboard
inline suspend fun <E : Event, R : Any> syncFromEventOrNull(timeoutMillis: Long, priority: EventPriority = EventPriority.MONITOR, crossinline mapper: suspend E.(E) -> R?): R?

挂起当前协程, 监听这个事件, 并尝试从这个事件中获取一个值, 在超时时返回 null

whileSelectMessages
Link copied to clipboard
inline suspend fun <T : MessageEvent> T.whileSelectMessages(timeoutMillis: Long = -1, filterContext: Boolean = true, priority: EventPriority = EventPriority.MONITOR, crossinline selectBuilder: MessageSelectBuilder<T, Boolean>.() -> Unit)

挂起当前协程, 等待任意一个事件监听器返回 false 后返回.

Properties

EventDisabled
Link copied to clipboard
var EventDisabled: Boolean = false

设置为 true 以关闭事件. 所有的 subscribe 都能正常添加到监听器列表, 但所有的广播都会直接返回.