feat: add caculate parse

This commit is contained in:
2024-09-21 14:12:08 +08:00
parent d75fbb9daf
commit 60b41ead3b
4 files changed
+31 -13

No files matched your search

+1
View File
@@ -38,6 +38,7 @@ dependencies {
implementation(libs.animated.gif.lib) implementation(libs.animated.gif.lib)
implementation(libs.simple.java.mail) implementation(libs.simple.java.mail)
implementation(libs.jsoup) implementation(libs.jsoup)
implementation(libs.nashorn.core)
} }
tasks.build { tasks.build {
+2
View File
@@ -9,6 +9,7 @@ motdPingerVersion = "1.0.1"
animatedGifLibVersion = "1.4" animatedGifLibVersion = "1.4"
simpleJavaMailVersion = "8.11.3" simpleJavaMailVersion = "8.11.3"
jsoupVersion = "1.18.1" jsoupVersion = "1.18.1"
nashornVersion = "15.4"
[libraries] [libraries]
rOneBot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" } rOneBot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" }
@@ -20,6 +21,7 @@ sqliteJDBC = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqlite
animated-gif-lib = { group = "com.madgag", name = "animated-gif-lib", version.ref = "animatedGifLibVersion" } animated-gif-lib = { group = "com.madgag", name = "animated-gif-lib", version.ref = "animatedGifLibVersion" }
simple-java-mail = { group = "org.simplejavamail", name = "simple-java-mail", version.ref = "simpleJavaMailVersion" } simple-java-mail = { group = "org.simplejavamail", name = "simple-java-mail", version.ref = "simpleJavaMailVersion" }
jsoup = { group = "org.jsoup", name = "jsoup", version.ref = "jsoupVersion" } jsoup = { group = "org.jsoup", name = "jsoup", version.ref = "jsoupVersion" }
nashorn-core = { group = "org.openjdk.nashorn", name = "nashorn-core", version.ref = "nashornVersion" }
[bundles] [bundles]
+3 -13
View File
@@ -35,6 +35,7 @@ import cn.rtast.fancybot.commands.misc.UnsetZiBiCommand
import cn.rtast.fancybot.commands.misc.ZiBiCommand import cn.rtast.fancybot.commands.misc.ZiBiCommand
import cn.rtast.fancybot.commands.parse.AsciiArtCommand import cn.rtast.fancybot.commands.parse.AsciiArtCommand
import cn.rtast.fancybot.commands.parse.BVParseCommand import cn.rtast.fancybot.commands.parse.BVParseCommand
import cn.rtast.fancybot.commands.parse.CalculateCommand
import cn.rtast.fancybot.commands.parse.GitHubParseCommand import cn.rtast.fancybot.commands.parse.GitHubParseCommand
import cn.rtast.fancybot.commands.parse.ImageURLCommand import cn.rtast.fancybot.commands.parse.ImageURLCommand
import cn.rtast.fancybot.commands.parse.ReverseGIFCommand import cn.rtast.fancybot.commands.parse.ReverseGIFCommand
@@ -85,9 +86,8 @@ class FancyBot : OneBotListener {
val messageId = message.messageId val messageId = message.messageId
println("$sender($senderId: $groupId >>> $messageId): $msg") println("$sender($senderId: $groupId >>> $messageId): $msg")
if (message.rawMessage.contains("1+1")) { val calculateResult = CalculateCommand.parse(message.rawMessage)
message.reply("1+1=2") calculateResult?.let { message.reply(calculateResult) }
}
coroutineScope.launch { coroutineScope.launch {
message.message.forEach { message.message.forEach {
@@ -190,16 +190,6 @@ class FancyBot : OneBotListener {
.build() .build()
this.sendGroupMessage(groupId, msg) this.sendGroupMessage(groupId, msg)
} }
override suspend fun onPrivateMessage(message: PrivateMessage, json: String) {
if (message.rawMessage.contains("1+1")) {
message.reply("1+1=2")
}
if (message.rawMessage == "ping") {
message.reply("pong")
}
}
} }
val configManager = ConfigManager() val configManager = ConfigManager()
@@ -0,0 +1,25 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/21
*/
package cn.rtast.fancybot.commands.parse
import javax.script.ScriptEngineManager
import javax.script.ScriptException
object CalculateCommand {
private val engine = ScriptEngineManager().getEngineByName("JavaScript")
fun parse(input: String): String? {
return try {
engine.eval(input)
} catch (_: ScriptException) {
"Invalid expression"
return null
}.toString()
}
}