优化代码,减少转换,取消正则

This commit is contained in:
2022-04-17 09:24:11 +08:00
parent 3a445f0e79
commit 2664f07cc1

View File

@ -1,13 +1,10 @@
package top.jie65535.jnr
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.PermissionDeniedException
import net.mamoe.mirai.event.EventPriority
import net.mamoe.mirai.event.events.NudgeEvent
import net.mamoe.mirai.event.globalEventChannel
@ -31,12 +28,12 @@ object JNudgeReply : KotlinPlugin(
Random.nextInt()
globalEventChannel().subscribeAlways<NudgeEvent>(priority = JNRPluginConfig.priority) {
if (target.id == bot.id && target.id != from.id && JNRPluginConfig.replyMessageList.isNotEmpty()) {
var replyList = JNRPluginConfig.replyMessageList
var replyList: List<ReplyMessage> = JNRPluginConfig.replyMessageList
if(subject !is Group){
replyList = replyList.filter { !it.message.startsWith("#group") }.toMutableList()
replyList = replyList.filter { !it.message.startsWith("#group") }
}else{
if((from as Member).permission.level >= (subject as Group).botPermission.level){
replyList = replyList.filter { !it.message.startsWith("#group.mute") }.toMutableList()
replyList = replyList.filter { !it.message.startsWith("#group.mute:") }
}
}
val totalWeight = replyList.sumOf { it.weight }
@ -57,23 +54,24 @@ object JNudgeReply : KotlinPlugin(
logger.info { "Plugin loaded" }
}
@OptIn(ExperimentalCommandDescriptors::class, ConsoleExperimentalApi::class)
suspend fun doReply(message: ReplyMessage, event: NudgeEvent) {
val mutePattern = Regex("(?<=#group.mute(\\\\)?:)\\d+")
if(message.message.startsWith("#")) {
when{
private suspend fun doReply(message: ReplyMessage, event: NudgeEvent) {
if (message.message.startsWith("#")) {
when {
message.message == "#nudge" -> {
event.from.nudge().sendTo(event.subject)
}
mutePattern.find(message.message) != null -> {
val duration = mutePattern.find(message.message)?.value?.toLong()!!
message.message.startsWith("#group.mute:") -> {
val duration = message.message.substringAfter(':').toIntOrNull()
if (duration == null) {
logger.warning("戳一戳禁言失败:\"${message.message}\" 格式不正确")
} else {
val member: Member = event.from as Member
try {
member.mute(duration.toInt())
}catch (e: PermissionDeniedException){
logger.warning("权限不足,无法进行禁言")
member.mute(duration)
} catch (e: Throwable) {
logger.warning("戳一戳禁言失败", e)
}
}
}
else -> {
event.subject.sendMessage(message.message.deserializeMiraiCode())