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
@ -1,6 +1,6 @@
|
|||||||
name: Gitea Actions Demo
|
name: Gitea Actions Demo
|
||||||
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
|
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
|
||||||
on: [push]
|
on: [ push ]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
Explore-Gitea-Actions:
|
Explore-Gitea-Actions:
|
||||||
|
@ -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,23 +25,26 @@ 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
|
||||||
fun bot(config: BotConfig): Bot{
|
private lateinit var services: List<Service<Event>>
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
fun botFunctionMap(): HashMap<KClass<out Event>, MutableList<Service<Event>>>{
|
fun botFunctionMap(): HashMap<KClass<out Event>, MutableList<Service<Event>>> {
|
||||||
val eventMap = HashMap<KClass<out Event>, MutableList<Service<Event>>>();
|
val eventMap = HashMap<KClass<out Event>, MutableList<Service<Event>>>();
|
||||||
for (service in services) {
|
for (service in services) {
|
||||||
val clazz = service::class
|
val clazz = service::class
|
||||||
@ -50,11 +52,11 @@ class BotConfiguration {
|
|||||||
if (annotations.isEmpty()) continue
|
if (annotations.isEmpty()) continue
|
||||||
var function: BotFunction? = null
|
var function: BotFunction? = null
|
||||||
for (annotation in annotations) {
|
for (annotation in annotations) {
|
||||||
if (annotation is BotFunction){
|
if (annotation is BotFunction) {
|
||||||
function = annotation
|
function = annotation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (function == null){
|
if (function == null) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for (kClass in function.value) {
|
for (kClass in function.value) {
|
||||||
@ -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
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -2,24 +2,23 @@ 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) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -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("测试成功")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import net.mamoe.mirai.event.Event
|
|||||||
* @author Kane
|
* @author Kane
|
||||||
* @since 2024/8/29 下午3:49
|
* @since 2024/8/29 下午3:49
|
||||||
*/
|
*/
|
||||||
interface Service<E: Event> {
|
interface Service<E : Event> {
|
||||||
/**
|
/**
|
||||||
* 服务入口
|
* 服务入口
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user