65 lines
2.0 KiB
Kotlin
65 lines
2.0 KiB
Kotlin
package info.alinadace.miraibot.bean
|
|
|
|
import info.alinadace.miraibot.annotation.BotFunction
|
|
import info.alinadace.miraibot.configuration.BotConfig
|
|
import info.alinadace.miraibot.service.Service
|
|
import kotlinx.coroutines.async
|
|
import kotlinx.coroutines.runBlocking
|
|
import net.mamoe.mirai.Bot
|
|
import net.mamoe.mirai.event.Event
|
|
import net.mamoe.mirai.utils.BotConfiguration
|
|
import org.springframework.context.annotation.Bean
|
|
import org.springframework.context.annotation.Configuration
|
|
import top.mrxiaom.overflow.BotBuilder
|
|
import kotlin.reflect.KClass
|
|
|
|
/**
|
|
* 机器人配置
|
|
* @author Kane
|
|
* @since 2024/8/29 下午2:47
|
|
*/
|
|
@Configuration
|
|
class BotConfiguration {
|
|
|
|
@Bean
|
|
fun bot(config: BotConfig): Bot {
|
|
val connect = runBlocking {
|
|
val bot = async {
|
|
BotBuilder.positive(config.ws).token(config.token).connect() ?: throw Exception("Bot connect failed")
|
|
}
|
|
bot.await()
|
|
}
|
|
return connect
|
|
}
|
|
|
|
@Bean("functionMap")
|
|
fun botFunctionMap(services: List<Service<out Event>>): HashMap<KClass<out Event>, MutableList<Service<out Event>>> {
|
|
val eventMap = HashMap<KClass<out Event>, MutableList<Service<out 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_PAD
|
|
enableContactCache()
|
|
}
|
|
}
|