重构机器人登录配置与服务开启流程

- 移除未使用的登录类型枚举 `LoginType`。- 简化 `BotConfiguration` 中的 `starter` 方法,并移除注释代码。
- 在 `Initialization.kt` 中更新日志信息,并移除全局事件监听器的死代码。
-调整 `SakuramikiApplication.kt`,引入 `MapperScan` 注解以自动扫描Mapper。
- 修正 `application.yml` 中的配置格式问题。
- 在 `application-dev.yml` 和 `application-prod.yml` 中分别设置沙盒模式的配置。

此外,废弃饮用水提醒服务。

注意:部分代码调整涉及异步函数修改为同步,可能需要关注并测试相关功能的完整性。
This commit is contained in:
Grand-cocoa 2024-09-23 18:36:34 +08:00
parent 136ccc1712
commit c141438048
15 changed files with 88 additions and 99 deletions

View File

@ -123,6 +123,10 @@
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -1,9 +1,11 @@
package info.alinadace.sakuramiki
import org.mybatis.spring.annotation.MapperScan
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
@MapperScan("info.alinadace.sakuramiki.service.**.mapper")
class SakuramikiApplication
fun main(args: Array<String>) {

View File

@ -1,7 +1,10 @@
package info.alinadace.sakuramiki.bean
import info.alinadace.sakuramiki.annotation.BotFunction
import info.alinadace.sakuramiki.configuration.BotConfig
import info.alinadace.sakuramiki.service.Service
import io.github.kloping.qqbot.Starter
import io.github.kloping.qqbot.api.Intents
import io.github.kloping.qqbot.api.event.Event
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@ -15,18 +18,14 @@ import kotlin.reflect.KClass
@Configuration
class BotConfiguration {
// @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()
//// }
// }
// }
@Bean
fun starter(config: BotConfig): Starter {
val starter = Starter(config.appId, config.token)
starter.config.code = Intents.PUBLIC_INTENTS.and(Intents.GROUP_INTENTS)
starter.config.isSandbox = config.isSandbox
starter.config.secret = config.appSecret
return starter
}
@Bean("functionMap")
fun botFunctionMap(services: List<Service<out Event>>): HashMap<KClass<out Event>, MutableList<Service<out Event>>> {
@ -51,10 +50,4 @@ class BotConfiguration {
}
return eventMap
}
// private fun BotConfiguration.extracted() {
// fileBasedDeviceInfo()
// protocol = BotConfiguration.MiraiProtocol.ANDROID_PAD
// enableContactCache()
// }
}

View File

@ -1,6 +1,5 @@
package info.alinadace.sakuramiki.configuration
import info.alinadace.sakuramiki.enums.LoginType
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@ -17,20 +16,10 @@ class BotConfig {
*/
final var id: Long = 0L
/**
* 机器人密码
*/
final var password: String = ""
/**
* 机器人登录方式
*/
final var type: LoginType = LoginType.PASSWORD
/**
* 管理员QQ
*/
final var admin: Long = 0L
final var admin: String = ""
/**
* 登录凭证
@ -47,25 +36,28 @@ class BotConfig {
*/
final var appSecret: String = ""
/**
* 是否沙盒模式
*/
final var isSandbox: Boolean = false
constructor()
constructor(
id: Long,
password: String,
type: LoginType,
admin: Long,
admin: String,
appId: String,
token: String,
appSecret: String
appSecret: String,
isSandbox: Boolean
) {
this.id = id
this.password = password
this.type = type
this.admin = admin
this.appId = appId
this.token = token
this.appSecret = appSecret
this.isSandbox = isSandbox
}
}

View File

@ -1,8 +1,9 @@
package info.alinadace.sakuramiki.configuration
import info.alinadace.sakuramiki.service.Service
import io.github.kloping.qqbot.Starter
import io.github.kloping.qqbot.api.event.Event
import io.github.kloping.qqbot.entities.Bot
import io.github.kloping.qqbot.impl.ListenerHost
import jakarta.annotation.Resource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
@ -22,26 +23,38 @@ class Initialization {
lateinit var functionMap: HashMap<KClass<out Event>, MutableList<Service<Event>>>
@Resource
lateinit var bot: Bot;
lateinit var starter: Starter;
val log: Logger = LoggerFactory.getLogger(this::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) }
// }
}
val eventListener = object : ListenerHost() {
override fun handleException(e: Throwable?): Boolean {
log.error("Exception: {}", e?.message, e)
return super.handleException(e)
}
// GlobalEventChannel.subscribeAlways(Event::class) {
// log.info("GlobalEvent: {}", this)
// }
@EventReceiver
fun onEvent(event: Event) {
log.info("Event: {}", event)
val filter = functionMap.keys.filter { x -> x.isInstance(event) }
filter.forEach { x ->
if (x.isInstance(event)) {
functionMap[x]?.forEach { y ->
if (y.entrance(event)) {
y.active(event)
}
}
}
}
log.info("Event-End: {}", event)
}
}
starter.registerListenerHost(eventListener)
log.info("Channel complete")
log.info("Login: id:{}", bot.id)
// bot.login()
log.info("Login: id:{}", starter.config.appid)
starter.run()
}
}

