Sakura-Miki/src/main/kotlin/info/alinadace/sakuramiki/configuration/Initialization.kt
Grand-cocoa 9755545639 文档更新与代码优化
- 移除了过时的MapperScan注解,调整了相关服务的注解位置,以适应新的映射器配置。
- 对服务类中的日志对象创建方式进行了微调,采用companion object模式进行优化。
- 删除了与喝水提醒服务相关的多个文件,可能由于这些功能不再需要或被重构到其他文件中。
2024-09-23 18:41:54 +08:00

61 lines
2.0 KiB
Kotlin

package info.alinadace.sakuramiki.configuration
import info.alinadace.sakuramiki.service.Service
import io.github.kloping.qqbot.Starter
import io.github.kloping.qqbot.api.event.Event
import io.github.kloping.qqbot.impl.ListenerHost
import jakarta.annotation.Resource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.event.EventListener
import kotlin.reflect.KClass
/**
* 机器人功能初始化
* @author Kane
* @since 2024/9/4 18:49
*/
@org.springframework.stereotype.Service
class Initialization {
@Suppress("SpringJavaInjectionPointsAutowiringInspection")
@Resource(name = "functionMap")
lateinit var functionMap: HashMap<KClass<out Event>, MutableList<Service<Event>>>
@Resource
lateinit var starter: Starter
val log: Logger = LoggerFactory.getLogger(this::class.java)
@EventListener(ApplicationReadyEvent::class)
suspend fun initFunction() {
val eventListener = object : ListenerHost() {
override fun handleException(e: Throwable?): Boolean {
log.error("Exception: {}", e?.message, e)
return super.handleException(e)
}
@EventReceiver
fun onEvent(event: Event) {
log.info("Event: {}", event)
val filter = functionMap.keys.filter { x -> x.isInstance(event) }
filter.forEach { x ->
if (x.isInstance(event)) {
functionMap[x]?.forEach { y ->
if (y.entrance(event)) {
y.active(event)
}
}
}
}
log.info("Event-End: {}", event)
}
}
starter.registerListenerHost(eventListener)
log.info("Channel complete")
log.info("Login: id:{}", starter.config.appid)
starter.run()
}
}