refactor(miraibot): 更新服务和配置,优化bot初始化- 重构BotConfiguration以使用延迟初始化。
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 10s

- 直接返回Bot实例以简化代码。
- 更新ExampleService以处理FriendMessageEvent。
- 修正Service.kt中的泛型声明格式。
- 在BotConfig中添加新的ADMIN_ID属性并进行初始化检查。
- 调整工作流文件格式并优化结构。

通过这些更改,我们优化了bot的初始化过程,更新了事件处理服务,使代码更加简洁高效。此外,工作流文件的调整增强了可读性和结构清晰度。
This commit is contained in:
Grand-cocoa 2024-09-04 18:32:06 +08:00
parent 16fda28d79
commit a7aa2bda2b
5 changed files with 28 additions and 20 deletions

View File

@ -4,6 +4,7 @@ import info.alinadace.miraibot.annotation.BotFunction
import info.alinadace.miraibot.config.BotConfig import info.alinadace.miraibot.config.BotConfig
import info.alinadace.miraibot.enums.LoginType import info.alinadace.miraibot.enums.LoginType
import info.alinadace.miraibot.service.Service import info.alinadace.miraibot.service.Service
import jakarta.annotation.PostConstruct
import jakarta.annotation.Resource import jakarta.annotation.Resource
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.BotFactory import net.mamoe.mirai.BotFactory
@ -13,8 +14,6 @@ import net.mamoe.mirai.utils.BotConfiguration
import org.springframework.boot.autoconfigure.AutoConfiguration import org.springframework.boot.autoconfigure.AutoConfiguration
import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import java.util.Collections
import java.util.HashMap
import kotlin.reflect.KClass import kotlin.reflect.KClass
/** /**
@ -26,15 +25,18 @@ import kotlin.reflect.KClass
@EnableConfigurationProperties(BotConfig::class) @EnableConfigurationProperties(BotConfig::class)
class BotConfiguration { class BotConfiguration {
@Resource
private val services: List<Service<Event>> = Collections.emptyList();
@Bean(initMethod = "login") @Resource
private lateinit var services: List<Service<Event>>
@Bean
fun bot(config: BotConfig): Bot { fun bot(config: BotConfig): Bot {
return when (config.type) { return when (config.type) {
LoginType.PASSWORD -> BotFactory.newBot(config.id, config.password) { LoginType.PASSWORD -> BotFactory.newBot(config.id, config.password) {
extracted() extracted()
} }
LoginType.QRCODE -> BotFactory.newBot(config.id, BotAuthorization.byQRCode()) { LoginType.QRCODE -> BotFactory.newBot(config.id, BotAuthorization.byQRCode()) {
extracted() extracted()
} }
@ -65,6 +67,11 @@ class BotConfiguration {
return eventMap return eventMap
} }
@PostConstruct
fun init() {
}
private fun BotConfiguration.extracted() { private fun BotConfiguration.extracted() {
fileBasedDeviceInfo() fileBasedDeviceInfo()
protocol = BotConfiguration.MiraiProtocol.ANDROID_WATCH protocol = BotConfiguration.MiraiProtocol.ANDROID_WATCH

View File

@ -16,10 +16,12 @@ class BotConfig(val id: Long, val password: String, val type: LoginType) {
companion object { companion object {
@JvmStatic @JvmStatic
val NULL_INSTANCE = BotConfig(0, "", LoginType.PASSWORD) val NULL_INSTANCE = BotConfig(0, "", LoginType.PASSWORD)
@JvmStatic @JvmStatic
@Value("\${bot.admin}") @Value("\${bot.admin}")
val ADMIN_ID: Long = 0; val ADMIN_ID: Long = 0;
} }
fun isNull(config: BotConfig): Boolean { fun isNull(config: BotConfig): Boolean {
return config == NULL_INSTANCE return config == NULL_INSTANCE
} }

View File

@ -2,22 +2,21 @@ package info.alinadace.miraibot.service
import info.alinadace.miraibot.annotation.BotFunction import info.alinadace.miraibot.annotation.BotFunction
import info.alinadace.miraibot.config.BotConfig import info.alinadace.miraibot.config.BotConfig
import net.mamoe.mirai.event.events.GroupMessageEvent import net.mamoe.mirai.event.events.FriendMessageEvent
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.message.data.PlainText import net.mamoe.mirai.message.data.PlainText
/** /**
* @author Kane * @author Kane
* @since 2024/8/29 下午3:47 * @since 2024/8/29 下午3:47
*/ */
@BotFunction(MessageEvent::class, GroupMessageEvent::class) @BotFunction(FriendMessageEvent::class)
class ExampleService: Service<MessageEvent>{ class ExampleService : Service<FriendMessageEvent> {
/** /**
* 服务入口 * 服务入口
*/ */
override fun entrance(event: MessageEvent): Boolean { override fun entrance(event: FriendMessageEvent): Boolean {
val chain = event.message val chain = event.message
if (chain.size == 1 && chain[0] is PlainText) { if (chain.size == 1 && chain[0] is PlainText) {
if (chain[0].contentToString() == "测试" && event.sender.id == BotConfig.ADMIN_ID) { if (chain[0].contentToString() == "测试" && event.sender.id == BotConfig.ADMIN_ID) {
@ -30,7 +29,7 @@ class ExampleService: Service<MessageEvent>{
/** /**
* 服务行为 * 服务行为
*/ */
override suspend fun active(event: MessageEvent) { override suspend fun active(event: FriendMessageEvent) {
event.subject.sendMessage("测试成功") event.subject.sendMessage("测试成功")
} }
} }