Compare commits

..

2 Commits

Author SHA1 Message Date
Grand-cocoa
885e6c7780 refactor(miraibot): 更新BotFunction注解和配置类- 为BotFunction注解指定"BotFunction"名称。
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
-直接在BotConfiguration类中注入服务列表。
- 更新BotConfig以存储管理员ID。
- 调整Initialization类以监听应用就绪事件。
- 更新ExampleService以处理MessageEvent。
- 在application.yml中添加服务器端口配置。
- 在application-dev.yml中配置机器人ID。
2024-09-05 11:38:44 +08:00
Grand-cocoa
76d6f849a4 refactor(bot-config): 重构Bot配置并更新初始化
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
- 重构BotConfig类并移动至新的package:info.alinadace.miraibot.configuration。
- 从BotConfiguration.kt中移除未使用的imports和函数。
- 在Initialization.kt中添加PostConstruct注解用于服务初始化。
- 更新ExampleService.kt以使用新的配置package。
- 通过更改名称为'functionMap'并使用GlobalEventChannel优化事件处理。

此重构改进了代码结构,更新了配置的初始化,并增强了事件处理机制。
2024-09-04 20:03:47 +08:00
9 changed files with 114 additions and 58 deletions

12
pom.xml
View File

@ -49,6 +49,10 @@
</properties> </properties>
<dependencies> <dependencies>
<!-- 运行依赖 --> <!-- 运行依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
@ -69,6 +73,14 @@
<groupId>org.jetbrains.kotlin</groupId> <groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId> <artifactId>kotlin-stdlib</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-reactor</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId></groupId>-->
<!-- <artifactId></artifactId>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>com.mysql</groupId> <groupId>com.mysql</groupId>

View File

@ -13,5 +13,5 @@ import kotlin.reflect.KClass
*/ */
@Target(AnnotationTarget.CLASS) @Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME)
@org.springframework.stereotype.Service @org.springframework.stereotype.Service("BotFunction")
annotation class BotFunction(vararg val value: KClass<out Event>) annotation class BotFunction(vararg val value: KClass<out Event>)

View File

