实现戳一戳间隔时间 (#7)

This commit is contained in:
2022-11-21 23:18:29 +08:00
parent fb0c1cceba
commit 8ea893a260
4 changed files with 98 additions and 16 deletions

View File

@ -10,6 +10,7 @@ MiraiConsolePlugin 自定义戳一戳回复消息
/jnr clear # 清空回复消息列表
/jnr list # 列出当前回复消息列表
/jnr remove <index> # 删除指定索引的回复消息
/jnr reload # 重载配置
```
## 特殊消息
@ -20,6 +21,26 @@ MiraiConsolePlugin 自定义戳一戳回复消息
- `#group.mute:30` 禁言30s, 可以自定义禁言时间, 单位秒
- `#ignore` 忽略
## 配置文件
文件位置:`config/me.jie65535.mirai-console-jnr-plugin/jnr.yml`
```yaml
# 戳一戳回复的消息
replyMessageList:
- message: 'Hello world'
weight: 1
# 事件优先级 从高到低可选 HIGHEST, HIGH, NORMAL, LOW, LOWEST, MONITOR
# 设置后需要重启插件生效
priority: HIGH
# 是否拦截事件 回复后可阻止其它插件响应戳一戳事件 优先级为MONITOR时拦截无效
isIntercept: true
# 群回复间隔0表示无限制
groupInterval: 0
# 用户私聊回复间隔0表示无限制
userInterval: 0
```
## 用例
![Use example image](doc/example.png)

View File

@ -14,6 +14,7 @@ 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
import top.jie65535.jnr.JNudgeReply.reload
object JNRCommand : CompositeCommand(
JNudgeReply, "jnr",
@ -107,4 +108,10 @@ object JNRCommand : CompositeCommand(
})
}
}
@SubCommand
@Description("重载配置")
suspend fun CommandSender.reload() {
JNRPluginConfig.reload()
}
}

View File

@ -21,8 +21,10 @@ object JNRPluginConfig : AutoSavePluginConfig("jnr") {
* 优先级 默认为高
* @see EventPriority
*/
@ValueDescription("事件优先级 从高到低可选 HIGHEST, HIGH, NORMAL, LOW, LOWEST, MONITOR\n" +
"设置后需要重启插件生效")
@ValueDescription(
"事件优先级 从高到低可选 HIGHEST, HIGH, NORMAL, LOW, LOWEST, MONITOR\n" +
"设置后需要重启插件生效"
)
var priority: EventPriority by value(EventPriority.HIGH)
/**
@ -31,4 +33,18 @@ object JNRPluginConfig : AutoSavePluginConfig("jnr") {
*/
@ValueDescription("是否拦截事件 回复后可阻止其它插件响应戳一戳事件 优先级为MONITOR时拦截无效")
var isIntercept: Boolean by value(true)
/**
* 群间隔时间单位秒
* 0 表示无限制
*/
@ValueDescription("群回复间隔0表示无限制")
var groupInterval: Long by value(0L)
/**
* 用户间隔单位秒
* 0 表示无限制
*/
@ValueDescription("用户私聊回复间隔0表示无限制")
var userInterval: Long by value(0L)
}

View File

@ -5,11 +5,13 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.nameCardOrNick
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.utils.info
import java.time.LocalDateTime
import kotlin.random.Random
object JNudgeReply : KotlinPlugin(
@ -22,6 +24,9 @@ object JNudgeReply : KotlinPlugin(
info("""自定义戳一戳回复插件""")
}
) {
private val groupLastReply = mutableMapOf<Long, LocalDateTime>()
private val userLastReply = mutableMapOf<Long, LocalDateTime>()
override fun onEnable() {
JNRPluginConfig.reload()
JNRCommand.register()
@ -29,25 +34,52 @@ object JNudgeReply : KotlinPlugin(
globalEventChannel().subscribeAlways<NudgeEvent>(priority = JNRPluginConfig.priority) {
if (target.id == bot.id && target.id != from.id && JNRPluginConfig.replyMessageList.isNotEmpty()) {
var replyList: List<ReplyMessage> = JNRPluginConfig.replyMessageList
if(subject !is Group){
val now = LocalDateTime.now()
var isReply = true
if (subject !is Group) {
if (JNRPluginConfig.userInterval > 0) {
val t = userLastReply[subject.id]
if (t == null || t.plusSeconds(JNRPluginConfig.userInterval) < now) {
userLastReply[subject.id] = now
} else {
isReply = false
}
}
replyList = replyList.filter { !it.message.startsWith("#group") }
}else{
if((from as Member).permission.level >= (subject as Group).botPermission.level){
} else {
if (JNRPluginConfig.groupInterval > 0) {
val t = groupLastReply[subject.id]
if (t == null || t.plusSeconds(JNRPluginConfig.groupInterval) < now) {
groupLastReply[subject.id] = now
} else {
isReply = false
}
}
if ((from as Member).permission.level >= (subject as Group).botPermission.level) {
replyList = replyList.filter { !it.message.startsWith("#group.mute:") }
}
}
val totalWeight = replyList.sumOf { it.weight }
var w = Random.nextInt(totalWeight)
for (msg in replyList) {
if (w < msg.weight) {
doReply(msg, this)
break
} else {
w -= msg.weight
// 判断间隔
if (isReply) {
val totalWeight = replyList.sumOf { it.weight }
var w = Random.nextInt(totalWeight)
for (msg in replyList) {
if (w < msg.weight) {
doReply(msg, this)
break
} else {
w -= msg.weight
}
}
} else {
logger.info("正在CD中本次已忽略")
}
if (JNRPluginConfig.priority != EventPriority.MONITOR && JNRPluginConfig.isIntercept)
// 拦截事件
if (JNRPluginConfig.priority != EventPriority.MONITOR && JNRPluginConfig.isIntercept) {
intercept()
}
}
}
@ -58,7 +90,10 @@ object JNudgeReply : KotlinPlugin(
if (message.message.startsWith("#")) {
when {
// 戳回去
message.message == "#nudge" -> event.from.nudge().sendTo(event.subject)
message.message == "#nudge" -> {
event.from.nudge().sendTo(event.subject)
logger.info("已尝试戳回发送者")
}
// 禁言
message.message.startsWith("#group.mute:") -> {
@ -69,6 +104,7 @@ object JNudgeReply : KotlinPlugin(
val member: Member = event.from as Member
try {
member.mute(duration)
logger.info("戳一戳禁言目标 ${member.nameCardOrNick}(${member.id}) $duration")
} catch (e: Throwable) {
logger.warning("戳一戳禁言失败", e)
}
@ -76,7 +112,9 @@ object JNudgeReply : KotlinPlugin(
}
// 忽略
message.message == "#ignore" -> Unit
message.message == "#ignore" -> {
logger.info("已忽略本次戳一戳回复")
}
// 其它
else -> event.subject.sendMessage(message.message.deserializeMiraiCode())