All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.love.forte.simbot.kook.stdlib.BotSubscribes.kt Maven / Gradle / Ivy

The newest version!
/*
 *     Copyright (c) 2024. ForteScarlet.
 *
 *     This file is part of simbot-component-kook.
 *
 *     simbot-component-kook is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     simbot-component-kook is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with simbot-component-kook,
 *     If not, see .
 */

package love.forte.simbot.kook.stdlib

import love.forte.simbot.kook.event.Event
import love.forte.simbot.kook.event.EventExtra


/**
 * 订阅一个具体的 [EventExtra] 类型的事件。
 *
 * ```kotlin
 * bot.subscribe { raw ->
 *      // ...
 * }
 * ```
 *
 * @param subscribeSequence 事件处理器的序列类型,默认为 [SubscribeSequence.NORMAL]
 * @param EX 事件内容 [Event.extra] 的具体类型。
 */
public inline fun  Bot.subscribe(
    subscribeSequence: SubscribeSequence = SubscribeSequence.NORMAL,
    crossinline processor: suspend Event.(raw: String) -> Unit
) {
    subscribe(subscribeSequence) { raw ->
        if (extra is EX) {
            @Suppress("UNCHECKED_CAST")
            processor.invoke(this as Event, raw)
        }
    }
}

/**
 * 订阅一个 [Event.type] 目标的事件。
 *
 * ```kotlin
 * bot.subscribe(type) { raw ->
 *      // ...
 * }
 * ```
 *
 * @param type 事件类型
 * @param subscribeSequence 事件处理器的序列类型,默认为 [SubscribeSequence.NORMAL]
 */
public inline fun Bot.subscribe(
    type: Event.Type,
    subscribeSequence: SubscribeSequence = SubscribeSequence.NORMAL,
    crossinline processor: suspend Event<*>.(raw: String) -> Unit
) {
    subscribe(subscribeSequence) { raw ->
        if (this.type == type) {
            processor(raw)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy