Compare commits

...

6 Commits

Author SHA1 Message Date
d5132d587c 更新Mirai依赖到2.16.0
优化正则表达式匹配条件
2025-03-01 00:14:01 +08:00
e6c476f948 更新Mirai依赖到2.15.0
修复返回null的问题
2024-03-25 23:56:45 +08:00
66f8798528 Update README 2023-02-10 13:19:16 +08:00
e2d4d00bb0 Update mirai version to 2.14.0 2023-02-10 13:16:21 +08:00
73a4b77c3b Add multiple matches
Update only matches plain text
2023-02-10 13:16:04 +08:00
7981ca3fef Add numeric range limit (<=100)
Add gradle-wrapper.properties
Fix multiple dice issue
2023-02-09 10:46:12 +00:00
5 changed files with 30 additions and 15 deletions

View File

@ -1,8 +1,8 @@
# J Dice # J Dice
基于Mirai Console的骰子插件 基于Mirai Console的骰子插件
用法十分简单,`3d6`表示投出`3`颗`6`面骰子,下限是`3`,上限是`3*6`即(3~18)。 用法十分简单, `3d6` 表示投出 `3` `6` 面骰子,下限是 `3`,上限是 `3*6` 即(3~18)。
`d20` 表示投出`1`颗`20`面骰子。 `d20` 表示投出`1`颗`20`面骰子,可以在一条消息中投多次,例如 `d20, d20 d20`(即正则可匹配多个结果)
匹配的正则表达式如下`\b(\d*)[dD](\d+)\b` 匹配的正则表达式如下 `\b(\d{0,3})[dD](\d{1,3})\b`

View File

@ -1,13 +1,13 @@
plugins { plugins {
val kotlinVersion = "1.7.10" val kotlinVersion = "1.8.10"
kotlin("jvm") version kotlinVersion kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion kotlin("plugin.serialization") version kotlinVersion
id("net.mamoe.mirai-console") version "2.13.4" id("net.mamoe.mirai-console") version "2.16.0"
} }
group = "top.jie65535" group = "top.jie65535"
version = "0.1.1" version = "0.3.0"
repositories { repositories {
maven("https://maven.aliyun.com/repository/public") maven("https://maven.aliyun.com/repository/public")

View File

@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-all.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0
gradlew vendored Normal file → Executable file
View File

View File

@ -2,9 +2,10 @@ package top.jie65535
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.event.GlobalEventChannel import net.mamoe.mirai.event.globalEventChannel
import net.mamoe.mirai.event.subscribeMessages import net.mamoe.mirai.event.subscribeMessages
import net.mamoe.mirai.message.data.MessageSource.Key.quote import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.utils.info import net.mamoe.mirai.utils.info
import kotlin.random.Random import kotlin.random.Random
@ -12,27 +13,36 @@ object JDice : KotlinPlugin(
JvmPluginDescription( JvmPluginDescription(
id = "top.jie65535.dice", id = "top.jie65535.dice",
name = "J Dice", name = "J Dice",
version = "0.1.1", version = "0.3.0",
) { ) {
author("jie65535") author("jie65535")
} }
) { ) {
private val regex = Regex("""\b(\d*)[dD](\d+)\b""") private val regex = Regex("""(?<!\S)(\d{0,3})[dD]([1-9]\d{0,2})(?!\S)""")
private val random = Random(System.currentTimeMillis()) private val random = Random(System.currentTimeMillis())
override fun onEnable() { override fun onEnable() {
GlobalEventChannel.parentScope(this) globalEventChannel().subscribeMessages {
.subscribeMessages { has<PlainText> { plainText ->
regex.finding { regex.findAll(plainText.content).map {
val (c, d) = it.destructured val (c, d) = it.destructured
val count = if (c.isEmpty()) 1 else c.toInt() val count = if (c.isEmpty()) 1 else c.toInt()
val top = d.toInt() val top = d.toInt()
if (count > 0 && top > 1) { if (count in 1..100 && top in 2..100) {
val value = random.nextInt(count, count * top + 1) var value = 0
subject.sendMessage(message.quote() + value.toString()) for (i in 1..count) {
value += random.nextInt(1, top + 1)
}
value.toString()
} else {
null
} }
}.filter{ it != null }.joinToString().let {
if (it.isNotEmpty())
subject.sendMessage(message.quote() + it)
} }
} }
}
logger.info { "Plugin loaded. https://github.com/jie65535/JDice" } logger.info { "Plugin loaded. https://github.com/jie65535/JDice" }
} }