refactor(miraibot): 更新服务和配置,优化bot初始化- 重构BotConfiguration以使用延迟初始化。
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 10s
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:
parent
16fda28d79
commit
a7aa2bda2b
@ -4,6 +4,7 @@ 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.PostConstruct
|
||||
import jakarta.annotation.Resource
|
||||
import net.mamoe.mirai.Bot
|
||||
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.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean
|
||||
import java.util.Collections
|
||||
import java.util.HashMap
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
@ -26,15 +25,18 @@ import kotlin.reflect.KClass
|
||||
@EnableConfigurationProperties(BotConfig::class)
|
||||
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 {
|
||||
return when (config.type) {
|
||||
LoginType.PASSWORD -> BotFactory.newBot(config.id, config.password) {
|
||||
extracted()
|
||||
}
|
||||
|
||||
LoginType.QRCODE -> BotFactory.newBot(config.id, BotAuthorization.byQRCode()) {
|
||||
extracted()
|
||||
}
|
||||
@ -65,6 +67,11 @@ class BotConfiguration {
|
||||
return eventMap
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
fun init() {
|
||||
|
||||
}
|
||||
|
||||
private fun BotConfiguration.extracted() {
|
||||
fileBasedDeviceInfo()
|
||||
protocol = BotConfiguration.MiraiProtocol.ANDROID_WATCH
|
||||
|
@ -16,10 +16,12 @@ class BotConfig(val id: Long, val password: String, val type: LoginType) {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
val NULL_INSTANCE = BotConfig(0, "", LoginType.PASSWORD)
|
||||
|
||||
@JvmStatic
|
||||
@Value("\${bot.admin}")
|
||||
val ADMIN_ID: Long = 0;
|
||||
}
|
||||
|
||||
fun isNull(config: BotConfig): Boolean {
|
||||
return config == NULL_INSTANCE
|
||||
}
|
||||
|
@ -2,22 +2,21 @@ package info.alinadace.miraibot.service
|
||||
|
||||
import info.alinadace.miraibot.annotation.BotFunction
|
||||
import info.alinadace.miraibot.config.BotConfig
|
||||
import net.mamoe.mirai.event.events.GroupMessageEvent
|
||||
import net.mamoe.mirai.event.events.MessageEvent
|
||||
import net.mamoe.mirai.event.events.FriendMessageEvent
|
||||
import net.mamoe.mirai.message.data.PlainText
|
||||
|
||||
/**
|
||||
* @author Kane
|
||||
* @since 2024/8/29 下午3:47
|
||||
*/
|
||||
@BotFunction(MessageEvent::class, GroupMessageEvent::class)
|
||||
class ExampleService: Service<MessageEvent>{
|
||||
@BotFunction(FriendMessageEvent::class)
|
||||
class ExampleService : Service<FriendMessageEvent> {
|
||||
|
||||
|
||||
/**
|
||||
* 服务入口
|
||||
*/
|
||||
override fun entrance(event: MessageEvent): Boolean {
|
||||
override fun entrance(event: FriendMessageEvent): Boolean {
|
||||
val chain = event.message
|
||||
if (chain.size == 1 && chain[0] is PlainText) {
|
||||
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("测试成功")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user