mirror of
https://github.com/jie65535/mirai-console-jnr-plugin.git
synced 2025-06-02 17:49:11 +08:00
更新版本到v1.0.0
更新项目依赖 修改包名 功能更新: - 现在戳一戳后会从随机列表中根据权重随机抽取一条消息进行回复 #4 - 现在允许添加多条回复消息,并允许设置消息权重 - 现在允许删除指定索引的回复消息 - 现在允许请空回复消息列表 - 现在允许查看回复消息列表
This commit is contained in:
parent
3feff8d40e
commit
f4485d9017
@ -1,15 +1,16 @@
|
||||
plugins {
|
||||
val kotlinVersion = "1.5.10"
|
||||
val kotlinVersion = "1.6.0"
|
||||
kotlin("jvm") version kotlinVersion
|
||||
kotlin("plugin.serialization") version kotlinVersion
|
||||
|
||||
id("net.mamoe.mirai-console") version "2.7.1"
|
||||
id("net.mamoe.mirai-console") version "2.10.1"
|
||||
}
|
||||
|
||||
group = "me.jie65535"
|
||||
version = "0.1.1"
|
||||
group = "top.jie65535"
|
||||
version = "1.0.0"
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
maven("https://maven.aliyun.com/repository/public")
|
||||
mavenCentral()
|
||||
}
|
||||
|
@ -1,53 +0,0 @@
|
||||
package me.jie65535.jnr
|
||||
|
||||
import kotlinx.coroutines.TimeoutCancellationException
|
||||
import net.mamoe.mirai.console.command.CommandSender
|
||||
import net.mamoe.mirai.console.command.RawCommand
|
||||
import net.mamoe.mirai.console.command.isUser
|
||||
import net.mamoe.mirai.event.events.MessageEvent
|
||||
import net.mamoe.mirai.event.nextEventAsync
|
||||
import net.mamoe.mirai.message.data.MessageChain
|
||||
import net.mamoe.mirai.message.data.isContentBlank
|
||||
import net.mamoe.mirai.utils.MiraiExperimentalApi
|
||||
|
||||
object JNRCommand : RawCommand(
|
||||
JNudgeReply, "jnr", "setPokeReply", "setNudgeReply",
|
||||
usage = "/jnr|setPokeReply|setNudgeReply <message> # 设置戳一戳回复消息",
|
||||
description = "设置戳一戳回复消息"
|
||||
) {
|
||||
private const val WAIT_REPLY_TIMEOUT_MS = 30000L
|
||||
|
||||
@OptIn(MiraiExperimentalApi::class)
|
||||
override suspend fun CommandSender.onCommand(args: MessageChain) {
|
||||
if (args.isContentBlank()) {
|
||||
if (this.isUser()) {
|
||||
try {
|
||||
sendMessage("请在${WAIT_REPLY_TIMEOUT_MS / 1000}秒内发送要回复的消息内容,你可以发送空白消息来清空预设回复。")
|
||||
val msg = subject.nextEventAsync<MessageEvent>(
|
||||
WAIT_REPLY_TIMEOUT_MS,
|
||||
coroutineContext = this.coroutineContext
|
||||
) { it.sender == user } .await()
|
||||
if (msg.message.isContentBlank()) {
|
||||
setReplyMessage(null)
|
||||
} else {
|
||||
setReplyMessage(msg.message)
|
||||
}
|
||||
sendMessage("OK")
|
||||
} catch (e: TimeoutCancellationException) {
|
||||
sendMessage("已取消")
|
||||
}
|
||||
} else {
|
||||
setReplyMessage(null)
|
||||
sendMessage("已清空预设回复")
|
||||
}
|
||||
} else {
|
||||
setReplyMessage(args)
|
||||
sendMessage("OK")
|
||||
}
|
||||
}
|
||||
|
||||
private fun setReplyMessage(message: MessageChain?) {
|
||||
JNRPluginConfig.replyMessage = message?.serializeToMiraiCode() ?: ""
|
||||
JNudgeReply.logger.info("已设置戳一戳回复内容 \"${JNRPluginConfig.replyMessage}\"")
|
||||
}
|
||||
}
|
110
src/main/kotlin/top/jie65535/jnr/JNRCommand.kt
Normal file
110
src/main/kotlin/top/jie65535/jnr/JNRCommand.kt
Normal file
@ -0,0 +1,110 @@
|
||||
package top.jie65535.jnr
|
||||
|
||||
import kotlinx.coroutines.TimeoutCancellationException
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import net.mamoe.mirai.console.command.CommandSender
|
||||
import net.mamoe.mirai.console.command.CompositeCommand
|
||||
import net.mamoe.mirai.console.command.fold
|
||||
import net.mamoe.mirai.console.command.isUser
|
||||
import net.mamoe.mirai.event.EventPriority
|
||||
import net.mamoe.mirai.event.events.MessageEvent
|
||||
import net.mamoe.mirai.event.globalEventChannel
|
||||
import net.mamoe.mirai.event.nextEvent
|
||||
import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
|
||||
import net.mamoe.mirai.message.data.PlainText
|
||||
import net.mamoe.mirai.message.data.buildForwardMessage
|
||||
import net.mamoe.mirai.message.data.isContentBlank
|
||||
|
||||
object JNRCommand : CompositeCommand(
|
||||
JNudgeReply, "jnr",
|
||||
description = "戳一戳自动回复"
|
||||
) {
|
||||
private const val WAIT_REPLY_TIMEOUT_MS = 30000L
|
||||
|
||||
@SubCommand
|
||||
@Description("添加简单回复消息(权重默认为1)")
|
||||
suspend fun CommandSender.add(message: PlainText, weight: Int = 1) {
|
||||
if (weight < 1) {
|
||||
sendMessage("请设置正确的权重值")
|
||||
return
|
||||
}
|
||||
if (message.isContentBlank()) {
|
||||
sendMessage("消息内容不能为空")
|
||||
return
|
||||
}
|
||||
JNRPluginConfig.replyMessageList.add(ReplyMessage(message.serializeToMiraiCode(), weight))
|
||||
sendMessage("已添加一条消息,权重为$weight")
|
||||
}
|
||||
|
||||
@SubCommand
|
||||
@Description("添加回复消息(权重默认为1)")
|
||||
suspend fun CommandSender.add(weight: Int = 1) {
|
||||
if (weight < 1) {
|
||||
sendMessage("请设置正确的权重值")
|
||||
return
|
||||
}
|
||||
if (isUser()) {
|
||||
try {
|
||||
sendMessage("请在${WAIT_REPLY_TIMEOUT_MS / 1000}秒内发送要添加的消息内容,你可以发送空白消息来取消。")
|
||||
val msg = withTimeout(WAIT_REPLY_TIMEOUT_MS) {
|
||||
subject.globalEventChannel().nextEvent<MessageEvent>(EventPriority.MONITOR) { it.sender == user }
|
||||
}
|
||||
if (msg.message.isContentBlank()) {
|
||||
sendMessage("已取消")
|
||||
} else {
|
||||
JNRPluginConfig.replyMessageList.add(ReplyMessage(msg.message.serializeToMiraiCode(), weight))
|
||||
sendMessage("已添加一条消息,权重为$weight")
|
||||
}
|
||||
} catch (e: TimeoutCancellationException) {
|
||||
sendMessage("已取消")
|
||||
}
|
||||
} else {
|
||||
sendMessage("必须使用聊天环境执行该命令")
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand
|
||||
@Description("删除指定索引的回复消息")
|
||||
suspend fun CommandSender.remove(index: Int) {
|
||||
if (index < 0 || index >= JNRPluginConfig.replyMessageList.size) {
|
||||
sendMessage("目标索引超出范围")
|
||||
} else {
|
||||
JNRPluginConfig.replyMessageList.removeAt(index)
|
||||
sendMessage("OK")
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand
|
||||
@Description("清空回复消息列表")
|
||||
suspend fun CommandSender.clear() {
|
||||
JNRPluginConfig.replyMessageList.clear()
|
||||
sendMessage("OK")
|
||||
}
|
||||
|
||||
@SubCommand
|
||||
@Description("列出当前回复消息列表")
|
||||
suspend fun CommandSender.list() {
|
||||
val list = JNRPluginConfig.replyMessageList
|
||||
if (list.isEmpty()) {
|
||||
sendMessage("当前列表为空")
|
||||
} else {
|
||||
this.fold({
|
||||
val sb = StringBuilder()
|
||||
for (i in list.indices) {
|
||||
sb.appendLine(" - [$i] (${list[i].weight}) \"${list[i].message}\"")
|
||||
}
|
||||
sendMessage(sb.toString())
|
||||
}, {
|
||||
if (list.size > 1) {
|
||||
sendMessage(buildForwardMessage(subject) {
|
||||
for (i in list.indices) {
|
||||
bot named "[$i] (${list[i].weight})" says list[i].message.deserializeMiraiCode()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
sendMessage(list[0].message.deserializeMiraiCode())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,10 @@
|
||||
package me.jie65535.jnr
|
||||
package top.jie65535.jnr
|
||||
|
||||
import net.mamoe.mirai.console.data.AutoSavePluginConfig
|
||||
import net.mamoe.mirai.console.data.ValueDescription
|
||||
import net.mamoe.mirai.console.data.value
|
||||
import net.mamoe.mirai.event.EventPriority
|
||||
import net.mamoe.mirai.message.data.Message
|
||||
import net.mamoe.mirai.message.data.MessageChain
|
||||
import net.mamoe.mirai.message.data.MessageChainBuilder
|
||||
|
||||
/**
|
||||
* 插件配置
|
||||
@ -17,7 +15,7 @@ object JNRPluginConfig : AutoSavePluginConfig("jnr") {
|
||||
* @see Message
|
||||
*/
|
||||
@ValueDescription("戳一戳回复的消息")
|
||||
var replyMessage: String by value()
|
||||
var replyMessageList: MutableList<ReplyMessage> by value()
|
||||
|
||||
/**
|
||||
* 优先级 默认为高
|
@ -1,18 +1,14 @@
|
||||
package me.jie65535.jnr
|
||||
package top.jie65535.jnr
|
||||
|
||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
|
||||
import net.mamoe.mirai.console.data.AutoSavePluginConfig
|
||||
import net.mamoe.mirai.console.data.value
|
||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||
import net.mamoe.mirai.event.EventPriority
|
||||
import net.mamoe.mirai.event.events.NudgeEvent
|
||||
import net.mamoe.mirai.event.globalEventChannel
|
||||
import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
|
||||
import net.mamoe.mirai.message.data.Message
|
||||
import net.mamoe.mirai.message.data.isContentBlank
|
||||
import net.mamoe.mirai.utils.info
|
||||
import kotlin.random.Random
|
||||
|
||||
object JNudgeReply : KotlinPlugin(
|
||||
JvmPluginDescription(
|
||||
@ -27,10 +23,19 @@ object JNudgeReply : KotlinPlugin(
|
||||
override fun onEnable() {
|
||||
JNRPluginConfig.reload()
|
||||
JNRCommand.register()
|
||||
|
||||
Random.nextInt()
|
||||
globalEventChannel().subscribeAlways<NudgeEvent>(priority = JNRPluginConfig.priority) {
|
||||
if (target.id == bot.id && JNRPluginConfig.replyMessage.isNotBlank()) {
|
||||
subject.sendMessage(JNRPluginConfig.replyMessage.deserializeMiraiCode())
|
||||
if (target.id == bot.id && JNRPluginConfig.replyMessageList.isNotEmpty()) {
|
||||
val totalWeight = JNRPluginConfig.replyMessageList.sumOf { it.weight }
|
||||
var w = Random.nextInt(totalWeight)
|
||||
for (msg in JNRPluginConfig.replyMessageList) {
|
||||
if (w < msg.weight) {
|
||||
subject.sendMessage(msg.message.deserializeMiraiCode())
|
||||
break
|
||||
} else {
|
||||
w -= msg.weight
|
||||
}
|
||||
}
|
||||
if (JNRPluginConfig.priority != EventPriority.MONITOR && JNRPluginConfig.isIntercept)
|
||||
intercept()
|
||||
}
|
12
src/main/kotlin/top/jie65535/jnr/ReplyMessage.kt
Normal file
12
src/main/kotlin/top/jie65535/jnr/ReplyMessage.kt
Normal file
@ -0,0 +1,12 @@
|
||||
package top.jie65535.jnr
|
||||
|
||||
/**
|
||||
* 回复的消息
|
||||
* @param message 消息内容
|
||||
* @param weight 回复该消息的权重
|
||||
*/
|
||||
@kotlinx.serialization.Serializable
|
||||
data class ReplyMessage(
|
||||
val message: String,
|
||||
val weight: Int
|
||||
)
|
@ -1 +1 @@
|
||||
me.jie65535.jnr.JNudgeReply
|
||||
top.jie65535.jnr.JNudgeReply
|
Loading…
Reference in New Issue
Block a user