View File

@ -1,18 +0,0 @@
package info.alinadace.sakuramiki.enums
/**
* 登录方式
* @author Kane
* @since 2024/8/29 下午3:19
*/
enum class LoginType {
/**
* 密码登录
*/
PASSWORD,
/**
* 扫码登录
*/
QRCODE
}

View File

@ -2,8 +2,10 @@ package info.alinadace.sakuramiki.service
import info.alinadace.sakuramiki.annotation.BotFunction
import info.alinadace.sakuramiki.configuration.BotConfig
import io.github.kloping.qqbot.api.message.MessageEvent
import io.github.kloping.qqbot.entities.qqpd.v2.Friend
import io.github.kloping.qqbot.api.v2.FriendMessageEvent
import io.github.kloping.qqbot.api.v2.GroupMessageEvent
import io.github.kloping.qqbot.api.v2.MessageV2Event
import io.github.kloping.qqbot.entities.ex.PlainText
import jakarta.annotation.Resource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
@ -12,8 +14,8 @@ import org.slf4j.LoggerFactory
* @author Kane
* @since 2024/8/29 下午3:47
*/
@BotFunction(MessageEvent::class)
class ExampleService : Service<MessageEvent<Friend, Friend>> {
@BotFunction(FriendMessageEvent::class, GroupMessageEvent::class)
class ExampleService : Service<MessageV2Event> {
val log: Logger = LoggerFactory.getLogger(this::class.java)
@ -23,21 +25,20 @@ class ExampleService : Service<MessageEvent<Friend, Friend>> {
/**
* 服务入口
*/
override fun entrance(event: MessageEvent<Friend, Friend>): Boolean {
// val chain = event.message
// val filter = chain.filter { it !is MessageSource }
// if (filter.size == 1 && filter[0] is PlainText) {
// if (filter[0].contentToString() == "测试" && event.sender.id == botConfig.admin) {
// return true
// }
// }
return false
override fun entrance(event: MessageV2Event): Boolean {
val chain = event.message
if (chain.size == 1 && chain[0] is PlainText && event.sender.id == botConfig.admin) {
if (chain[0].toString() == "测试") {
return true
}
}
return true
}
/**
* 服务行为
*/
override suspend fun active(event: MessageEvent<Friend, Friend>) {
// event.subject.sendMessage("测试成功")
override fun active(event: MessageV2Event) {
event.send("测试成功")
}
}

View File

@ -16,5 +16,5 @@ interface Service<E : Event> {
/**
* 服务行为
*/
suspend fun active(event: E)
fun active(event: E)
}

View File

@ -1,7 +1,6 @@
package info.alinadace.sakuramiki.service.drink_water
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import info.alinadace.sakuramiki.annotation.BotFunction
import info.alinadace.sakuramiki.service.Service
import info.alinadace.sakuramiki.service.drink_water.domain.DrinkWaterUser
import info.alinadace.sakuramiki.service.drink_water.mapper.DrinkWaterMapper
@ -15,7 +14,8 @@ import org.slf4j.LoggerFactory
* @author Kane
* @since 2024/9/18 13:27
*/
@BotFunction(FriendMessageEvent::class)
//@BotFunction(FriendMessageEvent::class)
@Deprecated("限制无法实现")
class DisableService : Service<FriendMessageEvent> {
companion object {
@ -43,7 +43,7 @@ class DisableService : Service<FriendMessageEvent> {
/**
* 服务行为
*/
override suspend fun active(event: FriendMessageEvent) {
override fun active(event: FriendMessageEvent) {
log.info("服务关闭请求 - DrinkWater - target:{}", event.sender.id)
val one = drinkWaterMapper.selectOne(
KtQueryWrapper(DrinkWaterUser())

View File

@ -1,7 +1,6 @@
package info.alinadace.sakuramiki.service.drink_water
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import info.alinadace.sakuramiki.annotation.BotFunction
import info.alinadace.sakuramiki.service.Service
import info.alinadace.sakuramiki.service.drink_water.domain.DrinkWaterUser
import info.alinadace.sakuramiki.service.drink_water.mapper.DrinkWaterMapper
@ -15,7 +14,8 @@ import org.slf4j.LoggerFactory
* @author Kane
* @since 2024/9/18 13:27
*/
@BotFunction(FriendMessageEvent::class)
//@BotFunction(FriendMessageEvent::class)
@Deprecated("限制无法实现")
class EnableService : Service<FriendMessageEvent> {
companion object {
@ -42,7 +42,7 @@ class EnableService : Service<FriendMessageEvent> {
/**
* 服务行为
*/
override suspend fun active(event: FriendMessageEvent) {
override fun active(event: FriendMessageEvent) {
log.info("服务开启请求 - DrinkWater - target:{}", event.sender.id)
val one = drinkWaterMapper.selectOne(
KtQueryWrapper(DrinkWaterUser())

View File

@ -8,5 +8,4 @@ import info.alinadace.sakuramiki.service.drink_water.domain.DrinkWaterUser
* @author Kane
* @since 2024/9/18 13:39
*/
interface DrinkWaterMapper : BaseMapper<DrinkWaterUser> {
}
interface DrinkWaterMapper : BaseMapper<DrinkWaterUser>

View File

@ -3,32 +3,30 @@ package info.alinadace.sakuramiki.service.drink_water.task
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import info.alinadace.sakuramiki.service.drink_water.domain.DrinkWaterUser
import info.alinadace.sakuramiki.service.drink_water.mapper.DrinkWaterMapper
import io.github.kloping.qqbot.entities.Bot
import jakarta.annotation.Resource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
/**
* 定时喝水提醒服务
* @author Kane
* @since 2024/9/18 13:59
*/
@Service
//@Service
@Deprecated("限制无法实现")
class ReminderService {
companion object {
val log: Logger = LoggerFactory.getLogger(this::class.java)
}
@Resource
lateinit var bot: Bot
// @Resource
// lateinit var bot: Bot
@Resource
lateinit var drinkWaterMapper: DrinkWaterMapper
@Scheduled(cron = "0 0 0,8-23/2 * * *")
// @Scheduled(cron = "0 0 0,8-23/2 * * *")
suspend fun reminder() {
log.info("定时喝水提醒服务 - 开始")
val list = drinkWaterMapper.selectList(

View File

@ -11,3 +11,5 @@ spring:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
bot:
is-sandbox: true

View File

@ -8,3 +8,5 @@ spring:
url: jdbc:mysql://10.0.4.16:3306/mirai?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: mirai
password: FydsaZsFm5w26We6
bot:
is-sandbox: false

View File

@ -11,3 +11,4 @@ bot:
app-id: 102372439
token: sq3CZjEpK7Z65s2xuyhwp8WVBqvx35XP
app-secret: Wgq0BMXit5HTfr3GTgt6JXlzDRgvAPet
admin: 59EC2526FBCC1EC5F851187C0F4F8BA5