Sakura-Miki/src/main/kotlin/info/alinadace/sakuramiki/service/mfa/MFAGenerateService.kt
Grand-cocoa 308c688f8c
All checks were successful
Sakura-Miki-build / Automatic-Packaging (push) Successful in 1m11s
fix(mfa): 修复 /mfa 命令识别错误- 在 MFAGenerateService 类中,对 /mfa 命令的识别进行了改进
- 通过在 startsWith 方法前添加 trim(),去除了字符串前后的空白字符
- 这样可以避免因为空白字符导致的命令识别失败,提高命令识别的准确性
2025-03-24 17:33:28 +08:00

79 lines
2.6 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package info.alinadace.sakuramiki.service.mfa
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import info.alinadace.sakuramiki.annotation.BotFunction
import info.alinadace.sakuramiki.service.Service
import info.alinadace.sakuramiki.service.mfa.domain.MFA
import info.alinadace.sakuramiki.service.mfa.mapper.MFAMapper
import info.alinadace.sakuramiki.util.MFAUtil
import io.github.kloping.qqbot.api.v2.FriendMessageEvent
import io.github.kloping.qqbot.entities.ex.PlainText
import jakarta.annotation.Resource
/**
* MFA生成服务
* @author Kane
* @since 2025/1/14 14:00
*/
@BotFunction(FriendMessageEvent::class)
class MFAGenerateService : Service<FriendMessageEvent> {
@Resource
private lateinit var mapper: MFAMapper
/**
* 服务入口
*/
override fun entrance(event: FriendMessageEvent): Boolean {
val message = event.message
if (message.size != 1 && message.size != 2) {
return false
}
if (message[0] is PlainText && message[0].toString().trim().startsWith("/mfa")){
if (message.size == 1){
val split = message[0].toString().split(" ")
return split.size <= 2
}
}
return false
}
/**
* 服务行为
*/
override fun active(event: FriendMessageEvent) {
val message = event.message
if (message[0] is PlainText && message[0].toString().trim().startsWith("/mfa")){
val command = message[0].toString().split(" ")
if (command.size == 1){
val mfa = mapper.selectOne(
KtQueryWrapper(MFA::class.java)
.eq(MFA::uid, event.friend.id)
.eq(MFA::name, "default")
)
if (mfa == null){
event.send("您还没有绑定默认MFA使用 #mfa bind default <secret> 绑定默认MFA")
return
}
event.send(MFAUtil.generate(mfa.secret, 30))
return
}
if (command.size == 2){
val mfa = mapper.selectOne(
KtQueryWrapper(MFA::class.java)
.eq(MFA::uid, event.friend.id)
.eq(MFA::name, command[1])
)
if (mfa == null){
event.send("您还没有绑定${command[1]} MFA使用 #mfa bind ${command[1]} <secret> 绑定${command[1]} MFA")
return
}
event.send(MFAUtil.generate(mfa.secret, 30))
return
}
}
}
}