feat: add caculate parse
This commit is contained in:
4 files changed
+31
-13
No files matched your search
@@ -38,6 +38,7 @@ dependencies {
|
||||
implementation(libs.animated.gif.lib)
|
||||
implementation(libs.simple.java.mail)
|
||||
implementation(libs.jsoup)
|
||||
implementation(libs.nashorn.core)
|
||||
}
|
||||
|
||||
tasks.build {
|
||||
|
||||
@@ -9,6 +9,7 @@ motdPingerVersion = "1.0.1"
|
||||
animatedGifLibVersion = "1.4"
|
||||
simpleJavaMailVersion = "8.11.3"
|
||||
jsoupVersion = "1.18.1"
|
||||
nashornVersion = "15.4"
|
||||
|
||||
[libraries]
|
||||
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" }
|
||||
simple-java-mail = { group = "org.simplejavamail", name = "simple-java-mail", version.ref = "simpleJavaMailVersion" }
|
||||
jsoup = { group = "org.jsoup", name = "jsoup", version.ref = "jsoupVersion" }
|
||||
nashorn-core = { group = "org.openjdk.nashorn", name = "nashorn-core", version.ref = "nashornVersion" }
|
||||
|
||||
[bundles]
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ import cn.rtast.fancybot.commands.misc.UnsetZiBiCommand
|
||||
import cn.rtast.fancybot.commands.misc.ZiBiCommand
|
||||
import cn.rtast.fancybot.commands.parse.AsciiArtCommand
|
||||
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.ImageURLCommand
|
||||
import cn.rtast.fancybot.commands.parse.ReverseGIFCommand
|
||||
@@ -85,9 +86,8 @@ class FancyBot : OneBotListener {
|
||||
val messageId = message.messageId
|
||||
println("$sender($senderId: $groupId >>> $messageId): $msg")
|
||||
|
||||
if (message.rawMessage.contains("1+1")) {
|
||||
message.reply("1+1=2")
|
||||
}
|
||||
val calculateResult = CalculateCommand.parse(message.rawMessage)
|
||||
calculateResult?.let { message.reply(calculateResult) }
|
||||
|
||||
coroutineScope.launch {
|
||||
message.message.forEach {
|
||||
@@ -190,16 +190,6 @@ class FancyBot : OneBotListener {
|
||||
.build()
|
||||
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()
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user