Compare commits

..

No commits in common. "885e6c778056c37d89d85bc1a2a3853be79953e2" and "a7aa2bda2be8df87d5cadee44f10d33a4a6acf47" have entirely different histories.

9 changed files with 58 additions and 114 deletions

12
pom.xml
View File

@ -49,10 +49,6 @@
</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>
@ -73,14 +69,6 @@
<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("BotFunction") @org.springframework.stereotype.Service
annotation class BotFunction(vararg val value: KClass<out Event>) annotation class BotFunction(vararg val value: KClass<out Event>)

View File

@ -1,16 +1,19 @@
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.configuration.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 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
/** /**
@ -18,9 +21,15 @@ import kotlin.reflect.KClass
* @author Kane * @author Kane
* @since 2024/8/29 下午2:47 * @since 2024/8/29 下午2:47
*/ */
@Configuration @AutoConfiguration
@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) {
@ -34,9 +43,9 @@ class BotConfiguration {
} }
} }
@Bean("functionMap") @Bean
fun botFunctionMap(services: List<Service<out Event>>): HashMap<KClass<out Event>, MutableList<Service<out Event>>> { fun botFunctionMap(): HashMap<KClass<out Event>, MutableList<Service<Event>>> {
val eventMap = HashMap<KClass<out Event>, MutableList<Service<out 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
val annotations = clazz.annotations val annotations = clazz.annotations
@ -58,9 +67,14 @@ class BotConfiguration {
return eventMap return eventMap
} }
@PostConstruct
fun init() {
}
private fun BotConfiguration.extracted() { private fun BotConfiguration.extracted() {
fileBasedDeviceInfo() fileBasedDeviceInfo()
protocol = BotConfiguration.MiraiProtocol.MACOS protocol = BotConfiguration.MiraiProtocol.ANDROID_WATCH
enableContactCache() enableContactCache()
} }
} }

View File

@ -0,0 +1,28 @@
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

@ -1,30 +0,0 @@
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

@ -1,43 +0,0 @@
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,34 +1,25 @@
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.configuration.BotConfig import info.alinadace.miraibot.config.BotConfig
import jakarta.annotation.Resource import net.mamoe.mirai.event.events.FriendMessageEvent
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(MessageEvent::class) @BotFunction(FriendMessageEvent::class)
class ExampleService : Service<MessageEvent> { class ExampleService : Service<FriendMessageEvent> {
val log: Logger = LoggerFactory.getLogger(this::class.java)
@Resource
lateinit var botConfig: BotConfig;
/** /**
* 服务入口 * 服务入口
*/ */
override fun entrance(event: MessageEvent): Boolean { override fun entrance(event: FriendMessageEvent): Boolean {
val chain = event.message val chain = event.message
val filter = chain.filter { it !is MessageSource } if (chain.size == 1 && chain[0] is PlainText) {
if (filter.size == 1 && filter[0] is PlainText) { if (chain[0].contentToString() == "测试" && event.sender.id == BotConfig.ADMIN_ID) {
if (filter[0].contentToString() == "测试" && event.sender.id == botConfig.admin) {
return true return true
} }
} }
@ -38,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("测试成功")
} }
} }

View File

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

View File

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