@ -1,19 +1,16 @@
package info.alinadace.miraibot.bean package info.alinadace.miraibot.bean
import info.alinadace.miraibot.annotation.BotFunction import info.alinadace.miraibot.annotation.BotFunction
import info.alinadace.miraibot.config.BotConfig import info.alinadace.miraibot.configuration.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 net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.BotFactory import net.mamoe.mirai.BotFactory
import net.mamoe.mirai.auth.BotAuthorization import net.mamoe.mirai.auth.BotAuthorization
import net.mamoe.mirai.event.Event import net.mamoe.mirai.event.Event
import net.mamoe.mirai.utils.BotConfiguration 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 org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import kotlin.reflect.KClass import kotlin.reflect.KClass
/** /**
@ -21,15 +18,9 @@ import kotlin.reflect.KClass
* @author Kane * @author Kane
* @since 2024/8/29 下午2:47 * @since 2024/8/29 下午2:47
*/ */
@AutoConfiguration @Configuration
@EnableConfigurationProperties(BotConfig::class)
class BotConfiguration { class BotConfiguration {
@Resource
private lateinit var services: List<Service<Event>>
@Bean @Bean
fun bot(config: BotConfig): Bot { fun bot(config: BotConfig): Bot {
return when (config.type) { return when (config.type) {
@ -43,9 +34,9 @@ class BotConfiguration {
} }
} }
@Bean @Bean("functionMap")
fun botFunctionMap(): HashMap<KClass<out Event>, MutableList<Service<Event>>> { fun botFunctionMap(services: List<Service<out Event>>): HashMap<KClass<out Event>, MutableList<Service<out Event>>> {
val eventMap = HashMap<KClass<out Event>, MutableList<Service<Event>>>(); val eventMap = HashMap<KClass<out Event>, MutableList<Service<out Event>>>();
for (service in services) { for (service in services) {
val clazz = service::class val clazz = service::class
val annotations = clazz.annotations val annotations = clazz.annotations
@ -67,14 +58,9 @@ 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.MACOS
enableContactCache() enableContactCache()
} }
} }

View File

@ -1,28 +0,0 @@
package info.alinadace.miraibot.config
import info.alinadace.miraibot.enums.LoginType
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.context.properties.ConfigurationProperties
/**
* 机器人配置
* @param id 机器人QQ号
* @param password 机器人密码
* @author Kane
* @since 2024/8/29 下午2:39
*/
@ConfigurationProperties("bot")
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
}
}

View File

@ -0,0 +1,30 @@
package info.alinadace.miraibot.configuration
import info.alinadace.miraibot.enums.LoginType
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
/**
* 机器人配置
* @param id 机器人QQ号
* @param password 机器人密码
* @author Kane
* @since 2024/8/29 下午2:39
*/
@Configuration
@ConfigurationProperties("bot")
class BotConfig {
final var id: Long = 0L
final var password: String = ""
final var type: LoginType = LoginType.PASSWORD
final var admin: Long = 0L
constructor()
constructor(id: Long, password: String, type: LoginType, admin: Long) {
this.id = id
this.password = password
this.type = type
this.admin = admin
}
}

View File

@ -0,0 +1,43 @@
package info.alinadace.miraibot.configuration
import info.alinadace.miraibot.service.Service
import jakarta.annotation.Resource
import net.mamoe.mirai.Bot
import net.mamoe.mirai.event.Event
import net.mamoe.mirai.event.GlobalEventChannel
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.event.EventListener
import kotlin.reflect.KClass
/**
* 机器人功能初始化
* @author Kane
* @since 2024/9/4 18:49
*/
@org.springframework.stereotype.Service
class Initialization {
@Resource(name = "functionMap")
lateinit var functionMap: HashMap<KClass<out Event>, MutableList<Service<Event>>>
@Resource
lateinit var bot: Bot;
val log: Logger = LoggerFactory.getLogger(Initialization::class.java)
@EventListener(ApplicationReadyEvent::class)
suspend fun initFunction() {
functionMap.forEach { v ->
GlobalEventChannel.subscribeAlways(v.key) {
log.info("Event: {}", this)
val filter = v.value.filter { x -> x.entrance(this) }
filter.forEach { x -> x.active(this) }
}
}
log.info("Channel complete")
log.info("Login: id:{}", bot.id)
bot.login()
}
}

View File

@ -1,25 +1,34 @@
package info.alinadace.miraibot.service 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.configuration.BotConfig
import net.mamoe.mirai.event.events.FriendMessageEvent import jakarta.annotation.Resource
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.message.data.PlainText import net.mamoe.mirai.message.data.PlainText
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/** /**
* @author Kane * @author Kane
* @since 2024/8/29 下午3:47 * @since 2024/8/29 下午3:47
*/ */
@BotFunction(FriendMessageEvent::class) @BotFunction(MessageEvent::class)
class ExampleService : Service<FriendMessageEvent> { class ExampleService : Service<MessageEvent> {
val log: Logger = LoggerFactory.getLogger(this::class.java)
@Resource
lateinit var botConfig: BotConfig;
/** /**
* 服务入口 * 服务入口
*/ */
override fun entrance(event: FriendMessageEvent): Boolean { override fun entrance(event: MessageEvent): Boolean {
val chain = event.message val chain = event.message
if (chain.size == 1 && chain[0] is PlainText) { val filter = chain.filter { it !is MessageSource }
if (chain[0].contentToString() == "测试" && event.sender.id == BotConfig.ADMIN_ID) { if (filter.size == 1 && filter[0] is PlainText) {
if (filter[0].contentToString() == "测试" && event.sender.id == botConfig.admin) {
return true return true
} }
} }
@ -29,7 +38,7 @@ class ExampleService : Service<FriendMessageEvent> {
/** /**
* 服务行为 * 服务行为
*/ */
override suspend fun active(event: FriendMessageEvent) { override suspend fun active(event: MessageEvent) {
event.subject.sendMessage("测试成功") event.subject.sendMessage("测试成功")
} }
} }

View File

@ -0,0 +1,2 @@
bot:
id: 3437522130

View File

@ -6,3 +6,5 @@ spring:
bot: bot:
type: qrcode type: qrcode
admin: 1075576561 admin: 1075576561
server:
port: 25684 # 无用随机端口