mirror of
https://github.com/jie65535/mirai-console-jnr-plugin.git
synced 2025-06-02 17:49:11 +08:00
优化代码,减少转换,取消正则
This commit is contained in:
parent
3a445f0e79
commit
2664f07cc1
@ -1,13 +1,10 @@
|
|||||||
package top.jie65535.jnr
|
package top.jie65535.jnr
|
||||||
|
|
||||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
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.JvmPluginDescription
|
||||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
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.Group
|
||||||
import net.mamoe.mirai.contact.Member
|
import net.mamoe.mirai.contact.Member
|
||||||
import net.mamoe.mirai.contact.PermissionDeniedException
|
|
||||||
import net.mamoe.mirai.event.EventPriority
|
import net.mamoe.mirai.event.EventPriority
|
||||||
import net.mamoe.mirai.event.events.NudgeEvent
|
import net.mamoe.mirai.event.events.NudgeEvent
|
||||||
import net.mamoe.mirai.event.globalEventChannel
|
import net.mamoe.mirai.event.globalEventChannel
|
||||||
@ -31,12 +28,12 @@ object JNudgeReply : KotlinPlugin(
|
|||||||
Random.nextInt()
|
Random.nextInt()
|
||||||
globalEventChannel().subscribeAlways<NudgeEvent>(priority = JNRPluginConfig.priority) {
|
globalEventChannel().subscribeAlways<NudgeEvent>(priority = JNRPluginConfig.priority) {
|
||||||
if (target.id == bot.id && target.id != from.id && JNRPluginConfig.replyMessageList.isNotEmpty()) {
|
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){
|
if(subject !is Group){
|
||||||
replyList = replyList.filter { !it.message.startsWith("#group") }.toMutableList()
|
replyList = replyList.filter { !it.message.startsWith("#group") }
|
||||||
}else{
|
}else{
|
||||||
if((from as Member).permission.level >= (subject as Group).botPermission.level){
|
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 }
|
val totalWeight = replyList.sumOf { it.weight }
|
||||||
@ -57,23 +54,24 @@ object JNudgeReply : KotlinPlugin(
|
|||||||
logger.info { "Plugin loaded" }
|
logger.info { "Plugin loaded" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalCommandDescriptors::class, ConsoleExperimentalApi::class)
|
private suspend fun doReply(message: ReplyMessage, event: NudgeEvent) {
|
||||||
suspend fun doReply(message: ReplyMessage, event: NudgeEvent) {
|
if (message.message.startsWith("#")) {
|
||||||
val mutePattern = Regex("(?<=#group.mute(\\\\)?:)\\d+")
|
when {
|
||||||
if(message.message.startsWith("#")) {
|
|
||||||
when{
|
|
||||||
message.message == "#nudge" -> {
|
message.message == "#nudge" -> {
|
||||||
event.from.nudge().sendTo(event.subject)
|
event.from.nudge().sendTo(event.subject)
|
||||||
}
|
}
|
||||||
mutePattern.find(message.message) != null -> {
|
message.message.startsWith("#group.mute:") -> {
|
||||||
val duration = mutePattern.find(message.message)?.value?.toLong()!!
|
val duration = message.message.substringAfter(':').toIntOrNull()
|
||||||
val member: Member = event.from as Member
|
if (duration == null) {
|
||||||
try {
|
logger.warning("戳一戳禁言失败:\"${message.message}\" 格式不正确")
|
||||||
member.mute(duration.toInt())
|
} else {
|
||||||
}catch (e: PermissionDeniedException){
|
val member: Member = event.from as Member
|
||||||
logger.warning("权限不足,无法进行禁言")
|
try {
|
||||||
|
member.mute(duration)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
logger.warning("戳一戳禁言失败", e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
event.subject.sendMessage(message.message.deserializeMiraiCode())
|
event.subject.sendMessage(message.message.deserializeMiraiCode())
|
||||||
|
Loading…
Reference in New Issue
Block a user