From 551a792c0403a2ba65e9ef140f2af966b445d46c Mon Sep 17 00:00:00 2001 From: YehowahLiu Date: Fri, 15 Apr 2022 16:32:17 +0800 Subject: [PATCH 1/7] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E5=90=AF=E5=8A=A8=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/RunMirai.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/kotlin/RunMirai.kt diff --git a/src/test/kotlin/RunMirai.kt b/src/test/kotlin/RunMirai.kt new file mode 100644 index 0000000..b5386be --- /dev/null +++ b/src/test/kotlin/RunMirai.kt @@ -0,0 +1,16 @@ +import net.mamoe.mirai.console.MiraiConsole +import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.enable +import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.load +import net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader +import net.mamoe.mirai.console.util.ConsoleExperimentalApi +import top.jie65535.jnr.JNudgeReply + +@OptIn(ConsoleExperimentalApi::class) +suspend fun main(){ + val s = "#group.mute:10" + println(s.matches(Regex("#group.mute:\\d+"))) + MiraiConsoleTerminalLoader.startAsDaemon() + JNudgeReply.load() + JNudgeReply.enable() + MiraiConsole.job.join() +} From 18adb49cdd406e513610adef44fb814edac26973 Mon Sep 17 00:00:00 2001 From: YehowahLiu Date: Fri, 15 Apr 2022 16:34:44 +0800 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=89=B9?= =?UTF-8?q?=E6=AE=8A=E5=93=8D=E5=BA=94=E5=8A=A8=E4=BD=9C=20=E5=8C=85?= =?UTF-8?q?=E6=8B=AC=E5=9B=9E=E6=88=B3=20/=20=E7=A6=81=E8=A8=80=E7=94=A8?= =?UTF-8?q?=E6=88=B7=20=E5=9C=A8=E6=8A=BD=E5=8F=96=E5=9B=9E=E5=A4=8D?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E6=97=B6=E4=BC=9A=E6=8C=89=E9=9C=80=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E6=8E=89=E4=BB=85=E7=BE=A4=E7=BB=84=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E5=8A=A8=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/top/jie65535/jnr/JNudgeReply.kt | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt index c7de7fc..24e63a5 100644 --- a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt +++ b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt @@ -1,8 +1,13 @@ 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 @@ -26,11 +31,17 @@ object JNudgeReply : KotlinPlugin( Random.nextInt() globalEventChannel().subscribeAlways(priority = JNRPluginConfig.priority) { if (target.id == bot.id && JNRPluginConfig.replyMessageList.isNotEmpty()) { - val totalWeight = JNRPluginConfig.replyMessageList.sumOf { it.weight } + val replyList = if(subject is Group){ + JNRPluginConfig.replyMessageList + }else{ + // 非群聊时过滤部分特殊指令 + JNRPluginConfig.replyMessageList.filter { !it.message.startsWith("#group") } + } + val totalWeight = replyList.sumOf { it.weight } var w = Random.nextInt(totalWeight) - for (msg in JNRPluginConfig.replyMessageList) { + for (msg in replyList) { if (w < msg.weight) { - subject.sendMessage(msg.message.deserializeMiraiCode()) + doReply(msg, this) break } else { w -= msg.weight @@ -43,4 +54,30 @@ object JNudgeReply : KotlinPlugin( logger.info { "Plugin loaded" } } + + @OptIn(ExperimentalCommandDescriptors::class, ConsoleExperimentalApi::class) + suspend fun doReply(message: ReplyMessage, event: NudgeEvent) { + if(message.message.startsWith("#")) { + when{ + message.message == "#nudge" -> { + event.from.nudge().sendTo(event.subject) + } + message.message.matches(Regex("#group\\.mute(\\\\)?:\\d+")) -> { + val (_, duration) = message.message.split(":") + val member: Member = event.from as Member + try { + member.mute(duration.toInt()) + }catch (e: PermissionDeniedException){ + logger.warning("权限不足,无法进行禁言") + } + + } + else -> { + event.subject.sendMessage(message.message.deserializeMiraiCode()) + } + } + } else { + event.subject.sendMessage(message.message.deserializeMiraiCode()) + } + } } From 6094637f03a0fdd864449f1da5b7052b7c713da8 Mon Sep 17 00:00:00 2001 From: YehowahLiu Date: Fri, 15 Apr 2022 16:44:20 +0800 Subject: [PATCH 3/7] =?UTF-8?q?fix:=20=E9=81=BF=E5=85=8D=E5=93=8D=E5=BA=94?= =?UTF-8?q?bot=E5=B8=90=E5=8F=B7=E7=9A=84=E6=88=B3=E4=B8=80=E6=88=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt index 24e63a5..5d8aece 100644 --- a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt +++ b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt @@ -30,7 +30,7 @@ object JNudgeReply : KotlinPlugin( JNRCommand.register() Random.nextInt() globalEventChannel().subscribeAlways(priority = JNRPluginConfig.priority) { - if (target.id == bot.id && JNRPluginConfig.replyMessageList.isNotEmpty()) { + if (target.id == bot.id && target.id != from.id && JNRPluginConfig.replyMessageList.isNotEmpty()) { val replyList = if(subject is Group){ JNRPluginConfig.replyMessageList }else{ From b20a92cc9ca233c1f96e128006ba7bda049b8e98 Mon Sep 17 00:00:00 2001 From: YehowahLiu Date: Fri, 15 Apr 2022 17:04:46 +0800 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20=E6=9D=83=E9=99=90=E5=88=A4=E6=96=AD?= =?UTF-8?q?=20=E5=9C=A8=E5=8F=91=E9=80=81=E4=BA=BA=E6=9D=83=E9=99=90?= =?UTF-8?q?=E4=B8=8D=E4=BD=8E=E4=BA=8Ebot=E6=97=B6,=20=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E6=8E=89=E7=A6=81=E8=A8=80=E7=AD=89=E7=89=B9=E6=9D=83=E5=8A=A8?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt index 5d8aece..d103b8c 100644 --- a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt +++ b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt @@ -31,11 +31,13 @@ object JNudgeReply : KotlinPlugin( Random.nextInt() globalEventChannel().subscribeAlways(priority = JNRPluginConfig.priority) { if (target.id == bot.id && target.id != from.id && JNRPluginConfig.replyMessageList.isNotEmpty()) { - val replyList = if(subject is Group){ - JNRPluginConfig.replyMessageList + var replyList = JNRPluginConfig.replyMessageList + if(subject !is Group){ + replyList = replyList.filter { !it.message.startsWith("#group") }.toMutableList() }else{ - // 非群聊时过滤部分特殊指令 - JNRPluginConfig.replyMessageList.filter { !it.message.startsWith("#group") } + if((from as Member).permission.level >= (subject as Group).botPermission.level){ + replyList = replyList.filter { !it.message.startsWith("#group.mute") }.toMutableList() + } } val totalWeight = replyList.sumOf { it.weight } var w = Random.nextInt(totalWeight) From a531cab16caad9e03e22d38c34b3658c63025d84 Mon Sep 17 00:00:00 2001 From: YehowahLiu Date: Fri, 15 Apr 2022 17:27:33 +0800 Subject: [PATCH 5/7] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E5=8A=A8=E4=BD=9C=E7=9A=84=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 0cdcd4d..e389476 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ # mirai-console-jnr-plugin MiraiConsolePlugin 自定义戳一戳回复消息 +其中, 下列消息具有特殊含义: + +`#nudge` 戳回去 + +`#group.mute:30` 禁言30s, 可以自定义禁言时间, 单位秒 + ![Use example image](doc/example.png) From b3ad996e68d7cd1be43319967516f0da27db034a Mon Sep 17 00:00:00 2001 From: YehowahLiu Date: Sun, 17 Apr 2022 02:53:48 +0800 Subject: [PATCH 6/7] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E6=AD=A3?= =?UTF-8?q?=E5=88=99=E6=8F=90=E5=8F=96=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt | 5 +++-- src/test/kotlin/RegexTest.kt | 4 ++++ src/test/kotlin/RunMirai.kt | 2 -- 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 src/test/kotlin/RegexTest.kt diff --git a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt index d103b8c..3bd643e 100644 --- a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt +++ b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt @@ -59,13 +59,14 @@ object JNudgeReply : KotlinPlugin( @OptIn(ExperimentalCommandDescriptors::class, ConsoleExperimentalApi::class) suspend fun doReply(message: ReplyMessage, event: NudgeEvent) { + val mutePattern = Regex("(?<=#group.mute(\\\\)?:)\\d+") if(message.message.startsWith("#")) { when{ message.message == "#nudge" -> { event.from.nudge().sendTo(event.subject) } - message.message.matches(Regex("#group\\.mute(\\\\)?:\\d+")) -> { - val (_, duration) = message.message.split(":") + message.message.matches(mutePattern) -> { + val duration = mutePattern.find(message.message)?.value?.toLong()!! val member: Member = event.from as Member try { member.mute(duration.toInt()) diff --git a/src/test/kotlin/RegexTest.kt b/src/test/kotlin/RegexTest.kt new file mode 100644 index 0000000..fa84a59 --- /dev/null +++ b/src/test/kotlin/RegexTest.kt @@ -0,0 +1,4 @@ +fun main(){ + val regex = Regex("(?<=#group\\.mute(\\\\)?:)\\d+") + println(regex.find("#group.mute:12345")?.value?.toLong()) +} \ No newline at end of file diff --git a/src/test/kotlin/RunMirai.kt b/src/test/kotlin/RunMirai.kt index b5386be..5519c94 100644 --- a/src/test/kotlin/RunMirai.kt +++ b/src/test/kotlin/RunMirai.kt @@ -7,8 +7,6 @@ import top.jie65535.jnr.JNudgeReply @OptIn(ConsoleExperimentalApi::class) suspend fun main(){ - val s = "#group.mute:10" - println(s.matches(Regex("#group.mute:\\d+"))) MiraiConsoleTerminalLoader.startAsDaemon() JNudgeReply.load() JNudgeReply.enable() From e6882a1c7a5ac451720eec0d2ee26d029ff01fdb Mon Sep 17 00:00:00 2001 From: YehowahLiu Date: Sun, 17 Apr 2022 03:12:42 +0800 Subject: [PATCH 7/7] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=A6=81=E8=A8=80?= =?UTF-8?q?=E5=8A=A8=E4=BD=9C=E7=9A=84=E6=AD=A3=E5=88=99=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt | 2 +- src/test/kotlin/RegexTest.kt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt index 3bd643e..51c88d8 100644 --- a/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt +++ b/src/main/kotlin/top/jie65535/jnr/JNudgeReply.kt @@ -65,7 +65,7 @@ object JNudgeReply : KotlinPlugin( message.message == "#nudge" -> { event.from.nudge().sendTo(event.subject) } - message.message.matches(mutePattern) -> { + mutePattern.find(message.message) != null -> { val duration = mutePattern.find(message.message)?.value?.toLong()!! val member: Member = event.from as Member try { diff --git a/src/test/kotlin/RegexTest.kt b/src/test/kotlin/RegexTest.kt index fa84a59..7a84c23 100644 --- a/src/test/kotlin/RegexTest.kt +++ b/src/test/kotlin/RegexTest.kt @@ -1,4 +1,5 @@ fun main(){ val regex = Regex("(?<=#group\\.mute(\\\\)?:)\\d+") + println(regex.find("#group.mute:abc")?.value?.toLong()) println(regex.find("#group.mute:12345")?.value?.toLong()) } \ No newline at end of file