Skip to content

创建BaseCommand全局拦截器

kotlin
class CustomInterceptor : ExecutionInterceptor() {
    override suspend fun beforeGroupExecute(message: GroupMessage, command: BaseCommand): CommandExecutionResult {
        println("before")
        return CommandExecutionResult.CONTINUE
    }

    override suspend fun afterGroupExecute(message: GroupMessage, command: BaseCommand) {
        println("after")
    }
}

然后注册这个拦截器

kotlin
suspend fun main() {
    OneBotFactory.setInterceptor(CustomInterceptor())
}

创建BaseCommand局部拦截器

只需要在构建指令的时候将上面的全局拦截器的实例传入BaseCommand即可(是的他们两个用的拦截器是同一套代码), 就像下面这样

kotlin
class EchoCommand : BaseCommand(CustomInterceptor()) {
    override val commandNames = listOf("/echo", "/eee")

    override suspend fun executeGroup(message: GroupMessage, args: List<String>) {
        message.reply("11")
    }
}

以Apache-2.0开源协议开源