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。
This commit is contained in:
Grand-cocoa 2024-09-05 11:38:44 +08:00
parent 76d6f849a4
commit 885e6c7780
8 changed files with 69 additions and 37 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

@ -4,15 +4,13 @@ import info.alinadace.miraibot.annotation.BotFunction
import info.alinadace.miraibot.configuration.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.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
/** /**
@ -20,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,8 +35,8 @@ class BotConfiguration {
} }
@Bean("functionMap") @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
@ -68,7 +60,7 @@ class BotConfiguration {
private fun BotConfiguration.extracted() { private fun BotConfiguration.extracted() {
fileBasedDeviceInfo() fileBasedDeviceInfo()
protocol = BotConfiguration.MiraiProtocol.ANDROID_WATCH protocol = BotConfiguration.MiraiProtocol.MACOS
enableContactCache() enableContactCache()
} }
} }

View File

@ -1,8 +1,8 @@
package info.alinadace.miraibot.configuration package info.alinadace.miraibot.configuration
import info.alinadace.miraibot.enums.LoginType import info.alinadace.miraibot.enums.LoginType
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
/** /**
* 机器人配置 * 机器人配置
@ -11,18 +11,20 @@ import org.springframework.boot.context.properties.ConfigurationProperties
* @author Kane * @author Kane
* @since 2024/8/29 下午2:39 * @since 2024/8/29 下午2:39
*/ */
@Configuration
@ConfigurationProperties("bot") @ConfigurationProperties("bot")
class BotConfig(val id: Long, val password: String, val type: LoginType) { class BotConfig {
companion object { final var id: Long = 0L
@JvmStatic final var password: String = ""
val NULL_INSTANCE = BotConfig(0, "", LoginType.PASSWORD) final var type: LoginType = LoginType.PASSWORD
final var admin: Long = 0L
@JvmStatic constructor()
@Value("\${bot.admin}")
val ADMIN_ID: Long = 0;
}
fun isNull(config: BotConfig): Boolean { constructor(id: Long, password: String, type: LoginType, admin: Long) {
return config == NULL_INSTANCE this.id = id
this.password = password
this.type = type
this.admin = admin
} }
} }

View File

@ -1,10 +1,14 @@
package info.alinadace.miraibot.configuration package info.alinadace.miraibot.configuration
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.event.Event import net.mamoe.mirai.event.Event
import net.mamoe.mirai.event.GlobalEventChannel 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 import kotlin.reflect.KClass
/** /**
@ -15,16 +19,25 @@ import kotlin.reflect.KClass
@org.springframework.stereotype.Service @org.springframework.stereotype.Service
class Initialization { class Initialization {
@Resource(name = "functionMap") @Resource(name = "functionMap")
lateinit var functionMap: HashMap<KClass<out Event>, MutableList<Service<Event>>>; lateinit var functionMap: HashMap<KClass<out Event>, MutableList<Service<Event>>>
@PostConstruct @Resource
fun initFunction() { lateinit var bot: Bot;
val log: Logger = LoggerFactory.getLogger(Initialization::class.java)
@EventListener(ApplicationReadyEvent::class)
suspend fun initFunction() {
functionMap.forEach { v -> functionMap.forEach { v ->
GlobalEventChannel.subscribeAlways(v.key) { GlobalEventChannel.subscribeAlways(v.key) {
log.info("Event: {}", this)
val filter = v.value.filter { x -> x.entrance(this) } val filter = v.value.filter { x -> x.entrance(this) }
filter.forEach { x -> x.active(this) } filter.forEach { x -> x.active(this) }
} }
} }
print("Channel complete")
log.info("Channel complete")
log.info("Login: id:{}", bot.id)
bot.login()
} }
} }

View File

@ -2,24 +2,33 @@ package info.alinadace.miraibot.service
import info.alinadace.miraibot.annotation.BotFunction import info.alinadace.miraibot.annotation.BotFunction
import info.alinadace.miraibot.configuration.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 # 无用随机端口