subscribeMessages

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

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

eventChannel.subscribeMessages {
    "test" {
        // 当消息内容为 "test" 时执行
        // this: MessageEvent
        reply("test!")
    }

    "Hello" reply "Hi" // 当消息内容为 "Hello" 时回复 "Hi"
    "quote me" quoteReply "ok" // 当消息内容为 "quote me" 时引用该消息并回复 "ok"
    "quote me2" quoteReply {
        // lambda 也是允许的:
        // 返回值接受 Any?
        // 为 Unit 时不发送任何内容;
        // 为 Message 时直接发送;
        // 为 String 时发送为 PlainText;
        // 否则 toString 并发送为 PlainText

        "ok"
    }

    case("iGNorECase", ignoreCase=true) reply "OK" // 忽略大小写
    startsWith("-") reply { cmd ->
        // 当消息内容以 "-" 开头时执行
        // cmd 为消息去除开头 "-" 的内容
    }


    val listener: Listener<MessageEvent> = "1" reply "2"
    // 每个语句都会被注册为事件监听器,可以这样获取监听器

    listener.complete() // 停止 "1" reply "2" 这个事件监听
}

See also