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

com.blr19c.falowp.bot.system.plugin.hook.Hook.kt Maven / Gradle / Ivy

The newest version!
package com.blr19c.falowp.bot.system.plugin.hook

import com.blr19c.falowp.bot.system.api.BotApi
import com.blr19c.falowp.bot.system.listener.hooks.ReceiveMessageHook
import com.blr19c.falowp.bot.system.plugin.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive

/**
 * 钩子函数
 * @param hook 钩子的信息
 * @param botApi 当前位置的botApi
 * @param block 执行内容
 */
suspend fun withPluginHook(
    botApi: BotApi,
    hook: Plugin.Listener.Hook,
    block: suspend () -> Unit
) {
    HookManager.buildHookJoinPoint(hook, botApi, block).process()
}

/**
 * 注册运行时钩子
 * 注意:请使用hook内的botApi而不是注册hook的botApi
 */
inline fun  BotApi.runtimeHook(
    hookType: HookTypeEnum,
    order: Int = 0,
    match: HookPluginRegisterMatch = HookPluginRegisterMatch.allMatch(),
    noinline block: suspend HookJoinPoint.(T, UnRegister) -> Unit
): UnRegister {
    var hook: UnRegister? = null
    hook = HookPluginRegister(order, T::class, hookType, match, {
        block.invoke(this, it, hook!!)
    })
    hook.register()
    return hook
}

/**
 * 监听下次匹配的消息
 */
suspend fun  BotApi.awaitReply(
    match: MessagePluginRegisterMatch = MessagePluginRegisterMatch.allMatch(),
    block: suspend BotApi.(args: Array) -> T
): T {
    var data: T? = null
    this.runtimeHook(HookTypeEnum.BEFORE) { (receiveMessage), unRegister ->
        val botApi = this.botApi()
        if (match.checkMath(receiveMessage)) {
            val args = match.regex?.find(receiveMessage.content.message)?.destructured?.toList() ?: listOf()
            try {
                data = block.invoke(botApi, args.toTypedArray())
            } finally {
                unRegister.unregister()
            }
            return@runtimeHook
        }
    }
    while (isActive && data == null) {
        delay(100)
    }
    return data!!
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy