
机器人框架已更新,支持通过BotFunction注解进行服务发现。注解现包含target和retention元数据,支持在运行时使用反射进行处理。服务实现现在 suspend 函数,以支持异步行为。此外,引入了新的配置属性来定义机器人管理员ID。```
74 lines
2.3 KiB
Kotlin
74 lines
2.3 KiB
Kotlin
package info.alinadace.miraibot.bean
|
|
|
|
import info.alinadace.miraibot.annotation.BotFunction
|
|
import info.alinadace.miraibot.config.BotConfig
|
|
import info.alinadace.miraibot.enums.LoginType
|
|
import info.alinadace.miraibot.service.Service
|
|
import jakarta.annotation.Resource
|
|
import net.mamoe.mirai.Bot
|
|
import net.mamoe.mirai.BotFactory
|
|
import net.mamoe.mirai.auth.BotAuthorization
|
|
import net.mamoe.mirai.event.Event
|
|
import net.mamoe.mirai.utils.BotConfiguration
|
|
import org.springframework.boot.autoconfigure.AutoConfiguration
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|
import org.springframework.context.annotation.Bean
|
|
import java.util.Collections
|
|
import java.util.HashMap
|
|
import kotlin.reflect.KClass
|
|
|
|
/**
|
|
* 机器人配置
|
|
* @author Kane
|
|
* @since 2024/8/29 下午2:47
|
|
*/
|
|
@AutoConfiguration
|
|
@EnableConfigurationProperties(BotConfig::class)
|
|
class BotConfiguration {
|
|
|
|
@Resource
|
|
private val services: List<Service<Event>> = Collections.emptyList();
|
|
|
|
@Bean(initMethod = "login")
|
|
fun bot(config: BotConfig): Bot{
|
|
return when (config.type) {
|
|
LoginType.PASSWORD -> BotFactory.newBot(config.id, config.password){
|
|
extracted()
|
|
}
|
|
LoginType.QRCODE -> BotFactory.newBot(config.id, BotAuthorization.byQRCode()){
|
|
extracted()
|
|
}
|
|
}
|
|
}
|
|
|
|
@Bean
|
|
fun botFunctionMap(): HashMap<KClass<out Event>, MutableList<Service<Event>>>{
|
|
val eventMap = HashMap<KClass<out Event>, MutableList<Service<Event>>>();
|
|
for (service in services) {
|
|
val clazz = service::class
|
|
val annotations = clazz.annotations
|
|
if (annotations.isEmpty()) continue
|
|
var function: BotFunction? = null
|
|
for (annotation in annotations) {
|
|
if (annotation is BotFunction){
|
|
function = annotation
|
|
}
|
|
}
|
|
if (function == null){
|
|
continue
|
|
}
|
|
for (kClass in function.value) {
|
|
val serviceList = eventMap.getOrPut(kClass) { mutableListOf() }
|
|
serviceList.add(service)
|
|
}
|
|
}
|
|
return eventMap
|
|
}
|
|
|
|
private fun BotConfiguration.extracted() {
|
|
fileBasedDeviceInfo()
|
|
protocol = BotConfiguration.MiraiProtocol.ANDROID_WATCH
|
|
enableContactCache()
|
|
}
|
|
}
|