Add multiple matches

Update only matches plain text
This commit is contained in:
2023-02-10 13:16:04 +08:00
parent 7981ca3fef
commit 73a4b77c3b

View File

@ -2,9 +2,10 @@ package top.jie65535
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.event.GlobalEventChannel
import net.mamoe.mirai.event.globalEventChannel
import net.mamoe.mirai.event.subscribeMessages
import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.utils.info
import kotlin.random.Random
@ -21,18 +22,24 @@ object JDice : KotlinPlugin(
private val random = Random(System.currentTimeMillis())
override fun onEnable() {
GlobalEventChannel.parentScope(this)
.subscribeMessages {
regex.finding {
globalEventChannel().subscribeMessages {
has<PlainText> { plainText ->
regex.findAll(plainText.content).map {
val (c, d) = it.destructured
val count = if (c.isEmpty()) 1 else c.toInt()
val top = d.toInt()
if (count > 0 && count <= 100 && top > 1 && top <= 100) {
if (count in 1..100 && top in 2..100) {
var value = 0
for (i in 1..count) {
value += random.nextInt(1, top + 1)
}
subject.sendMessage(message.quote() + value.toString())
value.toString()
} else {
null
}
}.joinToString().let {
if (it.isNotEmpty())
subject.sendMessage(message.quote() + it)
}
}
}