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

+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.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()
}
}